@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.
104 lines (102 loc) • 7 kB
JavaScript
import { isRangeDayPeriodCompatNeeded, isRangeZoneNameCompatNeeded } from "../../internal/temporalCompat/capabilities.js";
import { timeZoneEquals } from "../../internal/temporalCompat/timeZoneEquals.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
import { cachedFormatter } from "../../internal/formatterCache.js";
import { normalizeDateTime } from "../../internal/normalizeDateTime.js";
import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js";
import { instantFormatOptions } from "../../internal/instantFormatOptions.js";
import { repairRangeParts } from "../../internal/repairRangeParts.js";
// src/zoned/format/formatZonedRange.ts
/**
* The range with the parts a host gets wrong written in the requested form: the `timeZoneName`
* width (defect D16, ICU-20548) and the flexible `dayPeriod` (defect D17). The part merge is
* `repairRangeParts`. For D16 the carrier formatter uses a `short` zone name, which the engine
* keeps; for D17 it is the requested formatter.
*/
function rangeWithRepairedParts(locale, formatOptions, ms1, ms2, repair) {
const want = cachedFormatter("DateTimeFormat", locale, formatOptions);
const carrier = repair.zoneNames ? cachedFormatter("DateTimeFormat", locale, {
...formatOptions,
timeZoneName: "short"
}) : want;
const wanted = [want.formatToParts(ms1), want.formatToParts(ms2)];
return repairRangeParts(carrier.formatRangeToParts(ms1, ms2), wanted, carrier === want ? wanted : [carrier.formatToParts(ms1), carrier.formatToParts(ms2)], [...repair.zoneNames ? ["timeZoneName"] : [], ...repair.dayPeriods ? ["dayPeriod"] : []]);
}
/**
* Format a zoned datetime range using the Temporal Intl.DateTimeFormat formatRange API.
*
* - Both datetimes must have the same time zone: identifiers of one zone count as the same
* (`UTC` and `Etc/UTC` on every host; an IANA link such as `Europe/Kiev` and `Europe/Kyiv` where
* the host resolves it, which Safari 27 without `Temporal` does not).
* - Formats both instants as Temporal's ECMA-402 Instant format does, through the runtime's
* `Intl.DateTimeFormat#formatRange` in the endpoints' zone: the requested fields and widths are
* kept, and with no date or time field (`era` alone included) the date and time are the
* defaults.
* - The requested `timeZoneName` style is kept, also on engines whose ICU drops it from ranges
* (ICU-20548), and a flexible `dayPeriod` next to a zone name stays "in the morning" on engines
* that write AM/PM in ranges.
* - Returns "" for invalid input, mismatched timezones, or a `timeZone` option (a ZonedDateTime
* keeps its own zone).
* - **Compatibility:** before 1.21.0 on V8 and JSC, `long`, `longGeneric` and the offset zone names
* came out short or missing in ranges, and on Firefox a `dayPeriod` with a `timeZoneName` came out
* as AM or PM. Before 1.16.0 some locales lost a requested width (zh-CN with
* `{ year: "numeric", month: "long" }` gave `"2024/2"`), `era` alone dropped the time, and a
* `timeZone` option was silently overridden by the endpoints' zone. Pass the fields the old text
* showed to keep it, and drop the `timeZone` option to keep the endpoints' zone.
* - Output is normalized: dash separators become ASCII "-" (unspaced between digits, spaced
* otherwise), and no-break, narrow and thin spaces become U+0020.
* - `options` null returns `""`, as ECMA-402's CoerceOptionsToObject rejects it (a string or number
* options value formats with the defaults, as `Intl.DateTimeFormat` does).
*
* @param from zoned ISO 8601 datetime string (range start)
* @param to zoned ISO 8601 datetime string (range end)
* @param locale optional locale tag, or a preference list of tags (ECMA-402)
* @param options optional Intl.DateTimeFormatOptions
* @returns localized range string or "" when invalid
*
* @example formatZonedRange("2024-02-29T12:00:00.000+00:00[UTC]", "2024-02-29T14:00:00.000+00:00[UTC]", "de-DE", { dateStyle: "long", timeStyle: "short" }) // "29. Februar 2024, 12:00-14:00 Uhr"
* @example formatZonedRange("2024-02-29T12:00:00.000+00:00[UTC]", "2024-02-29T14:00:00.000+00:00[UTC]", "en-GB", { dateStyle: "short", timeStyle: "short" }) // "29/02/2024, 12:00-14:00"
* @example formatZonedRange("2024-02-03T14:30:45+08:00[Asia/Shanghai]", "2024-02-03T15:30:45+08:00[Asia/Shanghai]", "zh-CN", { year: "numeric", month: "long" }) // "2024年2月"
* @example formatZonedRange("2024-02-03T14:30:45+00:00[Europe/London]", "2024-02-03T15:30:45+00:00[Europe/London]", "en-GB", { era: "long" }) // "03/02/2024 Anno Domini, 14:30:45 - 15:30:45"
* @example formatZonedRange("2024-02-29T10:00:00+09:00[Asia/Tokyo]", "2024-02-29T12:00:00+09:00[Asia/Tokyo]", "ja-JP", { hour: "numeric", timeZone: "UTC" }) // "" — a ZonedDateTime keeps its own zone
* @example formatZonedRange("2024-02-29T10:00:00+00:00[Europe/London]", "2024-02-29T12:00:00+00:00[Europe/London]", "en-GB", { hour: "numeric", minute: "numeric", timeZoneName: "short" }) // "10:00-12:00 GMT" — the pre-1.16.0 text
* @example formatZonedRange("2024-02-29T10:00:00-05:00[America/New_York]", "2024-02-29T12:00:00-05:00[America/New_York]", "en-US", { hour: "numeric", minute: "numeric", timeZoneName: "long" }) // "10:00 AM - 12:00 PM Eastern Standard Time"
* @example formatZonedRange("invalid", "2024-02-29T14:00:00.000+00:00[UTC]", "fr-FR") // "" (invalid input)
* @example formatZonedRange("2024-02-03T14:30:45+01:00[Europe/Paris]", "2024-02-03T16:46:15+01:00[Europe/Paris]", ["fr-FR", "en-US"], { dateStyle: "short", timeStyle: "short" }) // "03/02/2024, 14:30 - 16:46"
* @example formatZonedRange("2024-02-03T14:30:00-03:00[America/Sao_Paulo]", "2024-02-05T17:00:00-03:00[America/Sao_Paulo]", "pt-BR", null as never) // "" (null options, as ECMA-402 rejects them)
*/
function formatZonedRange(from, to, locale, options) {
try {
if (options === null) return "";
if (!isValidZonedDateTime(from) || !isValidZonedDateTime(to)) return "";
let zdt1;
let zdt2;
try {
zdt1 = zonedDateTimeFrom(from);
zdt2 = zonedDateTimeFrom(to);
} catch {
return "";
}
if (!timeZoneEquals(zdt1, zdt2)) return "";
const formatOptions = instantFormatOptions(readDateTimeFormatOptions(options), zdt1.timeZoneId, "all");
if (formatOptions === null) return "";
try {
const ms1 = zdt1.epochMilliseconds;
const ms2 = zdt2.epochMilliseconds;
const style = formatOptions.timeZoneName;
const zoneNames = style !== void 0 && isRangeZoneNameCompatNeeded(style);
const dayPeriods = style !== void 0 && formatOptions.dayPeriod !== void 0 && isRangeDayPeriodCompatNeeded();
const out = zoneNames || dayPeriods ? rangeWithRepairedParts(locale, formatOptions, ms1, ms2, {
zoneNames,
dayPeriods
}) : cachedFormatter("DateTimeFormat", locale, formatOptions).formatRange(ms1, ms2);
return normalizeDateTime(out);
} catch {
return "";
}
} catch {
return "";
}
}
export { formatZonedRange };