Skip to content

Commit feccdde

Browse files
committed
fix(mobile): message times key the formatter on the live zone
The UTC-offset guard missed a switch between two zones that share the current offset, which then formatted summer timestamps an hour off. Key the mobile formatter on the resolved zone like web does; one resolvedOptions() call per rendered message is the accepted cost. Signed-off-by: Tiger Kaovilai <[email protected]>
1 parent 1083c0d commit feccdde

1 file changed

Lines changed: 9 additions & 16 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -294,29 +294,22 @@ const WIDE_MARKDOWN_BLOCK_OPTIONS = {
294294
includeOrderedLists: Platform.OS === "android",
295295
} as const;
296296

297-
// A formatter pins the default zone when built. Probing the zone builds a formatter too, so
298-
// re-probe only when the UTC offset moves: that catches DST and almost every zone change.
299-
// Missed: a switch between zones with the same current offset; it heals at the next offset change.
297+
// A formatter pins the zone when built, so key it on the live zone. Probing costs one
298+
// resolvedOptions() per rendered message; accepted for correctness across zone switches.
300299
const MESSAGE_TIME_OPTIONS: Intl.DateTimeFormatOptions = { hour: "numeric", minute: "2-digit" };
301-
let messageTimeFormatter = new Intl.DateTimeFormat(undefined, MESSAGE_TIME_OPTIONS);
302-
let messageTimeOffset = new Date().getTimezoneOffset();
300+
const messageTimeFormatters = new Map<string, Intl.DateTimeFormat>();
303301
function formatMessageTime(input: string): string {
304302
const timestamp = Date.parse(input);
305303
if (Number.isNaN(timestamp)) {
306304
return "";
307305
}
308-
const offset = new Date().getTimezoneOffset();
309-
if (offset !== messageTimeOffset) {
310-
messageTimeOffset = offset;
311-
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
312-
if (messageTimeFormatter.resolvedOptions().timeZone !== timeZone) {
313-
messageTimeFormatter = new Intl.DateTimeFormat(undefined, {
314-
...MESSAGE_TIME_OPTIONS,
315-
timeZone,
316-
});
317-
}
306+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
307+
let formatter = messageTimeFormatters.get(timeZone);
308+
if (!formatter) {
309+
formatter = new Intl.DateTimeFormat(undefined, { ...MESSAGE_TIME_OPTIONS, timeZone });
310+
messageTimeFormatters.set(timeZone, formatter);
318311
}
319-
return messageTimeFormatter.format(timestamp);
312+
return formatter.format(timestamp);
320313
}
321314

322315
const TURN_FOLD_HEIGHT = 42;

0 commit comments

Comments
 (0)