Skip to content

Commit 9e06973

Browse files
committed
fix: roll rounded durations into seconds
1 parent c79c380 commit 9e06973

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/infra/format-time/format-duration.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export function formatDurationPrecise(
3333
if (!Number.isFinite(ms)) {
3434
return "unknown";
3535
}
36-
if (ms < 1000) {
37-
return `${Math.max(0, Math.round(ms))}ms`;
36+
const roundedMs = Math.max(0, Math.round(ms));
37+
if (roundedMs < 1000) {
38+
return `${roundedMs}ms`;
3839
}
3940
return formatDurationSeconds(ms, {
4041
decimals: options.decimals ?? 2,
@@ -55,8 +56,9 @@ export function formatDurationCompact(
5556
if (ms == null || !Number.isFinite(ms) || ms <= 0) {
5657
return undefined;
5758
}
58-
if (ms < 1000) {
59-
return `${Math.round(ms)}ms`;
59+
const roundedMs = Math.round(ms);
60+
if (roundedMs < 1000) {
61+
return `${roundedMs}ms`;
6062
}
6163
const sep = options?.spaced ? " " : "";
6264
const totalSeconds = Math.round(ms / 1000);
@@ -85,8 +87,9 @@ export function formatDurationHuman(ms?: number | null, fallback = "n/a"): strin
8587
if (ms == null || !Number.isFinite(ms) || ms < 0) {
8688
return fallback;
8789
}
88-
if (ms < 1000) {
89-
return `${Math.round(ms)}ms`;
90+
const roundedMs = Math.round(ms);
91+
if (roundedMs < 1000) {
92+
return `${roundedMs}ms`;
9093
}
9194
const sec = Math.round(ms / 1000);
9295
if (sec < 60) {

src/infra/format-time/format-time.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("format-duration", () => {
3434
expectFormatterCases(formatDurationCompact, [
3535
{ input: 500, expected: "500ms" },
3636
{ input: 999, expected: "999ms" },
37+
{ input: 999.6, expected: "1s" },
3738
{ input: 1000, expected: "1s" },
3839
{ input: 45000, expected: "45s" },
3940
{ input: 59000, expected: "59s" },
@@ -71,6 +72,7 @@ describe("format-duration", () => {
7172
it("formats single-unit outputs and day threshold behavior", () => {
7273
expectFormatterCases(formatDurationHuman, [
7374
{ input: 500, expected: "500ms" },
75+
{ input: 999.6, expected: "1s" },
7476
{ input: 5000, expected: "5s" },
7577
{ input: 180000, expected: "3m" },
7678
{ input: 7200000, expected: "2h" },
@@ -88,7 +90,7 @@ describe("format-duration", () => {
8890
{ input: 999, expected: "999ms" },
8991
{ input: -1, expected: "0ms" },
9092
{ input: -500, expected: "0ms" },
91-
{ input: 999.6, expected: "1000ms" },
93+
{ input: 999.6, expected: "1s" },
9294
{ input: 1000, expected: "1s" },
9395
{ input: 1500, expected: "1.5s" },
9496
{ input: 1234, expected: "1.23s" },

0 commit comments

Comments
 (0)