UNPKG

@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.

54 lines (52 loc) • 3.17 kB
import { Temporal } from "../../internal/temporal.js"; import { nowZonedDateTimeISO } from "../../internal/temporalCompat/clock.js"; import { isObject } from "../../internal/isObject.js"; import { optionOrDefault } from "../../internal/optionOrDefault.js"; import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js"; import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js"; import { formatCalendarDays } from "../../internal/formatCalendarDays.js"; import { isValidUtc } from "../../utc/validate/isValidUtc.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. * * @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 (!isObject(options)) return ""; if (!isValidZonedDateTime(value)) return ""; const referenceOption = options.reference; if (!isValidZonedFormatReference(referenceOption)) return ""; try { const target = zonedDateTimeFrom(value); const timeZone = target.timeZoneId; let reference; if (referenceOption === void 0) reference = nowZonedDateTimeISO(timeZone); else if (typeof referenceOption === "string") reference = isValidUtc(referenceOption) ? Temporal.Instant.from(referenceOption).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, optionOrDefault(options.timeStyle, "short")); } catch { return ""; } } catch { return ""; } } export { formatCalendarZoned };