@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
25 lines (23 loc) • 868 B
JavaScript
import { isValidTimeZone } from "../validate/isValidTimeZone.js";
// src/zoned/get/getSystemTimeZone.ts
/**
* Return the runtime system timeZone name (for example "Europe/London").
*
* - Uses Intl.DateTimeFormat().resolvedOptions().timeZone to get system timezone.
* - Returns "" when the host reports no usable time zone. With an empty `TZ` the runtime reports
* ICU's placeholder `"Etc/Unknown"`, and with an unrecognised `TZ` it reports `undefined`;
* neither is a zone that Temporal or `Intl.DateTimeFormat` accepts.
*
* @returns system timeZone name or "" on failure
*
* @example getSystemTimeZone() // "Europe/London"
*/
function getSystemTimeZone() {
try {
const systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return isValidTimeZone(systemTimeZone) ? systemTimeZone : "";
} catch {
return "";
}
}
export { getSystemTimeZone };