@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.
68 lines (66 loc) • 4.17 kB
JavaScript
import { isOptionsObject } from "../../internal/isObject.js";
import { durationTotal } from "../../internal/zonedWallClockDifference.js";
import { frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js";
import { resolveUnixEpochUnit, unixEpochToInstant } from "../../internal/unixEpochValue.js";
import { formatRelativeDuration } from "../../internal/formatRelativeDuration.js";
import { resolveUnixFormatReference } from "../../internal/unixFormatReference.js";
// src/unix/format/formatRelativeUnix.ts
/**
* Format the relative time between a unix epoch value 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 `""`.
* - `value` and a numeric `reference` are safe integers or strings of optionally negative ASCII
* digits; `largestUnit` and `epochUnit` accept singular or plural names.
* - `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 unix epoch (string or number, per `epochUnit`) to format
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options optional: the wording, the unit and rounding of the distance, and what it is measured from
* @returns the formatted relative-time string, or "" on invalid input
*
* @example formatRelativeUnix(1710685845000, "en-GB", { epochUnit: "milliseconds", reference: 1805358645000 }) // "3 years ago"
* @example formatRelativeUnix(1710685845000, "de-DE", { epochUnit: "milliseconds", reference: 1805358645000, largestUnit: "day" }) // "vor 1.096 Tagen"
* @example formatRelativeUnix(0, "en-GB", { reference: 37800000, roundingMethod: "floor" }) // "11 hours ago" (−10.5 hours floors to −11; the default rounds to 10)
* @example formatRelativeUnix("0", "ja-JP", { reference: "1800", epochUnit: "second", largestUnit: "minutes" }) // "30 分前"
* @example formatRelativeUnix(0, "zh-CN", { reference: 1800000, timeZone: "Invalid/Zone" }) // ""
* @example formatRelativeUnix(0, "pt-BR", null as never) // ""
* @example formatRelativeUnix("not-a-number") // ""
* @example formatRelativeUnix(1709163000000, ["fr-FR", "en-US"], { reference: 1709164800000 }) // "il y a 30 minutes"
* @example formatRelativeUnix(218670000, "en-GB", { reference: 45870000, timeZone: "-00:44:30" }) // "in 2 days" (a stored offset with seconds)
*/
function formatRelativeUnix(value, locale, options = {}) {
try {
if (!isOptionsObject(options)) return "";
const epochUnit = resolveUnixEpochUnit(options.epochUnit);
if (epochUnit === null) return "";
const frame = normalizeZoneFrame(options.timeZone);
if (frame === null) return "";
const target = unixEpochToInstant(value, epochUnit);
if (target === null) return "";
const reference = resolveUnixFormatReference(options.reference, epochUnit);
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 { formatRelativeUnix };