@devries-cameron20 / t3code-1 / commits / d775f6bff1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web,mobile): message timestamps follow a system time zone change (#32371)

Julius Marminge committed Nov 16, 2026 at 18:06 UTC d775f6bff14ffcb2b841c128a3a7a4867780194e
4 files changed +184 -39
apps/mobile/src/features/threads/messageTimestamp.test.ts
+31 -1
@@ -1,4 +1,4 @@
1 -import { describe, expect, it } from "vite-plus/test";
1 +import { describe, expect, it, vi } from "vite-plus/test";
2
3 import { createMessageTimeFormatter, formatMessageTimestamp } from "./messageTimestamp";
4
@@ -19,6 +19,36 @@ describe("formatMessageTimestamp", () => {
19 `${lastYear.toLocaleDateString(undefined, { month: "numeric", day: "numeric", year: "numeric" })} ${time(lastYear)}`,
20 );
21 });
22 +
23 + it("follows a system time zone change", () => {
24 + const previous = process.env.TZ;
25 + vi.useFakeTimers({ now: 0 });
26 + const sameDay = "2026-11-07T04:45:43.877Z";
27 + const earlierInWeek = "2026-11-04T20:00:00.000Z";
28 + const nowMs = Date.parse(sameDay);
29 + const timeIn = (iso: string, timeZone: string) =>
30 + new Date(iso).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit", timeZone });
31 + const dayIn = (iso: string, timeZone: string) =>
32 + new Date(iso).toLocaleDateString(undefined, { month: "numeric", day: "numeric", timeZone });
33 + try {
34 + // Node re-resolves the default zone when TZ is assigned, like the OS zone changing.
35 + process.env.TZ = "America/New_York";
36 + const format24 = createMessageTimeFormatter(true);
37 + expect(formatMessageTimestamp(sameDay, nowMs)).toBe(timeIn(sameDay, "America/New_York"));
38 + expect(format24(sameDay, nowMs)).toBe("23:45");
39 + process.env.TZ = "Asia/Bangkok";
40 + vi.setSystemTime(Date.now() + 1_000);
41 + expect(formatMessageTimestamp(sameDay, nowMs)).toBe(timeIn(sameDay, "Asia/Bangkok"));
42 + expect(format24(sameDay, nowMs)).toBe("11:45");
43 + expect(formatMessageTimestamp(earlierInWeek, nowMs)).toBe(
44 + `${dayIn(earlierInWeek, "Asia/Bangkok")} ${timeIn(earlierInWeek, "Asia/Bangkok")}`,
45 + );
46 + } finally {
47 + vi.useRealTimers();
48 + if (previous === undefined) delete process.env.TZ;
49 + else process.env.TZ = previous;
50 + }
51 + });
52 });
53
54 describe("createMessageTimeFormatter", () => {
apps/mobile/src/features/threads/messageTimestamp.ts
+52 -21
@@ -1,34 +1,65 @@
1 -const TIME_FORMATTER = new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit" });
2 -const DATE_FORMATTER = new Intl.DateTimeFormat(undefined, { month: "numeric", day: "numeric" });
3 -const DATE_WITH_YEAR_FORMATTER = new Intl.DateTimeFormat(undefined, {
4 - month: "numeric",
5 - day: "numeric",
6 - year: "numeric",
7 -});
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(),
13 - timeFormatter: Intl.DateTimeFormat = TIME_FORMATTER,
35 + timeFormatter?: Intl.DateTimeFormat,
36 ): string {
37 const timestamp = Date.parse(input);
38 if (Number.isNaN(timestamp)) return "";
17 - const date = new Date(timestamp);
18 - const now = new Date(nowMs);
19 - const time = timeFormatter.format(date);
20 - if (date.toDateString() === now.toDateString()) return time;
21 - const sameYear = date.getFullYear() === now.getFullYear();
22 - return `${(sameYear ? DATE_FORMATTER : DATE_WITH_YEAR_FORMATTER).format(date)} ${time}`;
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) {
27 - const timeFormatter = new Intl.DateTimeFormat(locale, {
28 - hour: "numeric",
29 - minute: "2-digit",
30 - ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }),
31 - });
32 - return (input: string, nowMs?: number): string =>
33 - formatMessageTimestamp(input, nowMs, timeFormatter);
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 }
apps/web/src/timestampFormat.test.ts
+68 -1
@@ -1,6 +1,22 @@
1 import { describe, expect, it } from "vite-plus/test";
2
3 -import { formatDayAwareTimestamp, formatUpcomingTimestamp } from "./timestampFormat";
3 +import {
4 + formatChatTimestampTooltip,
5 + formatDayAwareTimestamp,
6 + formatUpcomingTimestamp,
7 +} from "./timestampFormat";
8 +
9 +// Node re-resolves the default zone when TZ is assigned, like a renderer after the OS zone changes.
10 +function useTimeZone(timeZone: string): Disposable {
11 + const previous = process.env.TZ;
12 + process.env.TZ = timeZone;
13 + return {
14 + [Symbol.dispose]: () => {
15 + if (previous === undefined) delete process.env.TZ;
16 + else process.env.TZ = previous;
17 + },
18 + };
19 +}
20
21 describe("timestamps in an explicit time zone", () => {
22 // 23:00 UTC on July 1 is 7 PM on July 1 in New York, whatever the host zone.
@@ -19,3 +35,54 @@ describe("timestamps in an explicit time zone", () => {
35 );
36 });
37 });
38 +
39 +describe("timestamps after the system time zone changes", () => {
40 + // Built fresh with an explicit zone, so expectations follow the host locale but not a stale zone.
41 + const timeIn = (iso: string, timeZone: string) =>
42 + new Intl.DateTimeFormat(undefined, {
43 + hour: "numeric",
44 + minute: "2-digit",
45 + hour12: true,
46 + timeZone,
47 + }).format(new Date(iso));
48 + const dayIn = (iso: string, timeZone: string) =>
49 + new Intl.DateTimeFormat(undefined, { month: "numeric", day: "numeric", timeZone }).format(
50 + new Date(iso),
51 + );
52 +
53 + it("renders the gutter label in the new zone", () => {
54 + const sameDay = "2026-11-07T04:45:43.877Z";
55 + const earlierInWeek = "2026-11-04T20:00:00.000Z";
56 + const now = Date.parse(sameDay);
57 + using _zone = useTimeZone("America/New_York");
58 + expect([
59 + formatDayAwareTimestamp(sameDay, "12-hour", now),
60 + formatDayAwareTimestamp(earlierInWeek, "12-hour", now),
61 + ]).toEqual([
62 + timeIn(sameDay, "America/New_York"),
63 + `${dayIn(earlierInWeek, "America/New_York")} ${timeIn(earlierInWeek, "America/New_York")}`,
64 + ]);
65 +
66 + process.env.TZ = "Asia/Bangkok";
67 + expect([
68 + formatDayAwareTimestamp(sameDay, "12-hour", now),
69 + formatDayAwareTimestamp(earlierInWeek, "12-hour", now),
70 + ]).toEqual([
71 + timeIn(sameDay, "Asia/Bangkok"),
72 + `${dayIn(earlierInWeek, "Asia/Bangkok")} ${timeIn(earlierInWeek, "Asia/Bangkok")}`,
73 + ]);
74 + });
75 +
76 + it("renders the tooltip in the new zone", () => {
77 + const monthEnd = "2026-10-31T20:00:00.000Z";
78 + using _zone = useTimeZone("America/New_York");
79 + expect(formatChatTimestampTooltip(monthEnd, "12-hour")).toBe(
80 + `${timeIn(monthEnd, "America/New_York")}, 31st October 2026`,
81 + );
82 +
83 + process.env.TZ = "Asia/Bangkok";
84 + expect(formatChatTimestampTooltip(monthEnd, "12-hour")).toBe(
85 + `${timeIn(monthEnd, "Asia/Bangkok")}, 1st November 2026`,
86 + );
87 + });
88 +});
apps/web/src/timestampFormat.ts
+33 -16
@@ -68,14 +68,20 @@ function resolveWeekStartsOn(locale: string | undefined): WeekdayIndex | undefin
68
69 export const weekStartsOn = resolveWeekStartsOn(timestampLocale);
70
71 +// A formatter keeps the default zone it was built in, so default-zone formatters run in UTC
72 +// on the wall-clock time, which Date reads from the current zone.
73 +function wallClockAsUtc(date: Date): Date {
74 + return new Date(date.getTime() - date.getTimezoneOffset() * 60_000);
75 +}
76 +
77 const timestampFormatterCache = new Map<string, Intl.DateTimeFormat>();
78
79 function getTimestampFormatter(
80 timestampFormat: TimestampFormat,
81 includeSeconds: boolean,
76 - timeZone?: string,
82 + timeZone = "UTC",
83 ): Intl.DateTimeFormat {
78 - const cacheKey = `${timestampFormat}:${includeSeconds ? "seconds" : "minutes"}:${timeZone ?? ""}`;
84 + const cacheKey = `${timestampFormat}:${includeSeconds ? "seconds" : "minutes"}:${timeZone}`;
85 const cachedFormatter = timestampFormatterCache.get(cacheKey);
86 if (cachedFormatter) {
87 return cachedFormatter;
@@ -83,12 +89,18 @@ function getTimestampFormatter(
89
90 const formatter = new Intl.DateTimeFormat(timestampLocale, {
91 ...getTimestampFormatOptions(timestampFormat, includeSeconds),
86 - ...(timeZone ? { timeZone } : {}),
92 + timeZone,
93 });
94 timestampFormatterCache.set(cacheKey, formatter);
95 return formatter;
96 }
97
98 +function formatTime(date: Date, timestampFormat: TimestampFormat, timeZone?: string): string {
99 + return timeZone
100 + ? getTimestampFormatter(timestampFormat, false, timeZone).format(date)
101 + : getTimestampFormatter(timestampFormat, false).format(wallClockAsUtc(date));
102 +}
103 +
104 export function resolveHour12(timestampFormat: TimestampFormat): boolean {
105 return getTimestampFormatter(timestampFormat, false).resolvedOptions().hour12 ?? false;
106 }
@@ -101,7 +113,7 @@ export function parseTimestampDate(isoDate: string): Date | null {
113 // Deliberately not the host locale: the tooltip's ordinal suffix and
114 // day-before-month order below are English, so a localized month alone would
115 // read "4th Juni 2026".
104 -const monthNameFormatter = new Intl.DateTimeFormat("en-US", { month: "long" });
116 +const monthNameFormatter = new Intl.DateTimeFormat("en-US", { month: "long", timeZone: "UTC" });
117
118 function ordinalSuffix(day: number): string {
119 const lastTwo = day % 100;
@@ -124,9 +136,9 @@ export function formatChatTimestampTooltip(
136 ): string {
137 const date = parseTimestampDate(isoDate);
138 if (!date) return "";
127 - const time = formatShortTimestamp(isoDate, timestampFormat);
139 + const time = formatTime(date, timestampFormat);
140 const day = date.getDate();
129 - const month = monthNameFormatter.format(date);
141 + const month = monthNameFormatter.format(wallClockAsUtc(date));
142 const year = date.getFullYear();
143 return `${time}, ${day}${ordinalSuffix(day)} ${month} ${year}`;
144 }
@@ -141,7 +153,7 @@ export function formatFullTimestamp(isoDate: string, timestampFormat: TimestampF
153 export function formatShortTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {
154 const date = parseTimestampDate(isoDate);
155 if (!date) return "";
144 - return getTimestampFormatter(timestampFormat, false).format(date);
156 + return formatTime(date, timestampFormat);
157 }
158
159 const numericDateOptions: Intl.DateTimeFormatOptions = { month: "numeric", day: "numeric" };
@@ -149,11 +161,14 @@ const numericDateWithYearOptions: Intl.DateTimeFormatOptions = {
161 ...numericDateOptions,
162 year: "numeric",
163 };
152 -const numericDateFormatter = new Intl.DateTimeFormat(timestampLocale, numericDateOptions);
153 -const numericDateWithYearFormatter = new Intl.DateTimeFormat(
154 - timestampLocale,
155 - numericDateWithYearOptions,
156 -);
164 +const numericDateFormatter = new Intl.DateTimeFormat(timestampLocale, {
165 + ...numericDateOptions,
166 + timeZone: "UTC",
167 +});
168 +const numericDateWithYearFormatter = new Intl.DateTimeFormat(timestampLocale, {
169 + ...numericDateWithYearOptions,
170 + timeZone: "UTC",
171 +});
172
173 interface CalendarDay {
174 year: number;
@@ -180,7 +195,9 @@ function calendarDay(date: Date, timeZone: string | undefined): CalendarDay {
195
196 function formatNumericDate(date: Date, sameYear: boolean, timeZone: string | undefined): string {
197 if (!timeZone) {
183 - return (sameYear ? numericDateFormatter : numericDateWithYearFormatter).format(date);
198 + return (sameYear ? numericDateFormatter : numericDateWithYearFormatter).format(
199 + wallClockAsUtc(date),
200 + );
201 }
202 return new Intl.DateTimeFormat(timestampLocale, {
203 ...(sameYear ? numericDateOptions : numericDateWithYearOptions),
@@ -196,7 +213,7 @@ export function formatDayAwareTimestamp(
213 ): string {
214 const date = parseTimestampDate(isoDate);
215 if (!date) return "";
199 - const time = getTimestampFormatter(timestampFormat, false, timeZone).format(date);
216 + const time = formatTime(date, timestampFormat, timeZone);
217
218 const today = calendarDay(new Date(nowMs), timeZone);
219 const messageDay = calendarDay(date, timeZone);
@@ -207,7 +224,7 @@ export function formatDayAwareTimestamp(
224 return `${formatNumericDate(date, messageDay.year === today.year, timeZone)} ${time}`;
225 }
226
210 -/** `timeZone` overrides the zone the formatters cached when they were built. */
227 +/** `timeZone` overrides the system's current zone. */
228 export function formatUpcomingTimestamp(
229 isoDate: string,
230 timestampFormat: TimestampFormat,
@@ -216,7 +233,7 @@ export function formatUpcomingTimestamp(
233 ): string {
234 const date = parseTimestampDate(isoDate);
235 if (!date) return "";
219 - const time = getTimestampFormatter(timestampFormat, false, timeZone).format(date);
236 + const time = formatTime(date, timestampFormat, timeZone);
237
238 const today = calendarDay(new Date(nowMs), timeZone);
239 const targetDay = calendarDay(date, timeZone);