@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.
70 lines (68 loc) • 4.26 kB
JavaScript
import { isoStringBody } from "../../internal/isoStringBody.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { optionOrDefault } from "../../internal/optionOrDefault.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { instantOfWallClock, zoneFrame } from "../../internal/zoneFrame.js";
import { isValidDateTime } from "../../plain/validate/isValidDateTime.js";
// src/instant/convert/resolveLocal.ts
const DISAMBIGUATIONS = [
"compatible",
"earlier",
"later",
"reject"
];
/**
* Resolve a zoneless local wall time in a time zone and return the instant it names.
*
* Feeds deliver wall times with no offset all the time — "gate-out 08:00" — and turning one
* into an instant takes a zone the sender did not send plus a policy for the two days a year
* the mapping is not one-to-one. This states both.
*
* - Call `classifyLocal` first to find out whether the wall time is ambiguous or nonexistent
* before a `disambiguation` policy is applied.
* - Returns a UTC instant, exactly — no rounding, so a nanosecond wall time survives. Pass it
* to `toOffsetInstant` for the offset pair, or reach for `convertPlainDateTimeToZoned` when
* what you want is the bracketed zoned string (it defaults to millisecond precision).
* - `localDateTime` is a PlainDateTime string, as `isValidDateTime` accepts: `<date>T<time>`
* with no offset (an offset means the wall time already resolved). Its RFC 9557 annotations
* are read as `Temporal.PlainDateTime.from` reads them, so a time zone annotation
* (`[Asia/Tokyo]`) is ignored and `timeZone` alone resolves the wall time.
* - Returns `""` on invalid input.
* - `timeZone` may be a UTC offset, including one `getTimeZoneOffset` returned with seconds. A
* fixed offset names one instant for every wall time, so `disambiguation` has nothing to decide
* and `"reject"` does not reject.
*
* @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)
* @param optionsArg How an ambiguous or nonexistent wall time is resolved
* @returns UTC instant string ending in "Z", or "" on invalid input
*
* @example resolveLocal("2024-07-15T12:00:00", "Asia/Tokyo") // "2024-07-15T03:00:00Z"
* @example resolveLocal("2024-10-27T01:30:00", "Europe/London") // "2024-10-27T00:30:00Z" (ambiguous; "compatible" takes the earlier)
* @example resolveLocal("2024-10-27T01:30:00", "Europe/London", { disambiguation: "later" }) // "2024-10-27T01:30:00Z" (the same wall time, an hour later)
* @example resolveLocal("2024-10-27T01:30:00", "Europe/London", { disambiguation: "reject" }) // "" (ambiguous, and not guessed)
* @example resolveLocal("2024-10-06T02:30:00", "Australia/Sydney") // "2024-10-05T16:30:00Z" (nonexistent; "compatible" takes the later)
* @example resolveLocal("2024-10-06T02:30:00", "Australia/Sydney", { disambiguation: "earlier" }) // "2024-10-05T15:30:00Z"
* @example resolveLocal("2024-07-15T12:00:00+05:30", "Asia/Kolkata") // "" (not a zoneless wall time)
* @example resolveLocal("1970-01-01T12:00:00", "-00:44:30") // "1970-01-01T12:44:30Z" (a stored offset with seconds, what `getTimeZoneOffset` returns for Africa/Monrovia then)
* @example resolveLocal("2024-07-15T12:00:00[foo=bar]", "Africa/Johannesburg") // "2024-07-15T10:00:00Z" (elective annotation ignored)
* @example resolveLocal("2024-07-15T12:00:00", "Invalid/Zone") // ""
*/
function resolveLocal(localDateTime, timeZone, optionsArg) {
try {
if (!isOptionsArgument(optionsArg)) return "";
const disambiguation = optionOrDefault(optionsArg?.disambiguation, "compatible");
const frame = zoneFrame(timeZone);
if (!isValidDateTime(localDateTime) || frame === null || !DISAMBIGUATIONS.includes(disambiguation)) return "";
try {
if (frame.shiftNanoseconds !== 0n) return instantOfWallClock(isoStringBody(localDateTime), frame).toString();
return zonedDateTimeFrom(`${isoStringBody(localDateTime)}[${frame.timeZone}]`, { disambiguation }).toInstant().toString();
} catch {
return "";
}
} catch {
return "";
}
}
export { resolveLocal };