* Copyright (C) 2025 Red Hat, Inc.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
export const is24HourLocale = (locale) => {
try {
const format = new Intl.DateTimeFormat(locale, { hour: "numeric" });
const opts = format.resolvedOptions();
return opts.hourCycle === "h23" || opts.hourCycle === "h24" || opts.hour12 === false;
} catch {
return true;
}
};
* Returns local time as string ("HH:mm" or "hh:mm a") for displaying in picker.
* @param {DateTime} dateTime - Luxon DateTime (from backend/state)
* @param {boolean} is24Hour - Should display as 24h (true) or 12h (false)
* @returns {string}
*/
export const getLocalTimeForPicker = (dateTime, is24Hour = true) => {
if (!dateTime || !dateTime.isValid) return "";
return dateTime.toFormat(is24Hour ? "HH:mm" : "hh:mm a");
};
export const isValidDate = (val) =>
val instanceof Date && !isNaN(val.getTime());
export const formatDateInput = (dt) => {
if (typeof dt?.toISO !== "function" || !dt?.isValid) return "";
return dt.toISODate();
};
export const getUserLocale = () =>
navigator.languages?.length
? navigator.languages[0]
: navigator.language || "en-US";
* True when **timezoneId** is a non-empty Region/City id present in the module catalog.
* @param {string} [timezoneId]
* @param {object} [allValidTimezones] - Object of form { region: [city1, city2, ...], ... }
*/
export const isValidTimezone = (timezoneId, allValidTimezones) => {
if (!timezoneId) {
return false;
}
if (!timezoneId.includes("/")) {
return false;
}
const [region, ...cityParts] = timezoneId.split("/");
const city = cityParts.join("/");
const cities = allValidTimezones?.[region];
return Array.isArray(cities) && cities.includes(city);
};