[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051/** 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 `<time datetime>`. */export function isoTime(seconds: number): string { return new Date(seconds * 1000).toISOString();}
/** The short form of a commit id, as git itself abbreviates it. */export function shortId(id: string): string { return id.slice(0, 7);}