// Dates, plurals and sizes for the chat, ported from a Golem15 SPA's // format.ts (only the helpers the chat uses). The SPA formatted for pl-PL in // Europe/Warsaw; here the locale and the time zone are module settings with // those defaults, and the layer's components set them from the app's locale // and the browser's time zone (setFormatLocale). let LOCALE = 'pl-PL' let TZ: string | undefined = 'Europe/Warsaw' interface Formatters { time: Intl.DateTimeFormat dayLong: Intl.DateTimeFormat date: Intl.DateTimeFormat dateShort: Intl.DateTimeFormat key: Intl.DateTimeFormat } let f: Formatters | null = null function formatters(): Formatters { if (f) return f const dtf = (opts: Intl.DateTimeFormatOptions) => new Intl.DateTimeFormat(LOCALE, { timeZone: TZ, ...opts }) f = { time: dtf({ hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }), dayLong: dtf({ weekday: 'long', day: 'numeric', month: 'long' }), date: dtf({ day: 'numeric', month: 'long', year: 'numeric' }), dateShort: dtf({ day: 'numeric', month: 'short' }), key: new Intl.DateTimeFormat('sv-SE', { timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit' }), } return f } /** * setFormatLocale sets the locale (a BCP 47 tag such as en-US) and, * optionally, the IANA time zone of every chat date; an empty time zone * means the runtime's own. */ export function setFormatLocale(locale: string, timeZone?: string) { const zone = timeZone === undefined ? TZ : timeZone || undefined if (locale === LOCALE && zone === TZ) return LOCALE = locale || LOCALE TZ = zone f = null } /** The current format locale. */ export function formatLocale(): string { return LOCALE } /** Polish plural: 1 plik, 2 pliki, 5 plików, 12 plików, 22 pliki. */ export function plural(n: number, one: string, few: string, many: string): string { const abs = Math.abs(n) if (abs === 1) return one const last = abs % 10 const last2 = abs % 100 if (last >= 2 && last <= 4 && !(last2 >= 12 && last2 <= 14)) return few return many } /** "5 pytań", "1 pytanie". */ export function count(n: number, one: string, few: string, many: string): string { return `${n} ${plural(n, one, few, many)}` } export const fmtTime = (iso: string) => formatters().time.format(new Date(iso)) export const fmtDay = (iso: string) => formatters().dayLong.format(new Date(iso)) export const fmtDate = (iso: string) => formatters().date.format(new Date(iso)) export const fmtDateShort = (iso: string) => formatters().dateShort.format(new Date(iso)) export const fmtDateTime = (iso: string) => `${fmtDate(iso)}, ${fmtTime(iso)}` /** The calendar day of an instant in the format time zone, `YYYY-MM-DD`. */ export const dayKey = (iso: string | Date) => formatters().key.format(typeof iso === 'string' ? new Date(iso) : iso) export const todayKey = (now: Date = new Date()) => dayKey(now) /** Whole calendar days from `now` to `iso` (negative when past). */ export function daysUntil(iso: string, now: Date = new Date()): number { const a = Date.parse(`${todayKey(now)}T00:00:00Z`) const b = Date.parse(`${dayKey(iso)}T00:00:00Z`) return Math.round((b - a) / 86_400_000) } /** A byte count: "512 B", "12 KB", "1,5 MB" (the decimal mark of the locale). */ export function fmtSize(bytes: number): string { if (bytes < 1024) return `${bytes} B` if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB` const mb = (bytes / 1024 / 1024).toFixed(1) return `${LOCALE.startsWith('en') ? mb : mb.replace('.', ',')} MB` }