UNPKG

@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.

68 lines (66 loc) • 2.78 kB
import { Temporal } from "../temporal.js"; import { isTimeZoneEqualsCompatNeeded } from "./capabilities.js"; // src/internal/temporalCompat/timeZoneEquals.ts /** The `Temporal` the host provides on `globalThis`, if any. */ function hostTemporal() { return globalThis.Temporal; } /** * TimeZoneEquals for two identifiers as the host's own `Temporal` answers it, or `undefined` when * the host has no `Temporal.ZonedDateTime`, it throws for either identifier, or its `equals` * returns something other than a boolean. * * @param host the host's `Temporal` namespace, or `undefined` when it has none * @param one a time zone identifier * @param two a time zone identifier * @returns the host's answer, or `undefined` when it cannot be had */ function timeZoneEqualsByHost(host, one, two) { const ZonedDateTime = host?.ZonedDateTime; if (typeof ZonedDateTime !== "function") return void 0; try { const answer = new ZonedDateTime(0n, one).equals(new ZonedDateTime(0n, two)); return typeof answer === "boolean" ? answer : void 0; } catch { return; } } /** * The identifiers ECMA-402 §6.5 resolves to `"UTC"` by name, with no time zone data: the last * step of AvailableNamedTimeZoneIdentifiers. The polyfill hands them back in this casing. */ const RESOLVE_TO_UTC = /* @__PURE__ */ new Set([ "Etc/UTC", "Etc/GMT", "GMT" ]); /** The identifier, or `"UTC"` when ECMA-402 §6.5 names it as one that resolves to `"UTC"`. */ function utcByName(id) { return RESOLVE_TO_UTC.has(id) ? "UTC" : id; } /** * Whether two zoned values are in the same time zone by TC39 TimeZoneEquals: the same * identifier, or two identifiers with one primary identifier (`UTC` and `Etc/UTC`, * `Asia/Kolkata` and `Asia/Calcutta`). * * The polyfill's `ZonedDateTime#equals` answers it, except where probe D19 fails: there the * polyfill cannot find a non-primary identifier's primary. GMT then resolves the names ECMA-402 * §6.5 sends to `"UTC"` itself, which needs no time zone data, and asks the host's own `Temporal` * for any other pair. With no host `Temporal` (Safari 27) the polyfill's answer stands for the * IANA links such as `Asia/Calcutta`: no Intl API there names their primary identifier * (temporalCompat README, D19). * * @param one a zoned value * @param two a zoned value * @returns true when the two time zones are one */ function timeZoneEquals(one, two) { const { timeZoneId: a } = one; const { timeZoneId: b } = two; if (a === b) return true; const byPolyfill = () => new Temporal.ZonedDateTime(0n, a).equals(new Temporal.ZonedDateTime(0n, b)); if (!isTimeZoneEqualsCompatNeeded()) return byPolyfill(); if (utcByName(a) === utcByName(b)) return true; return timeZoneEqualsByHost(hostTemporal(), a, b) ?? byPolyfill(); } export { timeZoneEquals, timeZoneEqualsByHost };