Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/__tests__/format-countdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ describe("formatCountdown", () => {
it("floors fractional seconds", () => {
expect(formatCountdown(90.9)).toBe("01:30")
})

it('returns "01:00:00" for 3600 seconds', () => {
expect(formatCountdown(3600)).toBe("01:00:00")
})

it('returns "23:54:06" for 86046 seconds', () => {
expect(formatCountdown(86046)).toBe("23:54:06")
})

it('returns "01:30" for 90 seconds (unchanged MM:SS)', () => {
expect(formatCountdown(90)).toBe("01:30")
})
})
8 changes: 6 additions & 2 deletions src/lib/format-countdown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/** Converts seconds into a "MM:SS" string (e.g. 90 → "01:30"). */
/** Converts seconds into a "MM:SS" string (e.g. 90 → "01:30") or "HH:MM:SS" for durations ≥ 1 hour (e.g. 86046 → "23:54:06"). */
export function formatCountdown(seconds: number): string {
const s = Math.max(0, Math.floor(seconds))
const m = Math.floor(s / 60)
const h = Math.floor(s / 3600)
const m = Math.floor((s % 3600) / 60)
const rem = s % 60
if (h > 0) {
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}:${String(rem).padStart(2, "0")}`
}
return `${String(m).padStart(2, "0")}:${String(rem).padStart(2, "0")}`
}
Loading