@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.
65 lines (63 loc) • 3.56 kB
JavaScript
import { stringOptionOrDefault } from "../../internal/typedOptions.js";
import { isZoneId } from "../../internal/inputChecks.js";
import { resolveRequiredLocale } from "../../internal/resolveLocale.js";
import { nowInstant } from "../../internal/temporalCompat/clock.js";
import { cachedFormatter } from "../../internal/formatterCache.js";
import { isOptionsArgument } from "../../internal/isObject.js";
// src/zoned/format/formatTimeZoneName.ts
/**
* Return the localized display name for an IANA timeZone.
*
* - `options.style` covers every `Intl.DateTimeFormatOptions` `timeZoneName`
* value: "short" (AEST), "long" (Australian Eastern Standard Time),
* "shortOffset" (GMT+10), "longOffset" (GMT+10:00), "shortGeneric" (AET),
* "longGeneric" (Australian Eastern Time).
* - "short"/"long"/"shortOffset"/"longOffset" name the zone's *current*
* offset — for a DST-observing zone the label flips between standard and
* daylight names depending on when this is called, since there's no
* instant parameter to pin it to (this matches how
* `Intl.DateTimeFormat.prototype.format()` itself defaults to "now" when
* called with no argument). "shortGeneric"/"longGeneric" are
* season-independent (e.g. "AET", "Australian Eastern Time") and don't have this
* issue — prefer them for a name that won't change twice a year.
* - Output depends on runtime ICU data.
* - Returns "" for an invalid timeZone or locale.
* - An offset with seconds (`-00:44:30`) returns `""`. `Intl.DateTimeFormat` takes a time zone
* identifier only: constructing one with an offset that has seconds throws (ECMA-402, the
* `DateTimeFormat` construction step "If parseResult contains more than one MinuteSecond Parse
* Node, throw a RangeError exception"). Pass the IANA name.
*
* @param timeZone IANA name or a UTC offset to the minute (what `isValidTimeZone` accepts)
* @param locale BCP 47 locale tag (e.g. "en-AU", "ja-JP"), or a preference list of tags (ECMA-402; the first with locale data is read). Required: omitted, or an empty list (which ECMA-402 would resolve to the host default), returns ""
* @param options optional setting for the form of the name
* @returns localized zone name, or "" on invalid input
*
* @example formatTimeZoneName("Australia/Sydney", "en-AU", { style: "shortGeneric" }) // "AET"
* @example formatTimeZoneName("Australia/Sydney", "en-AU", { style: "longGeneric" }) // "Australian Eastern Time"
* @example formatTimeZoneName("Australia/Sydney", "en-AU", { style: "shortOffset" }) // "GMT+10" or "GMT+11", depending on the current date
* @example formatTimeZoneName("Asia/Tokyo", "ja-JP", { style: "longGeneric" }) // "日本標準時"
* @example formatTimeZoneName("Invalid/Zone", "en-GB") // ""
* @example formatTimeZoneName("Australia/Sydney", "!!!") // ""
* @example formatTimeZoneName("Europe/Berlin", ["fr-FR", "en-US"], { style: "longGeneric" }) // "heure d’Europe centrale"
*/
function formatTimeZoneName(timeZone, locale, options) {
try {
if (!isOptionsArgument(options)) return "";
if (!isZoneId(timeZone)) return "";
const resolvedLocale = resolveRequiredLocale(locale);
if (resolvedLocale === null) return "";
try {
const style = stringOptionOrDefault(options?.style, "long");
return cachedFormatter("DateTimeFormat", resolvedLocale, {
timeZone,
timeZoneName: style,
hour: "numeric"
}).formatToParts(nowInstant().epochMilliseconds).find((part) => part.type === "timeZoneName")?.value ?? "";
} catch {
return "";
}
} catch {
return "";
}
}
export { formatTimeZoneName };