@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.
62 lines (60 loc) • 4.58 kB
JavaScript
import { cachedFormatter } from "../../internal/formatterCache.js";
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
import { normalizeDateTime } from "../../internal/normalizeDateTime.js";
import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js";
import { instantFormatOptions } from "../../internal/instantFormatOptions.js";
// src/zoned/format/formatZonedDateTime.ts
/**
* Format a zoned ISO 8601 datetime string using the Intl.DateTimeFormat implementation.
*
* - Formats as Temporal's ECMA-402 ZonedDateTime format (`ZonedDateTime#toLocaleString`) does,
* through the runtime's `Intl.DateTimeFormat` in the value's own zone: the requested fields and
* widths are kept, and with no date or time field (`era` alone included) the date, time and a
* short zone name are the defaults.
* - Accepts optional locale and Intl.DateTimeFormatOptions.
* - Returns "" for invalid input, and for a `timeZone` option, where
* `ZonedDateTime#toLocaleString` throws a TypeError.
* - Output is normalized: dash separators become ASCII "-" (unspaced between digits, spaced
* otherwise), and no-break, narrow and thin spaces become U+0020.
* - **Compatibility:** before 1.16.0 some locales and calendars lost a requested width (ja-JP with
* the japanese calendar and `month: "long"` gave `"R6/2"`) and `era` alone dropped the time and
* zone name. Pass the fields the old text showed to keep it. To render in another zone, use
* `formatUtc` with its `timeZone` option.
* - `options` null returns `""`, as ECMA-402's CoerceOptionsToObject rejects it (a string or number
* options value formats with the defaults, as `Intl.DateTimeFormat` does).
* - A `chinese` or `dangi` date follows the observatory the standard names: Purple Mountain
* Observatory for `chinese`, KASI for `dangi`. A runtime that prints a different date for one of
* them returns `""` for any format with a year, month, day or era in that calendar. A runtime
* that prints the observatory's date keeps its own text. A format with only a time or weekday
* field always returns its text.
*
* @param value zoned ISO 8601 datetime string
* @param locale optional locale tag (e.g. "en-GB", "ja-JP"), or a preference list of tags (ECMA-402)
* @param options optional Intl.DateTimeFormatOptions
* @returns localized string or "" when invalid
*
* @example formatZonedDateTime("2024-02-29T12:34:56.789+00:00[UTC]", "de-DE", { dateStyle: "long", timeStyle: "long" }) // "29. Februar 2024 um 12:34:56 UTC"
* @example formatZonedDateTime("2024-02-29T12:34:56.789+00:00[UTC]", "en-GB", { dateStyle: "short", timeStyle: "short" }) // "29/02/2024, 12:34"
* @example formatZonedDateTime("2024-02-03T14:30:45+09:00[Asia/Tokyo]", "ja-JP-u-ca-japanese", { year: "numeric", month: "long" }) // "令和6年2月"
* @example formatZonedDateTime("2024-02-03T14:30:45+09:00[Asia/Tokyo]", "ja-JP-u-ca-japanese", { year: "numeric", month: "numeric" }) // "R6/2" — the pre-1.16.0 text
* @example formatZonedDateTime("2024-02-03T14:30:45+00:00[Europe/London]", "en-GB", { era: "long" }) // "03/02/2024 Anno Domini, 14:30:45 GMT"
* @example formatZonedDateTime("2024-02-03T14:30:45+00:00[Europe/London]", "en-GB", { era: "long", year: "numeric", month: "numeric", day: "numeric" }) // "03/02/2024 Anno Domini" — the pre-1.16.0 text
* @example formatZonedDateTime("2024-02-03T14:30:45+00:00[Europe/London]", "en-GB", { timeZone: "Asia/Tokyo" }) // "" — a ZonedDateTime keeps its own zone
* @example formatUtc("2024-02-03T14:30:45Z", "en-GB", { timeZone: "Asia/Tokyo", includeTimeZoneName: true }) // "03/02/2024, 23:30:45 GMT+9" — the same instant in another zone
* @example formatZonedDateTime("invalid", "pt-BR") // "" (invalid input)
* @example formatZonedDateTime("2024-02-03T14:30:45+01:00[Europe/Paris]", ["fr-FR", "en-US"], { dateStyle: "short", timeStyle: "short" }) // "03/02/2024 14:30"
* @example formatZonedDateTime("2024-02-03T14:30:00+05:30[Asia/Kolkata]", "hi-IN", null as never) // "" (null options, as ECMA-402 rejects them)
*/
function formatZonedDateTime(value, locale, options) {
if (options === null) return "";
const zonedDateTime = zonedDateTimeOf(value);
if (zonedDateTime === null) return "";
try {
const resolved = instantFormatOptions(readDateTimeFormatOptions(options), zonedDateTime.timeZoneId, "zoned-date-time");
if (resolved === null) return "";
return normalizeDateTime(cachedFormatter("DateTimeFormat", locale, resolved).format(zonedDateTime.epochMilliseconds));
} catch {
return "";
}
}
export { formatZonedDateTime };