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.

30 lines (28 loc) • 1 kB
import { Temporal } from "./temporal.js"; import { nowInstant } from "./temporalCompat/clock.js"; // src/internal/toInstantFromUtc.ts function toInstantFromUtc(value) { try { return Temporal.Instant.from(value); } catch { return null; } } /** * Resolve a `reference` option to an instant: the parsed UTC string when one is given, else now. * * @param reference a UTC ISO string the caller has validated, or undefined (and `""`) for now * @returns the instant, or null when the string does not parse or the clock is unavailable * @example toReferenceInstantFromUtc("2024-03-15T12:00:00Z")?.toString() // "2024-03-15T12:00:00Z" * @example toReferenceInstantFromUtc(undefined) instanceof Temporal.Instant // true (now) * @example toReferenceInstantFromUtc("not-a-date") // null */ function toReferenceInstantFromUtc(reference) { if (reference) return toInstantFromUtc(reference); try { return nowInstant(); } catch { return null; } } export { toInstantFromUtc, toReferenceInstantFromUtc };