/** Formatting for display. Uses `Intl`, so no dependency and no locale data. */ const RELATIVE = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }); const ABSOLUTE = new Intl.DateTimeFormat(undefined, { dateStyle: "medium", timeStyle: "short", }); /** Largest unit first, so the first one that fits is the one to use. */ const UNITS: [Intl.RelativeTimeFormatUnit, number][] = [ ["year", 365 * 24 * 60 * 60], ["month", 30 * 24 * 60 * 60], ["week", 7 * 24 * 60 * 60], ["day", 24 * 60 * 60], ["hour", 60 * 60], ["minute", 60], ]; /** * "3 days ago", for a committer time in seconds since the epoch. * * Commit dates are usually read as "how long ago", not as a date — the exact * timestamp goes in the `title` and the `datetime` attribute instead. */ export function relativeTime(seconds: number, now = Date.now()): string { const elapsed = seconds - Math.floor(now / 1000); for (const [unit, size] of UNITS) { if (Math.abs(elapsed) >= size) { return RELATIVE.format(Math.round(elapsed / size), unit); } } return RELATIVE.format(Math.round(elapsed), "second"); } /** The full timestamp, for the tooltip. */ export function absoluteTime(seconds: number): string { return ABSOLUTE.format(new Date(seconds * 1000)); } /** A machine-readable timestamp for `