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.

33 lines (31 loc) • 1.42 kB
import { isOptionsArgument } from "../../internal/isObject.js"; import { unixUnitString } from "../../internal/unixWallClock.js"; // src/unix/parse/parseYearFromUnix.ts /** * Return the year from a unix epoch value. * * - Delegates to {@link parseUnitFromUnix} with unit "year". * - `value` is a safe integer or a digit string. * - Returns "" for invalid input. * - The year is written as Temporal writes it: four digits (`"2024"`, `"0005"`), or a sign and six * digits outside 0000–9999 (`"+010000"`, `"-000005"`). * * @param value unix epoch in milliseconds or seconds: a safe integer, or a string of optionally negative ASCII digits * @param options optional: how `value` is read and the zone its wall clock is read in * @returns the year as four digits, or a sign and six digits outside 0000–9999, or "" on invalid input * * @example parseYearFromUnix(1700000000000) // "2023" * @example parseYearFromUnix(-61996320000000) // "0005" (1 June of year 5) * @example parseYearFromUnix(1704067200000, { epochUnit: "milliseconds", timeZone: "UTC" }) // "2024" * @example parseYearFromUnix(-86400, { epochUnit: "seconds" }) // "1969" * @example parseYearFromUnix("") // "" (a blank string is not epoch 0) */ function parseYearFromUnix(value, options) { try { if (!isOptionsArgument(options)) return ""; return unixUnitString(value, "year", options); } catch { return ""; } } export { parseYearFromUnix };