Files
archie/ui/src/lib/format.ts
T
sam 0dc5bf9a9b
deploy / gate (push) Successful in 4m54s
Quality / check (push) Successful in 6m44s
deploy / archied-image (push) Successful in 3m44s
deploy / agent-image (push) Successful in 11s
deploy / dist-zip (push) Skipped
fix(ui): times honour the Appearance zone, one duration formatter everywhere (#1761)
lib/format.ts gains shortTime(value, zone) and becomes the home of the
single duration() formatter (moved from timeline-event.ts, which
re-exports it so the stage rail and duration.test.ts are unchanged).
LogTable's time column, audit's From/To chips and chat session titles now
format in the zone chosen under Appearance instead of the browser's.
StepNode, schedule runDuration, labels formatMs and ChatToolCall all
delegate to duration(), removing the "12ms" vs "12 ms" vs "under 1 s"
split.
2026-11-06 22:21:45 +11:00

67 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Display formatting shared across the dashboard pages. Pure functions: no
* DOM, no framework.
*/
/** Humanised relative time: "2 min ago". Raw timestamps make people do maths. */
export function ago(value: string | number | Date | null | undefined): string {
if (!value) return "–";
const then = value instanceof Date ? value : new Date(value);
const secs = Math.floor((Date.now() - then.getTime()) / 1000);
if (!Number.isFinite(secs)) return "–";
if (secs < 45) return "just now";
const units: Array<[string, number]> = [
["min", 60],
["hr", 3600],
["day", 86400],
["wk", 604800],
];
let label = "min";
let size = 60;
for (const [l, s] of units) {
if (secs >= s) [label, size] = [l, s];
}
const n = Math.floor(secs / size);
return `${n} ${label}${n === 1 ? "" : "s"} ago`;
}
/** Compact number: 2_400_000 -> "2.4M". Long digit strings do not scan. */
export function compact(n: number | null | undefined): string {
if (n == null || Number.isNaN(n)) return "–";
return new Intl.NumberFormat(undefined, {
notation: "compact",
maximumFractionDigits: 1,
}).format(n);
}
/** An exact date and time in zone, or in the browser's zone when zone is empty. */
export function exactTime(value: Date, zone: string): string {
return value.toLocaleString(undefined, zone ? { timeZone: zone, timeZoneName: "short" } : undefined);
}
/** A time of day in zone, or in the browser's zone when zone is empty: "14:03:22". */
export function shortTime(value: string | number | Date, zone = ""): string {
const d = value instanceof Date ? value : new Date(value);
if (Number.isNaN(d.getTime())) return "--:--:--";
return d.toLocaleTimeString(undefined, { hour12: false, ...(zone ? { timeZone: zone } : {}) });
}
/** The one duration formatter every view uses: "under 1 s", "41 s", "41 min 41 s",
* "2 h 3 min". Empty string when there is no duration. */
export function duration(milliseconds: unknown): string {
const ms = Number(milliseconds);
if (!Number.isFinite(ms) || ms < 0) return "";
if (ms < 1000) return "under 1 s";
const seconds = ms / 1000;
// Under 60s: 1 dp when seconds < 10, 0 dp otherwise (40.6 s, but 41 s).
if (seconds < 60) return `${seconds.toFixed(seconds < 10 ? 1 : 0)} s`;
const roundedSeconds = Math.round(seconds);
const minutes = Math.floor(roundedSeconds / 60);
const remSec = roundedSeconds % 60;
if (minutes < 60)
return remSec === 0 ? `${minutes} min` : `${minutes} min ${remSec} s`;
const hours = Math.floor(minutes / 60);
const remMin = minutes - hours * 60;
return remMin === 0 ? `${hours} h` : `${hours} h ${remMin} min`;
}