Attribute a night to its wake day by the calendar

This commit is contained in:
Roland Geider
2026-08-03 10:28:46 +02:00
parent 86f44639c1
commit d23787638c
3 changed files with 49 additions and 5 deletions

View File

@@ -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;

View File

@@ -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));

View File

@@ -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 => <MeasurementCategory>[]);
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 => <MeasurementCategory>[]);
stubReadings([