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.

69 lines (67 loc) • 3.87 kB
import { Temporal } from "../../internal/temporal.js"; import { nowZonedDateTimeISO } from "../../internal/temporalCompat/clock.js"; import { zonedDateTimeFrom, zonedDateTimeOf } from "../../internal/zonedWallClock.js"; import { isOptionsObject } from "../../internal/isObject.js"; import { durationTotal } from "../../internal/zonedWallClockDifference.js"; import { formatRelativeDuration } from "../../internal/formatRelativeDuration.js"; import { utcInstantOf } from "../../internal/utcInstant.js"; import { isValidZonedFormatReference } from "../../internal/zonedFormatReference.js"; // src/zoned/format/formatRelativeZoned.ts /** * Format the relative time between a zoned date-time and a reference instant. * * - Auto-picks the display unit based on the distance, unless `largestUnit` forces one: second * under a minute, minute under an hour, hour under a day, then `formatRelativeDate`'s thresholds — * day under 7 days, week under 28, month under 365, year beyond — so a 3-year distance reads * "3 years ago". * - **Compatibility:** before 1.16.0 week, month and year were never auto-picked ("1,096 days ago"). * Pass `largestUnit: "day"` to keep a day count. * - `largestUnit` is one of the seven units from `"second"` to `"year"`, singular or plural. Any * other value returns `""`. * - `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 wording, the display unit, rounding and the reference moment * @returns the formatted relative-time string, or "" on invalid input * * @example formatRelativeZoned("2023-12-29T00:00:00+00:00[UTC]", "en-GB", { reference: "2024-02-29T00:00:00+00:00[UTC]" }) // "2 months ago" * @example formatRelativeZoned("2026-03-08T01:00:00+05:30[Asia/Kolkata]", "en-IN", { reference: "2026-03-07T01:00:00+05:30[Asia/Kolkata]" }) // "tomorrow" * @example formatRelativeZoned("2026-01-15T00:00:00+00:00[UTC]", "en-US", { reference: "2026-01-15T10:30:00+00:00[UTC]", roundingMethod: "floor" }) // "11 hours ago" (−10.5 hours floors to −11; the default rounds to 10) * @example formatRelativeZoned("not-a-date") // "" * @example formatRelativeZoned("2024-02-28T23:30:00+00:00[UTC]", ["fr-FR", "en-US"], { reference: "2024-02-29T00:00:00+00:00[UTC]" }) // "il y a 30 minutes" * @example formatRelativeZoned("2024-03-12T10:00:00+02:00[Africa/Johannesburg]", "de-DE", null as never) // "" (null options) */ function formatRelativeZoned(value, locale, options = {}) { try { if (!isOptionsObject(options)) return ""; const valueZDT = zonedDateTimeOf(value); if (valueZDT === null) return ""; const referenceOption = options.reference; if (!isValidZonedFormatReference(referenceOption)) return ""; try { const valueInstant = valueZDT.toInstant(); const refZDT = resolveZonedReference(referenceOption, valueZDT.timeZoneId); const diff = valueInstant.since(refZDT.toInstant()); return formatRelativeDuration(diff, locale, options, (unit) => durationTotal(diff, unit, refZDT)); } catch { return ""; } } catch { return ""; } } /** * The reference as a zoned date-time: now or a UTC string or epoch placed into `timeZoneId`, or a * zoned string kept in its own zone. Throws where Temporal throws. */ function resolveZonedReference(reference, timeZoneId) { if (reference === void 0) return nowZonedDateTimeISO(timeZoneId); if (typeof reference === "string") { const instant = utcInstantOf(reference); return instant !== null ? instant.toZonedDateTimeISO(timeZoneId) : zonedDateTimeFrom(reference); } return Temporal.Instant.fromEpochMilliseconds(reference).toZonedDateTimeISO(timeZoneId); } export { formatRelativeZoned };