@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.
58 lines (56 loc) • 3.26 kB
JavaScript
import { isObject } from "../../internal/isObject.js";
import { optionOrDefault } from "../../internal/optionOrDefault.js";
import { resolveUnixEpochUnit, unixEpochToInstant } from "../../internal/unixEpochValue.js";
import { formatCalendarInstants } from "../../internal/formatCalendarDays.js";
import { normalizeTimeZone } from "../../internal/normalizeTimeZone.js";
import { resolveUnixFormatReference } from "../../internal/unixFormatReference.js";
// src/unix/format/formatCalendarUnix.ts
/**
* Format a unix epoch value as a relative day label plus time-of-day, e.g.
* "tomorrow at 14:30" — the unix counterpart of `formatCalendar`. See
* that function's JSDoc for the day-label/threshold/connector design; this
* variant compares calendar days and renders the clock time in `timeZone`.
*
* - `value` and a numeric `reference` are safe integers or strings of optionally negative ASCII
* digits; anything else returns `""`.
* - `options` must be an object or omitted: `null` returns `""`, as Temporal's GetOptionsObject
* rejects it.
* - An offset with seconds (`-00:44:30`) returns `""`. `Intl.DateTimeFormat` takes a time zone
* identifier only: constructing one with an offset that has seconds throws (ECMA-402, the
* `DateTimeFormat` construction step "If parseResult contains more than one MinuteSecond Parse
* Node, throw a RangeError exception"). Pass the IANA name.
*
* @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 reference instant, how epochs are read, the zone and the width of the time
* @returns the formatted calendar string, or "" on invalid input
*
* @example formatCalendarUnix(1710685845000, "ja-JP", { epochUnit: "milliseconds", timeZone: "Asia/Tokyo" }) // day label + time relative to "now", or the absolute fallback beyond the ±6-day threshold
* @example formatCalendarUnix(1710772200000, "en-GB", { reference: 1710685000000, timeZone: "UTC" }) // "tomorrow at 14:30"
* @example formatCalendarUnix("1710772200", "de-DE", { reference: "1710685000", epochUnit: "second" }) // "morgen um 14:30"
* @example formatCalendarUnix(1710772200000, "fr-FR", { reference: 1710685000000, timeZone: "Australia/Sidney" }) // "" (unknown zone)
* @example formatCalendarUnix(1710772200000, "hi-IN", null as never) // ""
* @example formatCalendarUnix("not-a-number") // ""
* @example formatCalendarUnix(1710613800000, ["fr-FR", "en-US"], { reference: 1710507600000 }) // "demain à 18:30"
*/
function formatCalendarUnix(value, locale, options = {}) {
try {
if (!isObject(options)) return "";
const epochUnit = resolveUnixEpochUnit(options.epochUnit);
if (epochUnit === null) return "";
const timeZone = normalizeTimeZone(options.timeZone);
if (!timeZone) return "";
const target = unixEpochToInstant(value, epochUnit);
if (target === null) return "";
const reference = resolveUnixFormatReference(options.reference, epochUnit);
if (reference === null) return "";
try {
return formatCalendarInstants(target, reference, timeZone, locale, optionOrDefault(options.timeStyle, "short"));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatCalendarUnix };