Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191// posts stop counting up after a day: past that the relative label is a static// date, so nothing needs the ticking clock anymoreexport 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 listconst 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 changesexport 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` givesconst 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<string, unknown>()
function cached<T>(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 weekexport 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 localeexport 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`}