forge
Explore
Log in

Don't you dare go Hollow

Contribution calendar counts days and today in the viewer's timezone

Browse files at this commit

1f7ef933ba11f1bb8a0cba6d68b0e4125983a7dfAhmet Kaan GümüşParent: 7b115ab
  • fix
    • contribution calendar counts days and today in the viewer's timezone
Show full message
🐛 fix: contribution calendar counts days and today in the viewer's timezone

Events are grouped by minute and placed on their local day, so the calendar no longer
ends at yesterday for zones ahead of UTC between midnight and their offset.
crates/forge-core/src/store/contributions.rsmodified+38 -15
@@ -1,32 +1,39 @@
11 //! Daily contribution counts for one user, for the dashboard heatmap.
22
3+use std::collections::BTreeMap;
4+
5+use jiff::Timestamp;
36 use turso::Value;
47
58 use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256};
69 use super::{Store, col_int, col_text, fetch_all};
710 use crate::error::Result;
11+use crate::tz::local_date;
812
913 impl Store {
10- /// Contributions per UTC day by `user_id` at or after `since` (an RFC 3339 stamp), as
14+ /// Contributions per day in `tz` by `user_id` at or after `since` (an RFC 3339 stamp), as
1115 /// `(YYYY-MM-DD, count)` sorted by day ascending; days with no activity are left out.
16+ /// Events are grouped by minute and each minute is placed on its local day, so zones with
17+ /// half-hour or quarter-hour offsets and DST changes land on the right day.
1218 /// A contribution is a push (ref deletions are left out), an issue or pull request opened
1319 /// (pull requests are `issues` rows with `kind = 'pull'`), or a comment.
1420 pub async fn contribution_counts(
1521 &self,
1622 user_id: &str,
1723 since: &str,
24+ tz: &str,
1825 ) -> Result<Vec<(String, u32)>> {
1926 // Parameters: ?1 user, ?2 since, ?3 and ?4 zero oids.
20- let sql = "SELECT day, COUNT(*) FROM ( \
21- SELECT substr(created_at, 1, 10) AS day FROM push_events \
27+ let sql = "SELECT minute, COUNT(*) FROM ( \
28+ SELECT substr(created_at, 1, 16) AS minute FROM push_events \
2229 WHERE actor_id = ?1 AND created_at >= ?2 AND new_oid NOT IN (?3, ?4) \
2330 UNION ALL \
24- SELECT substr(created_at, 1, 10) FROM issues \
31+ SELECT substr(created_at, 1, 16) FROM issues \
2532 WHERE author_id = ?1 AND created_at >= ?2 AND kind IN ('issue', 'pull') \
2633 UNION ALL \
27- SELECT substr(created_at, 1, 10) FROM comments \
34+ SELECT substr(created_at, 1, 16) FROM comments \
2835 WHERE author_id = ?1 AND created_at >= ?2 \
29- ) GROUP BY day ORDER BY day";
36+ ) GROUP BY minute";
3037 let params = vec![
3138 Value::Text(user_id.to_string()),
3239 Value::Text(since.to_string()),
@@ -39,10 +46,18 @@ impl Store {
3946 Ok((col_text(row, 0)?, col_int(row, 1)?))
4047 })
4148 .await?;
42- Ok(rows
43- .into_iter()
44- .map(|(day, count)| (day, u32::try_from(count).unwrap_or(u32::MAX)))
45- .collect())
49+ // `minute` is `YYYY-MM-DDTHH:MM` in UTC; the stamp is completed with seconds to parse.
50+ let mut days: BTreeMap<String, u32> = BTreeMap::new();
51+ for (minute, count) in rows {
52+ let Ok(ts) = format!("{minute}:00Z").parse::<Timestamp>() else {
53+ continue;
54+ };
55+ let day = local_date(ts, tz).to_string();
56+ let count = u32::try_from(count).unwrap_or(u32::MAX);
57+ let total = days.entry(day).or_default();
58+ *total = total.saturating_add(count);
59+ }
60+ Ok(days.into_iter().collect())
4661 }
4762 }
4863
@@ -135,23 +150,31 @@ mod tests {
135150 .unwrap();
136151
137152 let since = "2026-03-01T00:00:00.000000000Z";
138- let counts = store.contribution_counts(&tahinli.id, since).await.unwrap();
153+ let counts = store
154+ .contribution_counts(&tahinli.id, since, "UTC")
155+ .await
156+ .unwrap();
139157 assert_eq!(
140158 counts,
141159 vec![("2026-03-01".to_string(), 2), ("2026-03-02".to_string(), 3)]
142160 );
143161
144- // `since` filters out the first day.
162+ // `since` filters out the first day. In Kiritimati (+14) the 10:00 UTC events fall on
163+ // the next local day.
145164 let later = store
146- .contribution_counts(&tahinli.id, "2026-03-02T00:00:00.000000000Z")
165+ .contribution_counts(
166+ &tahinli.id,
167+ "2026-03-02T00:00:00.000000000Z",
168+ "Pacific/Kiritimati",
169+ )
147170 .await
148171 .unwrap();
149- assert_eq!(later, vec![("2026-03-02".to_string(), 3)]);
172+ assert_eq!(later, vec![("2026-03-03".to_string(), 3)]);
150173
151174 // A user with nothing in range gets an empty list.
152175 assert!(
153176 store
154- .contribution_counts(&bob.id, "2026-03-03T00:00:00.000000000Z")
177+ .contribution_counts(&bob.id, "2026-03-03T00:00:00.000000000Z", "UTC")
155178 .await
156179 .unwrap()
157180 .is_empty()
crates/forge-core/src/tz.rsmodified+11 -1
@@ -1,6 +1,6 @@
11 //! IANA timezone helpers backed by jiff's bundled tzdb, so results do not depend on the host.
22
3-use jiff::{Timestamp, tz::TimeZone};
3+use jiff::{Timestamp, civil, tz::TimeZone};
44
55 use crate::error::{Error, Result};
66
@@ -9,6 +9,16 @@ fn zone_or_utc(tz: &str) -> TimeZone {
99 TimeZone::get(tz).unwrap_or(TimeZone::UTC)
1010 }
1111
12+/// The civil date and clock time `ts` falls on in `tz`, DST-aware. Unknown zones fall back to UTC.
13+pub fn local_datetime(ts: Timestamp, tz: &str) -> civil::DateTime {
14+ ts.to_zoned(zone_or_utc(tz)).datetime()
15+}
16+
17+/// The civil date `ts` falls on in `tz`. Unknown zones fall back to UTC.
18+pub fn local_date(ts: Timestamp, tz: &str) -> civil::Date {
19+ local_datetime(ts, tz).date()
20+}
21+
1222 /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC".
1323 pub fn validate_timezone(name: &str) -> Result<()> {
1424 TimeZone::get(name)
crates/forge-web/src/pages/dashboard.rsmodified+22 -10
@@ -8,7 +8,7 @@ use std::time::Duration;
88 use forge_core::store::RepoSort;
99 use forge_core::store::repo_listing::RepoScope;
1010 use forge_core::{Notification, Repo, Visibility};
11-use time::OffsetDateTime;
11+use jiff::Timestamp;
1212 use topcoat::Result;
1313 use topcoat::context::{Cx, app_context};
1414 use topcoat::router::response::Response;
@@ -29,6 +29,12 @@ use crate::views::{SIDE_REPOS, display_name, now_unix, relative_time, shell};
2929 /// How far back the calendar reads contributions: 53 weeks plus the days of the current week.
3030 const CALENDAR_DAYS: u64 = 371;
3131
32+/// The `since` stamp for the calendar. One extra day covers the viewer's local day, which can
33+/// start before the UTC day that `CALENDAR_DAYS` reaches back to.
34+fn calendar_since() -> String {
35+ forge_core::time::stamp_ago(Duration::from_secs((CALENDAR_DAYS + 1) * 86_400))
36+}
37+
3238 /// Unread inbox rows in the "Needs your attention" column.
3339 const ATTENTION_ROWS: u32 = 5;
3440
@@ -145,8 +151,12 @@ async fn dashboard_fingerprint(state: &AppState, user: &forge_core::User) -> Res
145151 repo.name, repo.visibility, repo.description
146152 ));
147153 }
148- let since = forge_core::time::stamp_ago(Duration::from_secs(CALENDAR_DAYS * 86_400));
149- for (day, count) in state.store.contribution_counts(&user.id, &since).await? {
154+ let since = calendar_since();
155+ for (day, count) in state
156+ .store
157+ .contribution_counts(&user.id, &since, &user.timezone)
158+ .await?
159+ {
150160 lines.push(format!("day {day} {count}"));
151161 }
152162 for note in state
@@ -169,14 +179,16 @@ async fn load_dashboard(
169179 lang: Lang,
170180 ) -> Result<Dashboard> {
171181 let repos = state.store.list_repos_by_owner(&user.id).await?;
172- let now = OffsetDateTime::now_utc();
182+ let local_now = forge_core::tz::local_datetime(Timestamp::now(), &user.timezone);
173183 let today = (
174- i64::from(now.year()),
175- i64::from(u8::from(now.month())),
176- i64::from(now.day()),
184+ i64::from(local_now.year()),
185+ i64::from(local_now.month()),
186+ i64::from(local_now.day()),
177187 );
178- let since = forge_core::time::stamp_ago(Duration::from_secs(CALENDAR_DAYS * 86_400));
179- let counts = state.store.contribution_counts(&user.id, &since).await?;
188+ let counts = state
189+ .store
190+ .contribution_counts(&user.id, &calendar_since(), &user.timezone)
191+ .await?;
180192 let calendar = heatmap::build(&counts, today);
181193 let now_secs = now_unix();
182194 let attention = state
@@ -188,7 +200,7 @@ async fn load_dashboard(
188200 .collect();
189201 Ok(Dashboard {
190202 name: display_name(lang, &user.username),
191- hour: now.hour(),
203+ hour: u8::try_from(local_now.hour()).unwrap_or_default(),
192204 repos,
193205 calendar,
194206 attention,