@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.
82 lines (80 loc) • 4.62 kB
JavaScript
import { stringOptionOrDefault } from "../../internal/typedOptions.js";
import { plainDateTimeOf } from "../../internal/inputChecks.js";
import { nowPlainDateTimeISO } from "../../internal/temporalCompat/clock.js";
import { cachedFormatter } from "../../internal/formatterCache.js";
import { isOptionsObject } from "../../internal/isObject.js";
import { joinDateTimeConnector } from "../../internal/joinDateTimeConnector.js";
import { normalizeDateTime } from "../../internal/normalizeDateTime.js";
import { plainTimeStyle } from "../../internal/plainFormatOptions.js";
// src/plain/format/formatCalendar.ts
const ABS_DAY_THRESHOLD = 6;
/**
* Format a plain date-time as a relative day label plus time-of-day, e.g.
* "Tomorrow at 2:30 PM". The `formatRelativeDateTime` family does not
* cover this: it renders "in 1 day", an elapsed-time phrase, not a day
* label + clock time.
*
* - Within `±6` days of `reference` (default: now), renders `<day label>`
* joined to the localized time using the locale's own connector — never a
* hardcoded "at". The day label comes from `Intl.RelativeTimeFormat`
* (`numeric: "auto"`), so it reads "Today"/"Tomorrow"/"Yesterday" near
* the boundary and "in N days"/"N days ago" further out, all locale-native.
* - Beyond `±6` days, falls back to an absolute `dateStyle: "long"` +
* `timeStyle` string with no relative wording. The one-week limit is
* GMT's own rule, not a standard's: from 7 days out a date is easier to
* place than a day count.
* - The connector between the day label and the time is read from CLDR's
* own combined date+time pattern for the locale (see
* `internal/joinDateTimeConnector.ts`), not hardcoded — this is what lets
* `formatCalendar` avoid the i18n objection that excludes a token
* formatter.
* - The time never carries a zone name: a plain value has none.
* - Use `formatCalendar` for user-facing schedules ("Tomorrow at 2:30 PM");
* use `formatRelativeDateTime` for elapsed-time displays ("in 1 day").
* - `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 ISO PlainDateTime string to format
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options What the value is compared with and how the time is written
* @returns the formatted calendar string, or "" on invalid input
*
* @example formatCalendar("2026-03-16T14:30:00", "en-GB", { reference: "2026-03-15T09:00:00" }) // "tomorrow at 14:30"
* @example formatCalendar("2026-03-08T14:30:00", "pt-BR", { reference: "2026-03-15T09:00:00" }) // "8 de março de 2026 às 14:30" (7 days out — beyond the threshold, absolute fallback)
* @example formatCalendar("not-a-date") // ""
* @example formatCalendar("2024-03-16T14:30:00", ["fr-FR", "en-US"], { reference: "2024-03-15T09:00:00" }) // "demain à 14:30"
* @example formatCalendar("2024-03-12T10:00:00", "zh-CN", null as never) // "" (null options)
*/
function formatCalendar(value, locale, options = {}) {
try {
if (!isOptionsObject(options)) return "";
const target = plainDateTimeOf(value);
if (target === null) return "";
const referenceOption = options.reference;
const referenceOptionParsed = plainDateTimeOf(referenceOption);
if (referenceOption !== void 0 && referenceOptionParsed === null) return "";
try {
const reference = referenceOptionParsed ?? nowPlainDateTimeISO();
const diffDays = target.toPlainDate().since(reference.toPlainDate()).days;
const timeStyle = plainTimeStyle(stringOptionOrDefault(options.timeStyle, "short"));
const epochMilliseconds = target.toZonedDateTime("UTC").epochMilliseconds;
if (Math.abs(diffDays) > ABS_DAY_THRESHOLD) return normalizeDateTime(cachedFormatter("DateTimeFormat", locale, {
dateStyle: "long",
timeStyle,
timeZone: "UTC"
}).format(epochMilliseconds));
const dayLabel = cachedFormatter("RelativeTimeFormat", locale, { numeric: "auto" }).format(diffDays, "day");
return normalizeDateTime(joinDateTimeConnector(epochMilliseconds, "UTC", locale, dayLabel, timeStyle));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatCalendar };