@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
23 lines (22 loc) • 947 B
JavaScript
import { getSystemTimeZone } from "../zoned/get/getSystemTimeZone.js";
import { isValidTimeZone } from "../zoned/validate/index.js";
/**
* Resolve a timezone string for the Unix/UTC formatters.
*
* - `"local"` → the system timezone via `getSystemTimeZone()`.
* - A valid IANA timezone identifier → returned as-is.
* - Anything else (undefined, empty, invalid name, typo) → `"UTC"`.
*
* The UTC fallback is intentional: `formatUnix` / `formatUtc` / the relative
* formatters expose `timeZone` as an *optional* convenience, so we degrade
* gracefully when callers pass nothing or pass a typo. Callers that need
* strict timezone validation should use `isValidTimeZone()` themselves
* before calling these formatters.
*/
export function normalizeTimeZone(tz) {
if (tz === "local")
return getSystemTimeZone();
if (typeof tz === "string" && tz.length > 0 && isValidTimeZone(tz))
return tz;
return "UTC";
}