Skip to content

fix: use navigator.locale to evaluate time format #17025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const ScheduleForm: FC<ScheduleFormProps> = ({
},
});
const getFieldHelpers = getFormHelpers<ScheduleFormValues>(form, submitError);
const browserLocale = navigator.language || "en-US";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disappointed we have to use language as an approximation 😔. I prefer to use English US for text and UI language but Finland is used for numbers, time and currency. Yet this is not available to the browser (AFAICT).


return (
<Form onSubmit={form.handleSubmit}>
Expand Down Expand Up @@ -127,7 +128,12 @@ export const ScheduleForm: FC<ScheduleFormProps> = ({
disabled
fullWidth
label="Next occurrence"
value={quietHoursDisplay(form.values.time, form.values.timezone, now)}
value={quietHoursDisplay(
browserLocale,
form.values.time,
form.values.timezone,
now,
)}
/>

<div>
Expand Down
23 changes: 19 additions & 4 deletions site/src/utils/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ describe("util/schedule", () => {
});

describe("quietHoursDisplay", () => {
it("midnight", () => {
it("midnight in Poland", () => {
const quietHoursStart = quietHoursDisplay(
"pl",
"00:00",
"Australia/Sydney",
new Date("2023-09-06T15:00:00.000+10:00"),
Expand All @@ -89,8 +90,9 @@ describe("util/schedule", () => {
"00:00 tomorrow (in 9 hours) in Australia/Sydney",
);
});
it("five o'clock today", () => {
it("five o'clock today in Sweden", () => {
const quietHoursStart = quietHoursDisplay(
"sv",
"17:00",
"Europe/London",
new Date("2023-09-06T15:00:00.000+10:00"),
Expand All @@ -100,15 +102,28 @@ describe("util/schedule", () => {
"17:00 today (in 11 hours) in Europe/London",
);
});
it("lunch tomorrow", () => {
it("five o'clock today in Finland", () => {
const quietHoursStart = quietHoursDisplay(
"fl",
"17:00",
"Europe/London",
new Date("2023-09-06T15:00:00.000+10:00"),
);

expect(quietHoursStart).toBe(
"5:00 PM today (in 11 hours) in Europe/London",
);
});
it("lunch tomorrow in England", () => {
const quietHoursStart = quietHoursDisplay(
"en",
"13:00",
"US/Central",
new Date("2023-09-06T08:00:00.000+10:00"),
);

expect(quietHoursStart).toBe(
"13:00 tomorrow (in 20 hours) in US/Central",
"1:00 PM tomorrow (in 20 hours) in US/Central",
);
});
});
Expand Down
10 changes: 9 additions & 1 deletion site/src/utils/schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export const timeToCron = (time: string, tz?: string) => {
};

export const quietHoursDisplay = (
browserLocale: string,
time: string,
tz: string,
now: Date | undefined,
Expand All @@ -276,7 +277,14 @@ export const quietHoursDisplay = (

const today = dayjs(now).tz(tz);
const day = dayjs(parsed.next().toDate()).tz(tz);
let display = day.format("HH:mm");

const formattedTime = new Intl.DateTimeFormat(browserLocale, {
hour: "numeric",
minute: "numeric",
timeZone: tz,
}).format(day.toDate());

let display = formattedTime;

if (day.isSame(today, "day")) {
display += " today";
Expand Down
Loading