// posts stop counting up after a day: past that the relative label is a static // date, so nothing needs the ticking clock anymore export const RECENT_MS = 24 * 60 * 60 * 1000 export const DEFAULT_TIMEZONE = 'UTC' const VALID_TIMEZONES = new Set(Intl.supportedValuesOf('timeZone')) export function list_timezones(): string[] { return [...VALID_TIMEZONES] } export function is_valid_timezone(tz: string): boolean { return VALID_TIMEZONES.has(tz) } // Intl has no API to enumerate locales, so we hardcode a list const COMMON_LOCALES = [ 'en-US', 'en-GB', 'en-AU', 'en-CA', 'es-ES', 'es-MX', 'pt-BR', 'pt-PT', 'fr-FR', 'fr-CA', 'de-DE', 'it-IT', 'nl-NL', 'pl-PL', 'ru-RU', 'uk-UA', 'tr-TR', 'ar-SA', 'hi-IN', 'zh-CN', 'zh-TW', 'ja-JP', 'ko-KR', 'vi-VN', 'th-TH', 'id-ID', 'sv-SE', 'fi-FI' ] export function list_locales(): string[] { return COMMON_LOCALES } export function is_valid_locale(tag: string): boolean { try { return Intl.DateTimeFormat.supportedLocalesOf([tag]).length > 0 } catch { return false } } let resolved_timezone: string | undefined export function browser_timezone(): string | undefined { if (typeof window === 'undefined') return undefined return (resolved_timezone ??= Intl.DateTimeFormat().resolvedOptions().timeZone) } export function browser_locale(): string | undefined { if (typeof window === 'undefined') return undefined return navigator.language } // whether a timestamp is recent enough that its relative label still changes export function is_recent(iso: string, now: number = Date.now()): boolean { return now - new Date(iso).getTime() < RECENT_MS } // numeric date fields, e.g. 07/14/2026 in en-US or 14.07.2026 in de-DE — the // locale still governs field order and separators, just not the spelled-out // month `dateStyle` gives const NUMERIC_DATE: Intl.DateTimeFormatOptions = { year: 'numeric', month: '2-digit', day: '2-digit' } // Constructing an Intl formatter costs about 25µs, and a page of posts asks for // the same handful over and over — every post formats its own timestamp twice, // once on the server and again on hydration. The locale and timezone come from // the viewer's settings and change at most once a session, so key on them and // keep the formatters. (Measured: 200 `full_date` calls, 5.4ms → 0.2ms.) const formatters = new Map() function cached(key: string, make: () => T): T { let formatter = formatters.get(key) as T | undefined if (formatter === undefined) { formatter = make() formatters.set(key, formatter) } return formatter } // now / seconds / minutes / hours / days, plain calendar date if older than a week export function relative_time( iso: string, now: number, timezone: string = DEFAULT_TIMEZONE, locale?: string, exact = false ): string { const then = new Date(iso) const ms = now - then.getTime() const secs = Math.round(ms / 1000) const unit_count = (unit: Intl.NumberFormatOptions['unit'], n: number) => cached( `unit|${unit}|${locale}`, () => new Intl.NumberFormat(locale, { style: 'unit', unit, unitDisplay: 'narrow' }) ).format(n) if (exact && secs < 60) { return ms < 1000 ? unit_count('millisecond', Math.max(0, ms)) : unit_count('second', secs) } if (!exact && secs < 10) { return cached( `rel|${locale}`, () => new Intl.RelativeTimeFormat(locale, { style: 'narrow', numeric: 'auto' }) ).format(0, 'second') } if (secs < 60) return unit_count('second', secs) const mins = Math.floor(secs / 60) if (mins < 60) return unit_count('minute', mins) const hours = Math.floor(mins / 60) if (hours < 24) return unit_count('hour', hours) const days = Math.floor(hours / 24) if (days < 7) return unit_count('day', days) return cached( `date|${locale}|${timezone}`, () => new Intl.DateTimeFormat(locale, { ...NUMERIC_DATE, timeZone: timezone }) ).format(then) } // the full timestamp shown on hover, in the viewer's chosen timezone and locale export function full_date( iso: string, timezone: string = DEFAULT_TIMEZONE, locale?: string ): string { return cached( `full|${locale}|${timezone}`, () => new Intl.DateTimeFormat(locale, { ...NUMERIC_DATE, timeZone: timezone, hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' }) ).format(new Date(iso)) } export function coarse_duration(seconds: number, locale?: string): string { const s = Math.max(0, Math.round(seconds)) const count = (n: number) => n.toLocaleString(locale) const d = Math.floor(s / 86400) const h = Math.floor((s % 86400) / 3600) const m = Math.floor((s % 3600) / 60) if (d) return `${count(d)}d ${h}h` if (h) return `${h}h ${m}m` if (m) return `${m}m` return `${s}s` }