Skip to content

Commit dcecbf1

Browse files
Fallback to VITE_DEFAULT_HOUR_FORMAT if the browser doesn't provide a time format. (Fixes #867) (#868)
* Fallback to `VITE_DEFAULT_HOUR_FORMAT` if the browser doesn't provide a time format. (Fixes #867) * Use the browsers language val, and tighten up the error checking.
1 parent 1ee54d5 commit dcecbf1

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

frontend/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ VITE_POSTHOG_HOST=https://us.i.posthog.com
2828
VITE_POSTHOG_UI_HOST=https://us.posthog.com
2929

3030
VITE_REPORT_BUG_URL=https://github.com/thunderbird/appointment/issues/new?assignees=&labels=bug&projects=&template=bug_report.md
31+
32+
# If the browser's hour setting is empty or missing fallback to this value
33+
VITE_DEFAULT_HOUR_FORMAT=12

frontend/.env.prod.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ VITE_POSTHOG_HOST=https://data.appointment.day
1919
VITE_POSTHOG_UI_HOST=https://us.posthog.com
2020

2121
VITE_REPORT_BUG_URL=https://github.com/thunderbird/appointment/issues/new?assignees=&labels=bug&projects=&template=bug_report.md
22+
23+
# If the browser's hour setting is empty or missing fallback to this value
24+
VITE_DEFAULT_HOUR_FORMAT=12

frontend/.env.stage.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ VITE_POSTHOG_HOST=https://data.appointment.day
1919
VITE_POSTHOG_UI_HOST=https://us.posthog.com
2020

2121
VITE_REPORT_BUG_URL=https://github.com/thunderbird/appointment/issues/new?assignees=&labels=bug&projects=&template=bug_report.md
22+
23+
# If the browser's hour setting is empty or missing fallback to this value
24+
VITE_DEFAULT_HOUR_FORMAT=12

frontend/src/utils.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,22 @@ export const download = (data: BlobPart, filename: string, contenttype: string =
8181
// This functions works independent from Pinia stores so that
8282
// it can be called even if stores are not initialized yet.
8383
export const timeFormat = (): string => {
84+
const fallbackFormat = import.meta.env?.VITE_DEFAULT_HOUR_FORMAT ?? 12;
8485
const user = JSON.parse(localStorage?.getItem('tba/user') ?? '{}') as User;
85-
const detected = Intl.DateTimeFormat().resolvedOptions().hour12 ? 12 : 24;
86+
87+
let use12HourTime = null;
88+
try {
89+
use12HourTime = Intl.DateTimeFormat(window.navigator.language, { hour: 'numeric' }).resolvedOptions()?.hour12 ?? null;
90+
} catch (e: RangeError|any) {
91+
// Catch any range error raised by invalid language/locale codes and pass
92+
}
93+
94+
// `.hour12` is an optional value and can be undefined (we cast it as null.) So default to our env value, and if not null use it.
95+
let detected = fallbackFormat;
96+
if (use12HourTime !== null) {
97+
detected = use12HourTime ? 12 : 24;
98+
}
99+
86100
const format = Number(user.settings?.timeFormat ?? detected);
87101
return format === 24 ? 'HH:mm' : 'hh:mm A';
88102
};

0 commit comments

Comments
 (0)