2022-02-23 12:37:15 +00:00
|
|
|
/*
|
|
|
|
* Detects navigator locale 24h time preference
|
|
|
|
* It works by checking whether hour output contains AM ('1 AM' or '01 h')
|
|
|
|
* based on the user's preferred language
|
|
|
|
* defaults to 'en-US' (12h) if no navigator language is found
|
|
|
|
*/
|
2022-07-26 18:02:44 +00:00
|
|
|
import { localStorage } from "@calcom/lib/webstorage";
|
|
|
|
|
2022-09-30 12:45:28 +00:00
|
|
|
const is24hLocalstorageKey = "timeOption.is24hClock";
|
|
|
|
|
|
|
|
export const setIs24hClockInLocalStorage = (is24h: boolean) =>
|
|
|
|
localStorage.setItem(is24hLocalstorageKey, is24h.toString());
|
|
|
|
|
|
|
|
export const getIs24hClockFromLocalStorage = () => {
|
|
|
|
const is24hFromLocalstorage = localStorage.getItem(is24hLocalstorageKey);
|
|
|
|
if (is24hFromLocalstorage === null) return null;
|
|
|
|
|
|
|
|
return is24hFromLocalstorage === "true";
|
|
|
|
};
|
|
|
|
|
2022-02-23 12:37:15 +00:00
|
|
|
export const isBrowserLocale24h = () => {
|
2022-09-30 12:45:28 +00:00
|
|
|
const localStorageTimeFormat = getIs24hClockFromLocalStorage();
|
2022-07-26 18:02:44 +00:00
|
|
|
// If time format is already stored in the browser then retrieve and return early
|
2022-09-30 12:45:28 +00:00
|
|
|
if (localStorageTimeFormat === true) {
|
2022-07-26 18:02:44 +00:00
|
|
|
return true;
|
2022-09-30 12:45:28 +00:00
|
|
|
} else if (localStorageTimeFormat === false) {
|
2022-07-26 18:02:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:37:15 +00:00
|
|
|
let locale = "en-US";
|
2022-07-26 18:02:44 +00:00
|
|
|
if (typeof window !== "undefined" && navigator) locale = window.navigator?.language;
|
|
|
|
|
|
|
|
if (!new Intl.DateTimeFormat(locale, { hour: "numeric" }).format(0).match(/M/)) {
|
2022-09-30 12:45:28 +00:00
|
|
|
setIs24hClockInLocalStorage(false);
|
2022-07-26 18:02:44 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2022-09-30 12:45:28 +00:00
|
|
|
setIs24hClockInLocalStorage(true);
|
2022-07-26 18:02:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-23 12:37:15 +00:00
|
|
|
};
|
2022-07-26 18:02:44 +00:00
|
|
|
|
2022-02-23 12:37:15 +00:00
|
|
|
export const detectBrowserTimeFormat = isBrowserLocale24h() ? "H:mm" : "h:mma";
|