feat(web): add formatDuration helper
Renders seconds as mm:ss or h:mm:ss. Negative inputs clamp to 0:00 so a bad backend value doesn't crash the UI.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
// Formats a non-negative number of seconds as `mm:ss` when under an hour,
|
||||
// `h:mm:ss` otherwise. Negative values clamp to zero (callers shouldn't
|
||||
// pass them, but the UI shouldn't crash if they do).
|
||||
export function formatDuration(seconds: number): string {
|
||||
const total = Math.max(0, Math.floor(seconds));
|
||||
const h = Math.floor(total / 3600);
|
||||
const m = Math.floor((total % 3600) / 60);
|
||||
const s = total % 60;
|
||||
const ss = String(s).padStart(2, '0');
|
||||
if (h > 0) {
|
||||
const mm = String(m).padStart(2, '0');
|
||||
return `${h}:${mm}:${ss}`;
|
||||
}
|
||||
return `${m}:${ss}`;
|
||||
}
|
||||
Reference in New Issue
Block a user