// Relative-time formatting shared by the monitor widget and the dashboard // cockpit, so their "5m ago" labels never drift. // Compact relative label. `deltaMs = now - target`: positive is past ("5m // ago"), negative is future ("in 5m"). Steps s → m → h → d. export function relativeTime(deltaMs: number): string { const past = deltaMs >= 0; const s = Math.round(Math.abs(deltaMs) / 1000); const val = s < 60 ? `${s}s` : s < 3600 ? `${Math.round(s / 60)}m` : s < 86400 ? `${Math.round(s / 3600)}h` : `${Math.round(s / 86400)}d`; return past ? `${val} ago` : `in ${val}`; } // Format an epoch-ms marker per an absolute/relative flag. Absolute is // now-independent (locale string); relative depends on the live clock, so it // returns null until `now` is set (post-mount) to avoid an SSR/hydration // mismatch and a nonsensical "0s ago" flash. Callers render "…" for null. export function fmtTime( ms: number, now: number | null, absolute: boolean, ): string | null { if (absolute) return new Date(ms).toLocaleString(); if (now === null) return null; return relativeTime(now - ms); }