@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.
59 lines (57 loc) • 3.56 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isoStringBody } from "../../internal/isoStringBody.js";
import { instantOfWallClock, zoneFrame } from "../../internal/zoneFrame.js";
import { isValidDateTime } from "../../plain/validate/isValidDateTime.js";
import { classifyWall } from "../../internal/earlierOccurrence.js";
// src/instant/convert/classifyLocal.ts
/**
* Report whether a zoneless local wall time is unique, ambiguous or nonexistent in a zone —
* before any policy resolves it.
*
* Ambiguous and nonexistent wall times are the single most common datetime bug there is:
* 01:30 happens twice on a fall-back day and never on a spring-forward one. `resolveLocal`
* will pick one for you; this exists so a demurrage clock, a medication window or a duty
* limit can refuse instead, or route the case to a human, rather than silently accepting
* whichever instant a default handed it.
*
* - Reads the zone's own transition table via `Temporal`, so it is right for every IANA zone
* and every year, including 30- and 45-minute shifts and zones that skipped a whole
* calendar day changing sides of the date line.
* - `localDateTime` is a PlainDateTime string, as `isValidDateTime` accepts: `<date>T<time>`
* with no offset (a string with an offset has no ambiguity left to report). Its RFC 9557
* annotations are read as `Temporal.PlainDateTime.from` reads them, so a time zone annotation
* is ignored and `timeZone` alone is the zone.
* - Returns null on invalid input — never `"unique"`, which would read as a verdict.
* - `timeZone` may be a UTC offset, including one `getTimeZoneOffset` returned with seconds. A
* fixed offset names one instant for every wall time, so the classification is always `"unique"`.
*
* @param localDateTime zoneless ISO 8601 local datetime string (e.g. "2024-11-03T01:30:00")
* @param timeZone IANA name or UTC offset the wall time is read in: a time zone identifier
* (`+05:30`, `+0530`, `-08`) or a stored offset (`±HH:MM[:SS]`, what `getTimeZoneOffset` returns)
* @returns "unique" | "ambiguous" | "nonexistent", or null on invalid input
*
* @example classifyLocal("2024-07-15T12:00:00", "Asia/Tokyo") // "unique"
* @example classifyLocal("2024-10-27T01:30:00", "Europe/London") // "ambiguous" (fall-back overlap)
* @example classifyLocal("2024-10-06T02:30:00", "Australia/Sydney") // "nonexistent" (spring-forward gap)
* @example classifyLocal("2024-04-07T01:45:00", "Australia/Lord_Howe") // "ambiguous" (a 30-minute overlap)
* @example classifyLocal("2024-09-29T03:15:00", "Pacific/Chatham") // "nonexistent" (a 45-minute-offset zone's gap)
* @example classifyLocal("2011-12-30T12:00:00", "Pacific/Apia") // "nonexistent" (the day Samoa skipped crossing the date line)
* @example classifyLocal("2024-11-03T01:30:00", "UTC") // "unique" (UTC has no transitions)
* @example classifyLocal("1970-01-01T12:00:00", "-00:44:30") // "unique" (a fixed offset has no transitions)
* @example classifyLocal("2024-11-03T01:30:00+00:00", "Europe/London") // null (not a zoneless wall time)
* @example classifyLocal("2024-11-03T01:30:00", "Invalid/Zone") // null
*/
function classifyLocal(localDateTime, timeZone) {
const frame = zoneFrame(timeZone);
if (!isValidDateTime(localDateTime) || frame === null) return null;
try {
if (frame.shiftNanoseconds !== 0n) {
instantOfWallClock(isoStringBody(localDateTime), frame);
return "unique";
}
return classifyWall(Temporal.PlainDateTime.from(localDateTime), frame.timeZone).kind;
} catch {
return null;
}
}
export { classifyLocal };