Skip to content

Commit 287e319

Browse files
authored
fix: use navigator.locale to evaluate time format (coder#17025)
Fixes: coder#15452
1 parent 3bd32a2 commit 287e319

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

site/src/pages/UserSettingsPage/SchedulePage/ScheduleForm.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const ScheduleForm: FC<ScheduleFormProps> = ({
7979
},
8080
});
8181
const getFieldHelpers = getFormHelpers<ScheduleFormValues>(form, submitError);
82+
const browserLocale = navigator.language || "en-US";
8283

8384
return (
8485
<Form onSubmit={form.handleSubmit}>
@@ -127,7 +128,12 @@ export const ScheduleForm: FC<ScheduleFormProps> = ({
127128
disabled
128129
fullWidth
129130
label="Next occurrence"
130-
value={quietHoursDisplay(form.values.time, form.values.timezone, now)}
131+
value={quietHoursDisplay(
132+
browserLocale,
133+
form.values.time,
134+
form.values.timezone,
135+
now,
136+
)}
131137
/>
132138

133139
<div>

site/src/utils/schedule.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ describe("util/schedule", () => {
7878
});
7979

8080
describe("quietHoursDisplay", () => {
81-
it("midnight", () => {
81+
it("midnight in Poland", () => {
8282
const quietHoursStart = quietHoursDisplay(
83+
"pl",
8384
"00:00",
8485
"Australia/Sydney",
8586
new Date("2023-09-06T15:00:00.000+10:00"),
@@ -89,8 +90,9 @@ describe("util/schedule", () => {
8990
"00:00 tomorrow (in 9 hours) in Australia/Sydney",
9091
);
9192
});
92-
it("five o'clock today", () => {
93+
it("five o'clock today in Sweden", () => {
9394
const quietHoursStart = quietHoursDisplay(
95+
"sv",
9496
"17:00",
9597
"Europe/London",
9698
new Date("2023-09-06T15:00:00.000+10:00"),
@@ -100,15 +102,28 @@ describe("util/schedule", () => {
100102
"17:00 today (in 11 hours) in Europe/London",
101103
);
102104
});
103-
it("lunch tomorrow", () => {
105+
it("five o'clock today in Finland", () => {
104106
const quietHoursStart = quietHoursDisplay(
107+
"fl",
108+
"17:00",
109+
"Europe/London",
110+
new Date("2023-09-06T15:00:00.000+10:00"),
111+
);
112+
113+
expect(quietHoursStart).toBe(
114+
"5:00 PM today (in 11 hours) in Europe/London",
115+
);
116+
});
117+
it("lunch tomorrow in England", () => {
118+
const quietHoursStart = quietHoursDisplay(
119+
"en",
105120
"13:00",
106121
"US/Central",
107122
new Date("2023-09-06T08:00:00.000+10:00"),
108123
);
109124

110125
expect(quietHoursStart).toBe(
111-
"13:00 tomorrow (in 20 hours) in US/Central",
126+
"1:00 PM tomorrow (in 20 hours) in US/Central",
112127
);
113128
});
114129
});

site/src/utils/schedule.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export const timeToCron = (time: string, tz?: string) => {
256256
};
257257

258258
export const quietHoursDisplay = (
259+
browserLocale: string,
259260
time: string,
260261
tz: string,
261262
now: Date | undefined,
@@ -276,7 +277,14 @@ export const quietHoursDisplay = (
276277

277278
const today = dayjs(now).tz(tz);
278279
const day = dayjs(parsed.next().toDate()).tz(tz);
279-
let display = day.format("HH:mm");
280+
281+
const formattedTime = new Intl.DateTimeFormat(browserLocale, {
282+
hour: "numeric",
283+
minute: "numeric",
284+
timeZone: tz,
285+
}).format(day.toDate());
286+
287+
let display = formattedTime;
280288

281289
if (day.isSame(today, "day")) {
282290
display += " today";

0 commit comments

Comments
 (0)