Skip to content
Closed
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
10 changes: 10 additions & 0 deletions __tests__/lib/format-duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ describe("formatDuration", () => {
expect(formatDuration(3200)).toBe("3.2s");
});

it("carries rounded seconds into minutes", () => {
expect(formatDuration(59600)).toBe("59.6s");
expect(formatDuration(59999)).toBe("1m 0s");
expect(formatDuration(119600)).toBe("2m 0s");
});

it("boundary: exactly 60000ms", () => {
expect(formatDuration(60000)).toBe("1m 0s");
});
Expand All @@ -42,6 +48,10 @@ describe("formatDuration", () => {
expect(formatDuration(8100000)).toBe("2h 15m");
});

it("carries rounded minutes into hours", () => {
expect(formatDuration(3599600)).toBe("1h 0m");
});

it("large: 86400000ms (24h)", () => {
expect(formatDuration(86400000)).toBe("24h 0m");
});
Expand Down
8 changes: 5 additions & 3 deletions lib/format-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export function formatRelativeTime(ts: number): string {
export function formatDuration(ms: number): string {
if (ms < 1000) return `${ms}ms`;
const seconds = ms / 1000;
if (seconds < 60) return `${seconds.toFixed(1)}s`;
const totalMinutes = Math.floor(seconds / 60);
const roundedTenths = Math.round(seconds * 10) / 10;
if (roundedTenths < 60) return `${roundedTenths.toFixed(1)}s`;
const roundedSeconds = Math.round(seconds);
const totalMinutes = Math.floor(roundedSeconds / 60);
if (totalMinutes >= 60) {
const hours = Math.floor(totalMinutes / 60);
const remainingMinutes = totalMinutes % 60;
return `${hours}h ${remainingMinutes}m`;
}
const remainingSeconds = (seconds % 60).toFixed(0);
const remainingSeconds = roundedSeconds % 60;
return `${totalMinutes}m ${remainingSeconds}s`;
}