Skip to content

Commit 2d297fc

Browse files
committed
fix(mobile): honor Android clock format in message timestamps
1 parent 6417151 commit 2d297fc

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Button } from "../../components/ui/Button";
2+
import { useMessageTimeFormatter } from "./useMessageTimeFormatter";
23
import * as Dialog from "../../components/ui/Dialog";
34
import { useOpenPullRequest } from "../../native/useOpenPullRequest";
45
import { useWorkspaceSettledContentWidth } from "../layout/workspace-content-width";
@@ -294,18 +295,6 @@ const WIDE_MARKDOWN_BLOCK_OPTIONS = {
294295
includeOrderedLists: Platform.OS === "android",
295296
} as const;
296297

297-
const MESSAGE_TIME_FORMATTER = new Intl.DateTimeFormat(undefined, {
298-
hour: "numeric",
299-
minute: "2-digit",
300-
});
301-
function formatMessageTime(input: string): string {
302-
const timestamp = Date.parse(input);
303-
if (Number.isNaN(timestamp)) {
304-
return "";
305-
}
306-
return MESSAGE_TIME_FORMATTER.format(timestamp);
307-
}
308-
309298
const TURN_FOLD_HEIGHT = 42;
310299
const FEED_PADDING_TOP = 12;
311300
// Rows re-measure within a commit or two of a width change. LegendList also defers imperative
@@ -1660,6 +1649,7 @@ function renderFeedEntry(
16601649
| "threadId"
16611650
| "workspaceRoot"
16621651
> & {
1652+
readonly formatMessageTime: (input: string) => string;
16631653
readonly copiedRowId: string | null;
16641654
readonly expandedWorkRows: Record<string, boolean>;
16651655
readonly workRowSizing: ReturnType<typeof deriveThreadWorkLogSizing>;
@@ -1873,7 +1863,7 @@ function renderFeedEntry(
18731863
const presentation = resolveUserMessagePresentation(message);
18741864
const renderedText = renderAssistantCitationsAsText(presentation.text);
18751865
const styles = isUser ? markdownStyles.user : markdownStyles.assistant;
1876-
const timestampLabel = formatMessageTime(isUser ? message.createdAt : message.updatedAt);
1866+
const timestampLabel = props.formatMessageTime(isUser ? message.createdAt : message.updatedAt);
18771867
const attachments = message.attachments ?? [];
18781868
const hasReviewCommentContext = presentation.text.includes("<review_comment");
18791869
// Android does not reposition children after maxWidth clamps bubbles; definite width prevents wide-block overlap.
@@ -2658,6 +2648,7 @@ export const ThreadFeed = memo(function ThreadFeed({
26582648
ref,
26592649
...props
26602650
}: ThreadFeedProps & { readonly ref?: Ref<ThreadFeedHandle> }) {
2651+
const formatMessageTime = useMessageTimeFormatter();
26612652
const { listRef, onEndFollowEnabledChange, onHeaderMaterialVisibilityChange } = props;
26622653
const workflowOpening = useWorkflowOpening(props.detailRef);
26632654
const guestShare = useThreadGuestShare(props.detailRef);
@@ -3218,6 +3209,7 @@ export const ThreadFeed = memo(function ThreadFeed({
32183209
const listAppearanceData = {
32193210
disclosureLayout,
32203211
reduceMotion,
3212+
formatMessageTime,
32213213
worktreeSetup: props.worktreeSetup,
32223214
setupWorkingStartedAt: props.setupWorkingStartedAt,
32233215
dispatchingMessageId: props.dispatchingMessageId,
@@ -3755,6 +3747,7 @@ export const ThreadFeed = memo(function ThreadFeed({
37553747
) : null}
37563748
{renderFeedEntry(info, {
37573749
userMessageGrouping: info.item.userMessageGrouping,
3750+
formatMessageTime,
37583751
environmentId: props.environmentId,
37593752
dispatchingMessageId: props.dispatchingMessageId,
37603753
onEditPendingMessage: props.onEditPendingMessage,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, it } from "vite-plus/test";
2+
import { createMessageTimeFormatter } from "./messageTime";
3+
4+
it("honors 24-hour time even for a 12-hour locale, including midnight", () => {
5+
const format = createMessageTimeFormatter(true, "en-US");
6+
expect(format(new Date(2026, 0, 1, 15, 7).toISOString())).toBe("15:07");
7+
expect(format(new Date(2026, 0, 1, 0, 7).toISOString())).toBe("00:07");
8+
});
9+
10+
it("honors 12-hour time even for a 24-hour locale", () => {
11+
const format = createMessageTimeFormatter(false, "en-GB");
12+
expect(format(new Date(2026, 0, 1, 15, 7).toISOString())).toMatch(/^3:07 pm$/i);
13+
});
14+
15+
it("uses the locale default without an override and hides invalid timestamps", () => {
16+
const format = createMessageTimeFormatter(undefined, "en-GB");
17+
const timestamp = new Date(2026, 0, 1, 15, 7);
18+
const expected = new Intl.DateTimeFormat("en-GB", {
19+
hour: "numeric",
20+
minute: "2-digit",
21+
}).format(timestamp);
22+
expect(format(timestamp.toISOString())).toBe(expected);
23+
expect(format("invalid")).toBe("");
24+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function createMessageTimeFormatter(is24Hour: boolean | undefined, locale?: string) {
2+
const formatter = new Intl.DateTimeFormat(locale, {
3+
hour: "numeric",
4+
minute: "2-digit",
5+
...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }),
6+
});
7+
return (input: string): string => {
8+
const timestamp = Date.parse(input);
9+
return Number.isNaN(timestamp) ? "" : formatter.format(timestamp);
10+
};
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { requireNativeModule } from "expo";
2+
import { useEffect, useMemo, useState } from "react";
3+
import { AppState, Platform } from "react-native";
4+
import { createMessageTimeFormatter } from "./messageTime";
5+
6+
function systemUses24HourClock() {
7+
if (Platform.OS !== "android") return undefined;
8+
return requireNativeModule<{ is24HourFormat(): boolean }>("T3NativeControls").is24HourFormat();
9+
}
10+
11+
export function useMessageTimeFormatter() {
12+
const [is24Hour, setIs24Hour] = useState(systemUses24HourClock);
13+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Refresh the native clock preference when Android returns to the foreground.
14+
useEffect(() => {
15+
if (Platform.OS !== "android") return;
16+
// Android's clock preference can change while the app is in the background.
17+
const subscription = AppState.addEventListener("change", (state) => {
18+
if (state === "active") setIs24Hour(systemUses24HourClock());
19+
});
20+
return () => subscription.remove();
21+
}, []);
22+
return useMemo(() => createMessageTimeFormatter(is24Hour), [is24Hour]);
23+
}

0 commit comments

Comments
 (0)