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.

32 lines (30 loc) • 1.44 kB
import { isOptionsArgument } from "../../internal/isObject.js"; import { unixWallClock } from "../../internal/unixWallClock.js"; // src/unix/parse/parseTimeFromUnix.ts /** * Extract the time portion from a unix epoch value. * * - Converts to ZonedDateTime then extracts the PlainTime. * - Returns "" for invalid input. * * @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 ISO time string (e.g., "14:30:45") or "" on invalid input * * @example parseTimeFromUnix(1700000000000, { timeZone: "UTC" }) // "22:13:20" * @example parseTimeFromUnix(1700000000, { epochUnit: "seconds", timeZone: "UTC" }) // "22:13:20" * @example parseTimeFromUnix(-86400, { epochUnit: "seconds", timeZone: "UTC" }) // "00:00:00" * @example parseTimeFromUnix("1700000000", { epochUnit: "second" }) // "22:13:20" (digit string, UTC by default) * @example parseTimeFromUnix(1.5) // "" (not an integer epoch) * @example parseTimeFromUnix(45870000, { timeZone: "-00:44:30" }) // "12:00:00" (a stored offset with seconds) */ function parseTimeFromUnix(value, options) { try { if (!isOptionsArgument(options)) return ""; const wallClock = unixWallClock(value, options); return wallClock === null ? "" : wallClock.toPlainTime().toString(); } catch { return ""; } } export { parseTimeFromUnix };