mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 21:10:59 +00:00
Per-stage SLEEP_DURATION rows carry the stage segment's END instant, so a night where the user falls asleep before local midnight spread its stages across two calendar days. Keying each stage by its own day split one overnight sleep into two partial nights, and the headline last-night tile showed only the post-midnight slice. Reconstruct nights by clustering stage segments into sessions: a new session starts only when the next segment's start (end minus its minute duration) is more than three hours after the running session end. A single long stage block no longer splits a night, a daytime nap stays separable from the following overnight block, and the gap is measured on the absolute instant so the clustering is DST-immune. Each session is keyed by the local wake day of its latest segment end, matching the convention that a sleep session belongs to the morning you wake up. Collapse a multi-source night to one canonical source before summing, using the user's sleep source-priority ladder (default WHOOP > Apple Health > Withings), so a dual-source account no longer double-counts the total. Sources are never blended within a night. Bound the series sleep read to one year even on the 'Alle' range, matching the slim slice's sleep window, so a multi-year dual-source account does not walk a six-figure stage-row set into JS. Add fixtures straddling local midnight, crossing a DST spring-forward, keeping a nap separable, and collapsing a dual-source night.