@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.
86 lines (84 loc) • 3.55 kB
JavaScript
import { stringOptionOrDefault } from "./typedOptions.js";
import { cachedFormatter, checkFormatterArguments } from "./formatterCache.js";
import { plainDateTimeFormatOptions } from "./plainFormatOptions.js";
// src/internal/instantFormatOptions.ts
const REQUIRED_FIELDS = [
"weekday",
"year",
"month",
"day",
"dayPeriod",
"hour",
"minute",
"second",
"fractionalSecondDigits"
];
const DEFAULT_FIELDS = {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric"
};
/**
* Resolve caller options into runtime `Intl.DateTimeFormat` options that
* format an exact time in `timeZone`.
*
* - Returns `null` when the caller passed a `timeZone` option: the value is
* formatted in its own zone, and `Temporal.ZonedDateTime#toLocaleString`
* throws a TypeError for one (CreateDateTimeFormat with
* toLocaleStringTimeZone).
* - With no date or time field and no `dateStyle`/`timeStyle`, year, month,
* day, hour, minute and second default to `"numeric"`. With `defaults`
* `"zoned-date-time"` (ZonedDateTime#toLocaleString) `timeZoneName` also
* defaults to `"short"`; with `"all"` (an Instant) it does not.
*
* @param options caller's `Intl.DateTimeFormatOptions`
* @param timeZone the zone the value is formatted in
* @param defaults GetDateTimeFormat defaults: `"zoned-date-time"` or `"all"`
* @returns runtime options in `timeZone`, or `null` for a `timeZone` option
*/
function instantFormatOptions(options, timeZone, defaults) {
if (options.timeZone !== void 0) return null;
if (!(options.dateStyle === void 0 && options.timeStyle === void 0 && REQUIRED_FIELDS.every((field) => options[field] === void 0))) return {
...options,
timeZone
};
return {
...options,
...DEFAULT_FIELDS,
...defaults === "zoned-date-time" ? { timeZoneName: stringOptionOrDefault(options.timeZoneName, "short") } : {},
timeZone
};
}
/**
* Format an exact time as text, either as its wall clock (a PlainDateTime) or
* as a ZonedDateTime with its zone, through the runtime's
* `Intl.DateTimeFormat`. Shared by `formatUtc` and `formatUnix`.
*
* - The wall-clock path uses {@link plainDateTimeFormatOptions}
* (GetDateTimeFormat ~any~, ~all~, ~relevant~): `timeZoneName` is not
* inherited.
* - The zoned path uses {@link instantFormatOptions} with
* `"zoned-date-time"` defaults.
* - `options` must not carry a `timeZone`; the zone comes from
* `zonedDateTime`. Constructing with `options` first surfaces the TypeError
* or RangeError Intl.DateTimeFormat raises for invalid ones.
*
* @param zonedDateTime the value in the zone it is rendered in
* @param locale BCP 47 locale, or `undefined` for the runtime default
* @param options caller's `Intl.DateTimeFormatOptions` without `timeZone`
* @param includeTimeZoneName `true` for the zoned path
* @returns the formatted text, not yet normalised
*/
function formatWallClockOrZoned(zonedDateTime, locale, options, includeTimeZoneName) {
checkFormatterArguments("DateTimeFormat", locale, options);
if (includeTimeZoneName) {
const resolved = instantFormatOptions(options, zonedDateTime.timeZoneId, "zoned-date-time");
return resolved === null ? "" : cachedFormatter("DateTimeFormat", locale, resolved).format(zonedDateTime.epochMilliseconds);
}
const wallClockMilliseconds = zonedDateTime.toPlainDateTime().toZonedDateTime("UTC").epochMilliseconds;
return cachedFormatter("DateTimeFormat", locale, plainDateTimeFormatOptions(options)).format(wallClockMilliseconds);
}
export { formatWallClockOrZoned, instantFormatOptions };