From d23787638cd11108dcc928f385cd0fbb70a98def Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Mon, 3 Aug 2026 10:28:46 +0200 Subject: [PATCH] Attribute a night to its wake day by the calendar --- .../health/providers/health_sync.dart | 6 ++- lib/features/measurements/widgets/charts.dart | 4 +- .../health/providers/health_sync_test.dart | 44 +++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/features/health/providers/health_sync.dart b/lib/features/health/providers/health_sync.dart index d697cf99c..aec82ac58 100644 --- a/lib/features/health/providers/health_sync.dart +++ b/lib/features/health/providers/health_sync.dart @@ -583,9 +583,11 @@ class HealthSyncNotifier extends _$HealthSyncNotifier { /// count towards the next day, so a night of sleep lands on the day the user /// wakes up instead of being split at midnight. DateTime _dayOf(DateTime date, HealthMetric metric) { - final day = DateTime(date.year, date.month, date.day); final rollover = metric.dayRollsOverAtHour; - return rollover != null && date.hour >= rollover ? day.add(const Duration(days: 1)) : day; + final rollsOver = rollover != null && date.hour >= rollover; + + // Calendar arithmetic, not +24h: a DST day is 23 or 25 hours long + return DateTime(date.year, date.month, date.day + (rollsOver ? 1 : 0)); } static DateTime _earlier(DateTime a, DateTime b) => a.isBefore(b) ? a : b; diff --git a/lib/features/measurements/widgets/charts.dart b/lib/features/measurements/widgets/charts.dart index a28eb23e3..2bcec301f 100644 --- a/lib/features/measurements/widgets/charts.dart +++ b/lib/features/measurements/widgets/charts.dart @@ -1044,9 +1044,7 @@ MeasurementHeatmapGrid buildHeatmapGrid( int maxWeeks = heatmapMaxWeeks, DateTime? today, }) { - // Days are shifted through the constructor rather than by adding a Duration: - // a day is not always 24 hours long, and an hour lost to a time change would - // put the date in the neighbouring cell + // Calendar arithmetic, not Duration: a DST day is 23 or 25 hours long DateTime dayOf(DateTime date) => DateTime(date.year, date.month, date.day); DateTime shift(DateTime day, int days) => DateTime(day.year, day.month, day.day + days); DateTime mondayOf(DateTime date) => shift(date, -(date.weekday - 1)); diff --git a/test/features/health/providers/health_sync_test.dart b/test/features/health/providers/health_sync_test.dart index ab80ca9fa..c80f1e0f7 100644 --- a/test/features/health/providers/health_sync_test.dart +++ b/test/features/health/providers/health_sync_test.dart @@ -59,6 +59,14 @@ const _idWLb = '0000000e-0000-4000-8000-000000000014'; // w-lb const _catHrId = 'aaaaaaaa-0000-4000-8000-000000000001'; const _catSleepId = 'bbbbbbbb-0000-4000-8000-000000000002'; +/// Whether the local zone shifts its clocks in the night the DST test uses. +/// +/// The zone belongs to the process, so a single test cannot pick its own; where +/// there is no shift, that test cannot tell the two ways of counting a day +/// apart and is skipped rather than passing without meaning. +final _springsForward = + DateTime(2026, 3, 29).timeZoneOffset != DateTime(2026, 3, 30).timeZoneOffset; + @GenerateMocks([HealthRepository, MeasurementRepository, AuthCredentialsStorage]) void main() { // The notifier registers an AppLifecycleListener for the resume re-sync, @@ -969,6 +977,42 @@ void main() { expect(byDay[dailyAggregateExternalId(categoryId, DateTime(2026, 1, 2))], 400); }); + test( + 'keeps a night across a time change as one aggregate', + () async { + // With +24h the two halves of the night key apart but format to the same + // date, so both would claim one external id + when(measurements.getAllOnce()).thenAnswer((_) async => []); + stubReadings([ + HealthReading( + type: HealthDataType.SLEEP_ASLEEP, + value: 120, + date: DateTime(2026, 3, 29, 23, 0), + dateTo: DateTime(2026, 3, 30, 1, 0), + externalId: _idS1, + ), + HealthReading( + type: HealthDataType.SLEEP_ASLEEP, + value: 300, + date: DateTime(2026, 3, 30, 1, 0), + dateTo: DateTime(2026, 3, 30, 6, 0), + externalId: _idS2, + ), + ]); + + final count = await createNotifier().sync(); + + expect(count, 1); + final entry = + verify(measurements.addLocalDrift(captureAny)).captured.single as MeasurementEntry; + final categoryId = _sleepCategoryId(measurements, MetricType.sleepTotal); + expect(entry.externalId, dailyAggregateExternalId(categoryId, DateTime(2026, 3, 30))); + expect(entry.date, DateTime(2026, 3, 30)); + expect(entry.value, 420); + }, + skip: _springsForward ? false : 'needs a time zone that shifts on 2026-03-30 (CI: Berlin)', + ); + test('imports each sleep stage into its own category', () async { when(measurements.getAllOnce()).thenAnswer((_) async => []); stubReadings([