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.

40 lines (38 loc) • 1.46 kB
import { plainDateTimeOf } from "./inputChecks.js"; import { zonedDateTimeFrom } from "./zonedWallClock.js"; import { instantOfWallClock, zoneFrame } from "./zoneFrame.js"; // src/internal/resolveLocalInstant.ts const DISAMBIGUATIONS = [ "compatible", "earlier", "later", "reject" ]; /** * The core of `resolveLocal`: the instant a local date-time names in a zone, under one * disambiguation. Never throws. * * @param localDateTime a PlainDateTime string * @param timeZone an IANA zone or a numeric offset * @param disambiguation how a repeated or skipped wall time resolves * @returns the instant as an ISO 8601 string, or `""` when any input is invalid or the time is * rejected * * @example resolveLocalInstant("2024-06-15T12:00:00", "UTC", "compatible") // "2024-06-15T12:00:00Z" * @example resolveLocalInstant("2024-03-10T02:30:00", "America/New_York", "reject") // "" */ function resolveLocalInstant(localDateTime, timeZone, disambiguation) { try { const frame = zoneFrame(timeZone); if (plainDateTimeOf(localDateTime) === null || frame === null || disambiguation === void 0 || !DISAMBIGUATIONS.includes(disambiguation)) return ""; try { if (frame.shiftNanoseconds !== 0n) return instantOfWallClock(localDateTime, frame).toString(); return zonedDateTimeFrom(`${localDateTime}[${frame.timeZone}]`, { disambiguation }).toInstant().toString(); } catch { return ""; } } catch { return ""; } } export { resolveLocalInstant };