The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // Formatters pin the default zone when built, and Hermes keeps Date's local offset until a cold |
| 2 | // start (react/react-native#59131), so formatting and day checks follow the zone Intl reports. |
| 3 | function buildFormatters(timeZone: string) { |
| 4 | return { |
| 5 | timeZone, |
| 6 | time: new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit", timeZone }), |
| 7 | date: new Intl.DateTimeFormat(undefined, { month: "numeric", day: "numeric", timeZone }), |
| 8 | dateWithYear: new Intl.DateTimeFormat(undefined, { |
| 9 | month: "numeric", |
| 10 | day: "numeric", |
| 11 | year: "numeric", |
| 12 | timeZone, |
| 13 | }), |
| 14 | year: new Intl.DateTimeFormat(undefined, { year: "numeric", timeZone }), |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | // Reading the zone builds a formatter (tens of µs), so feed rows share one read per second. |
| 19 | const ZONE_RECHECK_MS = 1_000; |
| 20 | let formatters: ReturnType<typeof buildFormatters> | null = null; |
| 21 | let zoneCheckedAt = 0; |
| 22 | function currentFormatters() { |
| 23 | const now = Date.now(); |
| 24 | if (formatters && Math.abs(now - zoneCheckedAt) < ZONE_RECHECK_MS) return formatters; |
| 25 | zoneCheckedAt = now; |
| 26 | const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 27 | if (!formatters || formatters.timeZone !== timeZone) formatters = buildFormatters(timeZone); |
| 28 | return formatters; |
| 29 | } |
| 30 | |
| 31 | /** Time only for today's messages; older ones lead with the day and month. */ |
| 32 | export function formatMessageTimestamp( |
| 33 | input: string, |
| 34 | nowMs: number = Date.now(), |
| 35 | timeFormatter?: Intl.DateTimeFormat, |
| 36 | ): string { |
| 37 | const timestamp = Date.parse(input); |
| 38 | if (Number.isNaN(timestamp)) return ""; |
| 39 | const f = currentFormatters(); |
| 40 | const time = (timeFormatter ?? f.time).format(timestamp); |
| 41 | const dateWithYear = f.dateWithYear.format(timestamp); |
| 42 | if (dateWithYear === f.dateWithYear.format(nowMs)) return time; |
| 43 | const sameYear = f.year.format(timestamp) === f.year.format(nowMs); |
| 44 | return `${sameYear ? f.date.format(timestamp) : dateWithYear} ${time}`; |
| 45 | } |
| 46 | |
| 47 | /** `is24Hour` overrides the locale's hour cycle with the system clock preference when known. */ |
| 48 | export function createMessageTimeFormatter(is24Hour: boolean | undefined, locale?: string) { |
| 49 | let cached: { timeZone: string; formatter: Intl.DateTimeFormat } | null = null; |
| 50 | return (input: string, nowMs?: number): string => { |
| 51 | const { timeZone } = currentFormatters(); |
| 52 | if (cached?.timeZone !== timeZone) { |
| 53 | cached = { |
| 54 | timeZone, |
| 55 | formatter: new Intl.DateTimeFormat(locale, { |
| 56 | hour: "numeric", |
| 57 | minute: "2-digit", |
| 58 | timeZone, |
| 59 | ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), |
| 60 | }), |
| 61 | }; |
| 62 | } |
| 63 | return formatMessageTimestamp(input, nowMs, cached.formatter); |
| 64 | }; |
| 65 | } |