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";
|
|
|
|
|
2022-10-25 00:14:06 +00:00
|
|
|
export enum TimeFormat {
|
|
|
|
TWELVE_HOUR = "h:mma",
|
|
|
|
TWENTY_FOUR_HOUR = "HH:mm",
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:45:28 +00:00
|
|
|
export const setIs24hClockInLocalStorage = (is24h: boolean) =>
|
|
|
|
localStorage.setItem(is24hLocalstorageKey, is24h.toString());
|
|
|
|
|
|
|
|
export const getIs24hClockFromLocalStorage = () => {
|
|
|
|
const is24hFromLocalstorage = localStorage.getItem(is24hLocalstorageKey);
|
2023-03-10 22:10:56 +00:00
|
|
|
|
2022-09-30 12:45:28 +00:00
|
|
|
if (is24hFromLocalstorage === null) return null;
|
|
|
|
|
|
|
|
return is24hFromLocalstorage === "true";
|
|
|
|
};
|
|
|
|
|
2023-07-19 14:30:37 +00:00
|
|
|
export const getTimeFormatStringFromUserTimeFormat = (timeFormat: number | null | undefined) => {
|
|
|
|
return timeFormat === 24 ? TimeFormat.TWENTY_FOUR_HOUR : TimeFormat.TWELVE_HOUR;
|
|
|
|
};
|
|
|
|
|
2023-03-10 22:10:56 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the browsers time format preference, checking local storage first
|
|
|
|
* for a user set preference. If no preference is found, it will use the browser
|
|
|
|
* locale to determine the time format and store it in local storage.
|
|
|
|
*/
|
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-11-14 19:46:58 +00:00
|
|
|
// Intl.DateTimeFormat with value=undefined uses local browser settings.
|
|
|
|
if (!!new Intl.DateTimeFormat(undefined, { hour: "numeric" }).format(0).match(/M/i)) {
|
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
|
|
|
|
2023-03-10 22:10:56 +00:00
|
|
|
/**
|
|
|
|
* Returns the time format string based on whether the current set locale is 24h or 12h.
|
|
|
|
*/
|
2022-10-25 00:14:06 +00:00
|
|
|
export const detectBrowserTimeFormat = isBrowserLocale24h()
|
|
|
|
? TimeFormat.TWENTY_FOUR_HOUR
|
|
|
|
: TimeFormat.TWELVE_HOUR;
|