git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/format.ts
/** 42 → "42s", 300 → "5m", 7200 → "2.0h", like `midas stats`. */
export function dur(s: number | null | undefined): string {
if (s == null || !Number.isFinite(s)) return '–'
if (s < 90) return `${Math.round(s)}s`
if (s < 5400) return `${Math.round(s / 60)}m`
if (s < 3 * 86400) return `${(s / 3600).toFixed(1)}h`
return `${(s / 86400).toFixed(1)}d`
}
export function now(): number {
return Math.floor(Date.now() / 1000)
}
/** Seconds since a unix time, as a duration. */
export function since(t: number | null | undefined): string {
return t == null ? '–' : dur(now() - t)
}
/** A clock's way of saying a span, for a person: 40 → "now", 420 → "7m", 6840 → "1h 54m", 50400 → "14h", 2 days → "2d". */
export function span(s: number | null | undefined): string {
if (s == null || !Number.isFinite(s)) return '–'
if (s < 60) return 'now'
const m = Math.floor(s / 60)
if (m < 60) return `${m}m`
const h = Math.floor(m / 60)
if (h < 10) return m % 60 ? `${h}h ${m % 60}m` : `${h}h`
if (h < 48) return `${h}h`
return `${Math.floor(h / 24)}d`
}
/** How long since a unix time, as `span` says it. */
export function age(t: number | null | undefined): string {
return t == null ? '–' : span(now() - t)
}
export function bytes(b: number | null | undefined): string {
if (b == null) return '–'
if (b >= 1e12) return `${(b / 1e12).toFixed(1)} TB`
if (b >= 1e9) return `${Math.round(b / 1e9)} GB`
return `${Math.round(b / 1e6)} MB`
}
/*
* The formatters, made once: `toLocaleString` with options builds a new
* Intl.DateTimeFormat on every call, and a year of Activity called it for
* every bar's tooltip and every table row (1.6 s of a range switch's 3 s).
*/
const WHEN = new Intl.DateTimeFormat(undefined, {month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit'})
const CLOCK = new Intl.DateTimeFormat(undefined, {hour: '2-digit', minute: '2-digit'})
const WEEKDAY = new Intl.DateTimeFormat(undefined, {weekday: 'short', day: 'numeric', month: 'short'})
const DAY = new Intl.DateTimeFormat(undefined, {month: 'short', day: 'numeric'})
/** `toLocaleString` said "Invalid Date" where `format` throws: a bad time must not take the page down. */
const fmt = (f: Intl.DateTimeFormat, d: Date) => (Number.isNaN(d.getTime()) ? '–' : f.format(d))
/** A unix time or an RFC 3339 string as local "Oct 17 14:05". */
export function when(t: number | string | null | undefined): string {
if (t == null) return '–'
const d = typeof t === 'number' ? new Date(t * 1000) : new Date(t)
return fmt(WHEN, d)
}
/** A unix time to come, as a person says it: "today 14:05", "tomorrow 09:00", "Sat 31 Oct 09:00"; `from` is now. */
export function ahead(t: number, from: Date = new Date()): string {
const d = new Date(t * 1000)
const clock = fmt(CLOCK, d)
const days = Math.round((new Date(d).setHours(0, 0, 0, 0) - new Date(from).setHours(0, 0, 0, 0)) / 86400000)
if (days === 0) return `today ${clock}`
if (days === 1) return `tomorrow ${clock}`
return `${fmt(WEEKDAY, d)} ${clock}`
}
export function day(d: Date): string {
return fmt(DAY, d)
}
/** A session's name made short: `bridge-cse_01CNAdFm…` keeps its distinguishing head. */
export function shortName(name: string, max = 22): string {
return name.length > max ? `${name.slice(0, max - 1)}…` : name
}