@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.
77 lines (75 loc) • 5.05 kB
JavaScript
import { booleanOption } from "../../internal/typedOptions.js";
import { normalizeDateTime } from "../../internal/normalizeDateTime.js";
import { resolveUnixEpochUnit, unixEpochToInstant } from "../../internal/unixEpochValue.js";
import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js";
import { normalizeTimeZone } from "../../internal/normalizeTimeZone.js";
import { formatWallClockOrZoned } from "../../internal/instantFormatOptions.js";
// src/unix/format/formatUnix.ts
/**
* Format a unix epoch value (string or number) as a localized date/time string.
*
* - Returns `""` if the input is not a valid unix epoch value for the given `epochUnit`.
* - `value` is a safe integer or a string of optionally negative ASCII digits (no whitespace, `+`,
* decimal point or exponent).
* - Without `includeTimeZoneName` the wall clock is formatted as Temporal's ECMA-402 PlainDateTime
* format does (`timeZoneName` is ignored); with it, as the ZonedDateTime format does (a short
* zone name is added to the defaults). Either way the requested fields and style widths are kept,
* and `era` alone still gets the date and time defaults.
* - **Compatibility:** before 1.16.0 some locales and calendars lost a requested width (ja-JP with
* the japanese calendar and `month: "long"` gave `"R6/2"`), a `long`/`full` `timeStyle` replaced
* the `dateStyle` width, `era` alone dropped the time, and `timeZoneName` alone without
* `includeTimeZoneName` returned `""`. Pass the fields the old text showed to keep it.
* - `options` null returns `""`, as ECMA-402's CoerceOptionsToObject 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.
* - 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 unix epoch value to format (string or number, per `epochUnit`)
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options optional: `Intl.DateTimeFormatOptions` fields, plus how `value` is read, the zone and whether its name is shown
* @returns the formatted date/time string, or "" on invalid input
*
* @example formatUnix("1710685845000", "en-GB", { epochUnit: "milliseconds" }) // "17/03/2024, 14:30:45"
* @example formatUnix(1710685845000, "ja-JP", { epochUnit: "milliseconds" }) // "2024/3/17 14:30:45"
* @example formatUnix("1710685845", "de-DE", { epochUnit: "seconds" }) // "17.3.2024, 14:30:45"
* @example formatUnix("1710685845000", "en-GB", { epochUnit: "milliseconds", includeTimeZoneName: true }) // "17/03/2024, 14:30:45 UTC"
* @example formatUnix(1706970645, "ja-JP-u-ca-japanese", { epochUnit: "seconds", year: "numeric", month: "long" }) // "令和6年2月"
* @example formatUnix(1706970645, "ja-JP-u-ca-japanese", { epochUnit: "seconds", year: "numeric", month: "numeric" }) // "R6/2" — the pre-1.16.0 text
* @example formatUnix(1706970645, "en-GB", { epochUnit: "seconds", era: "long" }) // "03/02/2024 Anno Domini, 14:30:45"
* @example formatUnix(1706970645, "en-GB", { epochUnit: "seconds", era: "long", year: "numeric", month: "numeric", day: "numeric" }) // "03/02/2024 Anno Domini" — the pre-1.16.0 text
* @example formatUnix(0, "fr-FR", { timeZoneName: "short" }) // "01/01/1970 00:00:00"
* @example formatUnix(1710685845000, "zh-CN", { timeZone: "Asia/Tokio" }) // "" (unknown zone)
* @example formatUnix("1710685845000.0") // "" (not a digit string)
* @example formatUnix(0, "pt-BR", null as never) // "" (null options, as ECMA-402 rejects them)
* @example formatUnix("not-a-number") // ""
* @example formatUnix(1710685845000, ["fr-FR", "en-US"], { dateStyle: "long" }) // "17 mars 2024"
*/
function formatUnix(value, locale, options) {
try {
if (options === null) return "";
const epochUnit = options?.epochUnit;
const includeTimeZoneName = booleanOption(options?.includeTimeZoneName, false);
const { timeZone, ...intlOptions } = readDateTimeFormatOptions(options);
const resolvedUnit = resolveUnixEpochUnit(epochUnit);
if (resolvedUnit === null) return "";
const instant = unixEpochToInstant(value, resolvedUnit);
if (instant === null) return "";
const tz = normalizeTimeZone(timeZone);
if (!tz) return "";
try {
const zdt = instant.toZonedDateTimeISO(tz);
return normalizeDateTime(formatWallClockOrZoned(zdt, locale, intlOptions, includeTimeZoneName));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatUnix };