Skip to content

fix(sleep): prevent epoch dates on Sleep startup (#137) - #138

Open
voiceagentscc wants to merge 1 commit into
foureight84:mainfrom
voiceagentscc:fix/sleep-date-zero
Open

voiceagentscc wants to merge 1 commit into
foureight84:mainfrom
voiceagentscc:fix/sleep-date-zero

Conversation

@voiceagentscc

Copy link
Copy Markdown
Contributor

fix(sleep): prevent epoch dates on Sleep startup (#137)

Fixes #137.

A cold start with the ring disconnected and PulseLoop on the Sleep screen can briefly show December 31, 1969 in the Day header. This PR gives the screen the correct reference-night date immediately, while stored data is still loading, and prevents zero or negative day values from reaching the header or date picker.

Cause and changes

SleepState.shownDayMillis starts at 0L. The header formats that initial value before the asynchronous database rebuild supplies the real day. Unix timestamp zero becomes December 31, 1969 in America/New_York; it can remain visible if the rebuild fails.

  • Initialize the displayed day with TimeUtil.referenceNightLocal(), the same calculation used after loading.
  • Resolve zero or negative day values to the reference night at the display boundary.
  • Use that one resolved day for the primary label, secondary label, and date picker. Valid historical selections remain unchanged.

The existing reference-night rule stays consistent across startup and loading: before 4 AM local, both show yesterday; from 4 AM, both show today. Ring connection does not select the day. A date change when the clock actually crosses 4 AM remains expected.

iOS parity and related work

This restores iOS's existing behavior, following the README's guidance to keep the interfaces aligned. SleepView on iOS derives the date from the current time and passes it through dayReferenceNight, without an initial zero timestamp. The iOS source, issues, and PRs were reviewed; the iOS app was not run to verify its behavior.

Android PR #28 introduced this startup path in 0d7212c, when porting iOS PR #84's day navigation. Searches of both repositories found no duplicate report or existing fix. Android issue #107 and PR #108 addressed incorrect dates in decoded sleep history; this fix only changes the screen's initial state and date presentation.

Tests

Five new SleepDayDateTest cases cover:

  • An empty initial state before any database or ring data arrives.
  • Zero and negative day values, including Long.MIN_VALUE.
  • The reference-night boundary immediately before and at 4 AM.
  • Local-midnight fallback across spring and autumn daylight-saving transitions.
  • Preservation of valid historical selections.

The initial-state, invalid-value, boundary, and historical-selection checks cover New York, UTC, and Tokyo. The daylight-saving cases use New York.

Validated from a clean worktree based on current Android main (3f39c09), using Java 17 as CI does:

./gradlew --no-daemon :app:testDebugUnitTest :app:assembleDebug

The full suite passed 1,642 tests, zero failures, errors, or skips, including all five new cases and the 13 existing TimeUtilTest cases. The debug APK built successfully. No test exclusions were needed.

Device validation and limits

Debug build 77 (2.11.0-sleep-date-pr-test-debug) was built from this PR's exact commit, 2c364b9, on fix/sleep-date-zero, then installed and tested on a Samsung Galaxy A71 running Android 13.

Verified on November 4, 2026:

  • A cold launch with the ring disconnected succeeded. Opening Sleep → Day showed Today — WED, NOV 4.
  • The date picker selected November 4, 2026, matching the header.
  • Choosing November 3 in the picker changed the header to Yesterday. The next-day control returned it to Today — WED, NOV 4.
  • Reconnecting the ring left the date unchanged. The phone was left with the ring connected and Sleep showing today.

No 1969 or 1970 date appeared in the inspected views. These checks inspected the rendered UI; they did not record its first frame before the database returned. The new initial-state regression test covers the value available during that interval.

There are no database migrations, stored-history changes, BLE protocol changes, or new public APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sleep: December 31, 1969 appears on startup before the ring connects

1 participant