Skip to main content
Script: scripts/data_migrations/backfill_garmin_sleep_end_datetime.py

Problem

Garmin’s durationInSeconds field covers only asleep time (deep + light + REM) and excludes the final wake period at the end of the session. This caused end_datetime and duration_seconds on event_record to be earlier than the actual session end, resulting in sleep scores anchored to the wrong date for users in non-UTC time zones.

What it does

For every Garmin sleep session that has a sleep_stages array in sleep_details, the script finds the latest end_time across all stages and uses it as the corrected end_datetime. duration_seconds is recomputed as the difference between start_datetime and the new end_datetime. Only records where the stage-derived end is later than the current end_datetime are updated — already-correct records are left untouched.

Usage

Options

Dry run output

Diff (min) is the difference in minutes between the old and new duration_seconds.