// Formatting for times, durations and measurements. export const dash = '—'; export function ago(ts) { if (!ts) return dash; const s = Date.now() / 1000 - ts; if (s < 45) return 'just now'; if (s < 3600) return `${Math.round(s / 60)} min ago`; if (s < 86400) return `${Math.round(s / 3600)} h ago`; return `${Math.round(s / 86400)} d ago`; } export const clock = (ts) => new Date(ts * 1000).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' }); export const hm = (ts) => new Date(ts * 1000).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); export const when = (ts) => (ts ? new Date(ts * 1000).toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }) : dash); export const day = (ts) => new Date(ts * 1000).toLocaleDateString([], { month: 'short', day: 'numeric' }); export function dur(s) { if (s == null) return dash; if (s < 60) return `${Math.round(s)} s`; if (s < 3600) return `${Math.round(s / 60)} min`; if (s < 86400) return `${(s / 3600).toFixed(1)} h`; return `${(s / 86400).toFixed(1)} d`; } export const ms = (v) => (v == null ? dash : v < 10 ? `${v.toFixed(1)} ms` : `${Math.round(v).toLocaleString()} ms`); export const pct = (v, digits = 0) => (v == null ? dash : `${(v * 100).toFixed(digits)}%`); export const num = (n) => (n == null ? dash : Number(n).toLocaleString()); export const mbps = (v) => (v == null ? dash : `${v < 10 ? v.toFixed(1) : Math.round(v)} Mbps`); /** A time label fitting the span: clock for short ranges, date + hour for long ones. */ export const axisTime = (span) => (ts) => (span > 2 * 86400 ? `${day(ts)} ${hm(ts)}` : hm(ts));