@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.
60 lines (58 loc) • 3.58 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { stringOptionOrDefault } from "../../internal/typedOptions.js";
import { nowZonedDateTimeISO } from "../../internal/temporalCompat/clock.js";
import { zonedDateTimeFrom, zonedDateTimeOf } from "../../internal/zonedWallClock.js";
import { isOptionsObject } from "../../internal/isObject.js";
import { utcInstantOf } from "../../internal/utcInstant.js";
import { formatCalendarDays } from "../../internal/formatCalendarDays.js";
import { isValidZonedFormatReference } from "../../internal/zonedFormatReference.js";
// src/zoned/format/formatCalendarZoned.ts
/**
* Format a zoned date-time as a relative day label plus time-of-day, e.g.
* "tomorrow at 14:30" — the zoned counterpart of `formatCalendar`. See
* that function's JSDoc for the day-label/threshold/connector design; this
* variant differs only in reading `value`'s IANA timezone for both the
* calendar-day comparison and the rendered clock time.
* - `options` must be an object or omitted: `null` or any other primitive returns `""`, as
* Temporal's GetOptionsObject rejects it.
* - 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 ZonedDateTime ISO string to format
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options optional settings for the reference moment and the time style
* @returns the formatted calendar string, or "" on invalid input
*
* @example formatCalendarZoned("2026-03-16T14:30:00+00:00[Europe/London]", "en-GB", { reference: "2026-03-15T09:00:00+00:00[Europe/London]" }) // "tomorrow at 14:30"
* @example formatCalendarZoned("2026-03-16T14:30:00+01:00[Europe/Berlin]", "de-DE", { reference: "2026-03-15T09:00:00+01:00[Europe/Berlin]" }) // "morgen um 14:30"
* @example formatCalendarZoned("not-a-date") // ""
* @example formatCalendarZoned("2024-03-16T14:30:00+02:00[Africa/Johannesburg]", ["fr-FR", "en-US"], { reference: "2024-03-15T09:00:00+02:00[Africa/Johannesburg]" }) // "demain à 14:30"
* @example formatCalendarZoned("2024-03-12T10:00:00+09:00[Asia/Tokyo]", "ja-JP", null as never) // "" (null options)
*/
function formatCalendarZoned(value, locale, options = {}) {
try {
if (!isOptionsObject(options)) return "";
const target = zonedDateTimeOf(value);
if (target === null) return "";
const referenceOption = options.reference;
if (!isValidZonedFormatReference(referenceOption)) return "";
try {
const timeZone = target.timeZoneId;
let reference;
if (referenceOption === void 0) reference = nowZonedDateTimeISO(timeZone);
else if (typeof referenceOption === "string") {
const referenceInstant = utcInstantOf(referenceOption);
reference = referenceInstant !== null ? referenceInstant.toZonedDateTimeISO(timeZone) : zonedDateTimeFrom(referenceOption).withTimeZone(timeZone);
} else reference = Temporal.Instant.fromEpochMilliseconds(referenceOption).toZonedDateTimeISO(timeZone);
return formatCalendarDays(target.toPlainDate().since(reference.toPlainDate()).days, target.epochMilliseconds, timeZone, locale, stringOptionOrDefault(options.timeStyle, "short"));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatCalendarZoned };