@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.
63 lines (61 loc) • 3.61 kB
JavaScript
import { isObject } from "../../internal/isObject.js";
import { durationTotal } from "../../internal/zonedWallClockDifference.js";
import { frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js";
import { formatRelativeDuration } from "../../internal/formatRelativeDuration.js";
import { isValidUtc } from "../validate/isValidUtc.js";
import { toInstantFromUtc, toReferenceInstantFromUtc } from "../../internal/toInstantFromUtc.js";
// src/utc/format/formatRelativeUtc.ts
/**
* Format the relative time between a UTC ISO string and a reference instant.
*
* - Picks the display unit from the distance, using `formatRelativeDate`'s day thresholds past a
* day, 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` returns `""`, as Temporal's GetOptionsObject
* rejects it.
* - **Limit at an offset with seconds.** A month or a year is measured from `reference`, placed in
* the offset's whole-minute zone and moved by its seconds, and the moved instant must be inside
* Temporal's range. So when one is measured and `reference` is within the offset's seconds (under
* a minute) of the last instant Temporal supports (`+275760-09-13T00:00:00Z`) for an offset east
* of UTC, or of the first (`-271821-04-20T00:00:00Z`) for one west, this returns `""`. An offset
* to the minute has no such limit.
*
* @param value UTC ISO string to format
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options The reference instant, the display unit, rounding, wording and time zone
* @returns the formatted relative-time string, or "" on invalid input
*
* @example formatRelativeUtc("2026-01-15T14:30:45Z", "de-DE", { reference: "2026-04-15T14:30:45Z" }) // "vor 3 Monaten"
* @example formatRelativeUtc("2026-01-15T00:00:00Z", "en-GB", { reference: "2026-01-15T10:30:00Z", roundingMethod: "floor" }) // "11 hours ago" (−10.5 hours floors to −11; the default rounds to 10)
* @example formatRelativeUtc("2026-01-15T00:00:00Z", "zh-CN", { reference: "2026-01-15T00:30:00Z", timeZone: "Invalid/Zone" }) // ""
* @example formatRelativeUtc("2026-01-15T14:30:45Z", "pt-BR", null as never) // ""
* @example formatRelativeUtc("not-a-date") // ""
* @example formatRelativeUtc("2024-03-15T11:30:00Z", ["fr-FR", "en-US"], { reference: "2024-03-15T12:00:00Z" }) // "il y a 30 minutes"
* @example formatRelativeUtc("1970-01-03T12:00:00Z", "en-GB", { reference: "1970-01-01T12:00:00Z", timeZone: "-00:44:30" }) // "in 2 days" (a stored offset with seconds)
*/
function formatRelativeUtc(value, locale, options = {}) {
try {
if (!isObject(options)) return "";
if (!isValidUtc(value)) return "";
const frame = normalizeZoneFrame(options.timeZone);
if (frame === null) return "";
const referenceOption = options.reference;
if (referenceOption !== void 0 && !isValidUtc(referenceOption)) return "";
const target = toInstantFromUtc(value);
if (target === null) return "";
const reference = toReferenceInstantFromUtc(referenceOption);
if (reference === null) return "";
try {
const diff = target.since(reference);
return formatRelativeDuration(diff, locale, options, (unit) => durationTotal(diff, unit, frameZoned(reference, frame)));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatRelativeUtc };