games-hub: fix streak counting across DST transitions
- Commit
5917d465516f75e3a947c10b873ed8a5e848996a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 06:02 UTC (28 days ago)
- Parents
dce4bd8
What: StreakCalculator now compares calendar days (LocalDate.toEpochDays) instead of exact 24h gaps between local day-start instants when deciding whether two play days are consecutive, in both longestStreak and currentStreak. Why: HubCalculatorsTest.streakConsecutiveDaysAndGap failed with expected:<2> but was:<1> on the stale-streak longest assertion. Root cause: dayStart() returns local-midnight instants, but longestStreak treated two days as consecutive only when their starts differ by exactly 86_400_000 ms, and currentStreak derived yesterday as todayStart minus 24h. Across a DST transition consecutive local days start 23h/25h apart โ e.g. after the Nov 1 fall-back, Nov 1 starts at 07:00 UTC and Nov 2 at 08:00 UTC (US Pacific), a 25h gap โ so a genuine Nov 1 + Nov 2 streak was split into two 1-day runs (and a same-week current streak could read 0). This is a real production bug: every user's streaks miscount around both DST changes, twice a year. The test's UTC-anchored sessions landing on Nov 1/Nov 2 local are legitimately consecutive days. Rejected alternatives: pinning the test to UTC and leaving production untouched (hides a user-facing bug to make the suite green); special-casing 23h/25h gaps in millis (re-derives calendar logic badly โ epoch days are the exact abstraction); changing the test helper's anchoring (the test is correct; the calculator was wrong). User-visible effect: daily streaks in games-hub stay correct through DST transitions instead of resetting or undercounting for days around the change. No other behavior changes; day bucketing still uses local midnights. Verification: reproduced pre-fix in isolation (:games:hub:testDevUnitTest --tests HubCalculatorsTest FAILED, same assertion); post-fix full :games:hub:testDevUnitTest 16/0/0 plus :games:hub:lint green, and the suite re-run green under TZ=America/Denver, TZ=Australia/Sydney (southern spring forward) and TZ=UTC.
Diff
games/hub/src/main/java/com/vayunmathur/games/hub/util/StreakCalculator.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/util/StreakCalculator.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/util/StreakCalculator.ktindex 80def03971..879f69950f 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/util/StreakCalculator.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/util/StreakCalculator.kt@@ -1,6 +1,5 @@ package com.vayunmathur.games.hub.util -import kotlin.time.Duration.Companion.days import kotlin.time.Instant import kotlinx.datetime.TimeZone import kotlinx.datetime.atStartOfDayIn@@ -46,7 +45,9 @@ object StreakCalculator { var maxStreak = 1 var curRun = 1 for (i in 1 until sortedDays.size) {- if (sortedDays[i] - sortedDays[i - 1] == DAY_MILLIS) {+ // Compare calendar days, not millis: across a DST transition+ // consecutive local days start 23h/25h apart, never 24h.+ if (epochDay(sortedDays[i]) - epochDay(sortedDays[i - 1]) == 1L) { curRun++ if (curRun > maxStreak) maxStreak = curRun } else {@@ -57,24 +58,29 @@ object StreakCalculator { } private fun currentStreak(sortedDays: List<Long>, now: Long): Int {- val todayStart = dayStart(now)- val yesterdayStart = todayStart - DAY_MILLIS- val lastDay = sortedDays.last()- if (lastDay != todayStart && lastDay != yesterdayStart) return 0+ val today = epochDay(now)+ val lastDay = epochDay(sortedDays.last())+ if (lastDay != today && lastDay != today - 1) return 0 var streak = 1 var idx = sortedDays.lastIndex - 1- var expectedDay = lastDay - DAY_MILLIS- while (idx >= 0 && sortedDays[idx] >= expectedDay) {- if (sortedDays[idx] == expectedDay) {+ var expectedDay = lastDay - 1+ while (idx >= 0 && epochDay(sortedDays[idx]) >= expectedDay) {+ if (epochDay(sortedDays[idx]) == expectedDay) { streak++- expectedDay -= DAY_MILLIS+ expectedDay-- } idx-- } return streak } - private val DAY_MILLIS = 1.days.inWholeMilliseconds+ private fun epochDay(millis: Long): Long {+ val tz = TimeZone.currentSystemDefault()+ return Instant.fromEpochMilliseconds(millis)+ .toLocalDateTime(tz)+ .date+ .toEpochDays()+ } private fun dayStart(millis: Long): Long { val tz = TimeZone.currentSystemDefault()