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.

53 lines • 3.51 kB
/** * What a zoneless wall time turns out to be once a zone is attached to it. * * @remarks Members: * * | Member | Description | * | --- | --- | * | `unique` | One instant. The ordinary case, and every case in a zone with no DST. | * | `ambiguous` | Two instants an offset shift apart — a fall-back hour the clock ran through twice. | * | `nonexistent` | No instant. A spring-forward hour the clock skipped, or a calendar day a zone deleted crossing the date line. | * * @example * import { LocalTimeClassification } from "@northguild/gmt/instant"; * const kind: LocalTimeClassification = "ambiguous"; */ export type LocalTimeClassification = "unique" | "ambiguous" | "nonexistent"; /** * 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 and no annotation of any kind (a string with an offset has no ambiguity left * to report), so `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 */ export declare function classifyLocal(localDateTime: string, timeZone: string): LocalTimeClassification | null;