Open History to see the last commit
1import { mFallback, msg } from "gt-i18n";
2import type { M } from "gt-i18n";
3
4/** @public */
5export function formatDuration(durationMs: number, resolve: M = mFallback): string {
6 if (!Number.isFinite(durationMs) || durationMs < 0) return resolve(msg`${{ ms: 0 }}ms`);
7 if (durationMs < 1_000) {
8 return resolve(msg`${{ ms: Math.max(1, Math.round(durationMs)) }}ms`);
9 }
10 if (durationMs < 10_000) {
11 return resolve(msg`${{ seconds: Math.round(durationMs / 100) / 10 }}s`);
12 }
13 const totalSeconds = Math.round(durationMs / 1_000);
14 // Choose the unit after rounding so 59.5 seconds never renders as "60s".
15 if (totalSeconds < 60) return resolve(msg`${{ seconds: totalSeconds }}s`);
16 const hours = Math.floor(totalSeconds / 3_600);
17 const minutes = Math.floor((totalSeconds % 3_600) / 60);
18 const seconds = totalSeconds % 60;
19 if (hours === 0) return resolve(msg`${{ minutes }}m ${{ seconds }}s`);
20 if (minutes > 0) {
21 return resolve(msg`${{ hours }}h ${{ minutes }}m ${{ seconds }}s`);
22 }
23 return seconds > 0 ? resolve(msg`${{ hours }}h ${{ seconds }}s`) : resolve(msg`${{ hours }}h`);
24}