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.

34 lines (32 loc) • 1.43 kB
import { resolveUnixEpochUnitOptions, unixEpochToInstant } from "../../internal/unixEpochValue.js"; // src/unix/convert/convertUnixToUtc.ts /** * Convert a unix epoch value to a UTC Instant ISO string. * * - Converts to UTC Instant using Temporal.Instant. * - `value` is a safe integer or a string of optionally negative ASCII digits; anything else, or an * instant outside the Temporal range, returns "". * - An explicit `undefined` `options` is the same as omitted; a non-object `options` (such as a * bare `"seconds"` string) returns "". * * @param value epoch value: a safe integer or a digit string * @param options optional: how `value` is read * @returns UTC Instant string or "" on invalid * * @example convertUnixToUtc(1709164800000) // "2024-02-29T00:00:00Z" * @example convertUnixToUtc(1709164800, { epochUnit: "seconds" }) // "2024-02-29T00:00:00Z" * @example convertUnixToUtc("1709164800", { epochUnit: "second" }) // "2024-02-29T00:00:00Z" * @example convertUnixToUtc(-1) // "1969-12-31T23:59:59.999Z" * @example convertUnixToUtc(1709164800, "seconds" as never) // "" (options must be an object) * @example convertUnixToUtc(NaN) // "" */ function convertUnixToUtc(value, options) { try { const epochUnit = resolveUnixEpochUnitOptions(options); if (epochUnit === null) return ""; return unixEpochToInstant(value, epochUnit)?.toString() ?? ""; } catch { return ""; } } export { convertUnixToUtc };