Skip to content

Fix duration display showing 6:60 instead of 7:00#13

Merged
gsbernstein merged 1 commit into
masterfrom
cursor/fix-duration-format-rollover-a266
Jun 16, 2026
Merged

Fix duration display showing 6:60 instead of 7:00#13
gsbernstein merged 1 commit into
masterfrom
cursor/fix-duration-format-rollover-a266

Conversation

@gsbernstein

@gsbernstein gsbernstein commented Jun 16, 2026

Copy link
Copy Markdown
Owner

Problem

Sleep durations can display as 6:60 instead of 7:00.

Root cause

TimeFormatter.formatDuration computed hours and minutes separately from seconds:

let hours = Int(duration) / 3600
let minutes = Int((duration.truncatingRemainder(dividingBy: 3600) / 60).rounded())

Hours are truncated while minutes are rounded independently. Any duration from X:59:30 to X:59:59 produces X hours with a remainder that rounds to 60 minutes — without rolling into the next hour. This affects any call to formatDuration, not just day totals.

HealthKit durations are exact TimeInterval values (including seconds), while individual segment rows round for display. A day total can therefore be something like 6h 59m 45s even when the listed segments appear to sum to a whole number of hours. If the total were exactly 420 minutes (25200 seconds), the old code would actually display 7:00 correctly — so 6:60 indicates sub-minute seconds in the underlying data, not a summation bug.

Fix

Calculate total minutes first, then split into hours and minutes:

let totalMinutes = Int((duration / 60).rounded())
let hours = totalMinutes / 60
let minutes = totalMinutes % 60

Durations like 6h 59m 45s now correctly display as 7:00.

Open in Web Open in Cursor 

When summing many sleep segments, floating-point imprecision could leave
the remainder at 3599 seconds. Rounding that to minutes produced 60
without rolling into the next hour.

Calculate total minutes first, then split into hours and minutes so
values like 420 minutes always display as 7:00.

Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
@gsbernstein

Copy link
Copy Markdown
Owner Author

Thanks @virbedi!

@gsbernstein gsbernstein marked this pull request as ready for review June 16, 2026 19:13
@gsbernstein gsbernstein requested a review from Copilot June 16, 2026 19:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an edge-case in TimeFormatter.formatDuration where floating-point imprecision could cause minute rounding to hit 60 without carrying into the hour (e.g., showing 6:60 instead of 7:00) when summing many sleep segment durations.

Changes:

  • Compute totalMinutes by rounding the full duration in minutes.
  • Derive hours and minutes from totalMinutes via integer division/modulo to guarantee minutes is always 0...59.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gsbernstein

Copy link
Copy Markdown
Owner Author

The bots were wrong, this shouldn't require floating point imprecision or summing multiple ranges. Just normal rounding. Updated description.

@gsbernstein gsbernstein merged commit d968445 into master Jun 16, 2026
1 check passed
@gsbernstein gsbernstein deleted the cursor/fix-duration-format-rollover-a266 branch June 16, 2026 19:22
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.

3 participants