From cafc8d7cee4918ff703c20e194e88a5485461ab2 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:08:03 +0000 Subject: [PATCH] fix(format): round a session span before choosing its unit A session of 59.6 s read "60 s" and one of 59.5 min read "60 min": span() picked the unit from the raw value and rounded afterwards. Round first, then pick, as msShort already does. --- src/format.test.ts | 5 +++++ src/format.ts | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/format.test.ts b/src/format.test.ts index 2b5d2756..b0db57c9 100644 --- a/src/format.test.ts +++ b/src/format.test.ts @@ -89,6 +89,11 @@ describe("会话的跨度", () => { expect(span(42_400)).toBe("42 s"); setLang("zh"); }); + + it("进了位就按下一档写", () => { + expect(span(59_600)).toBe("1 分"); + expect(span(3_570_000)).toBe("1.0 小时"); + }); }); describe("绝对时间", () => { diff --git a/src/format.ts b/src/format.ts index 28e69c9d..ec33aee2 100644 --- a/src/format.ts +++ b/src/format.ts @@ -60,12 +60,17 @@ export function msShort(n: number): string { /** * 一段跨度(一次会话从头到尾):`42 秒`、`7 分`、`1.5 小时`。和 `ms` 说的不是同一个量: * 那是一个请求等了多久,要精确到毫秒;这是一次任务做了多久,读的是量级。 + * + * **先取整再定单位**:59.6 秒取整是 60,该写成「1 分」,不是「60 秒」;59.5 分同理是 + * 「1.0 小时」,不是「60 分」。 */ export function span(n: number): string { // 取文案要在调用的那一刻,不能提到模块级 —— 换了语言它不会跟着换 const t = textOf(formatText).span; - if (n < 60_000) return t.seconds(Math.round(n / 1000)); - if (n < 3_600_000) return t.minutes(Math.round(n / 60_000)); + const secs = Math.round(n / 1000); + if (secs < 60) return t.seconds(secs); + const mins = Math.round(n / 60_000); + if (mins < 60) return t.minutes(mins); return t.hours((n / 3_600_000).toFixed(1)); }