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.

208 lines (206 loc) • 9.89 kB
import { Temporal } from "./temporal.js"; import { isZoneId } from "./inputChecks.js"; import { systemTimeZone } from "./resolveLocale.js"; import { nowInstant, nowZonedDateTimeISO } from "./temporalCompat/clock.js"; import { instantFrom } from "./instantNanoseconds.js"; import { formatUtcOffset, parseUtcOffsetNanoseconds } from "./utcOffsetString.js"; import { wallClockAtOffset } from "./wallClockAtOffset.js"; // src/internal/zoneFrame.ts const NANOSECONDS_PER_MINUTE = 60000000000n; /** * Read a time zone argument that may also be a stored UTC offset. * * The only place that turns "time zone identifier or stored offset" into something Temporal can * use. It accepts exactly what `isValidTimeZone` or `isValidUtcOffset` accepts, by five rules in * order: * * 1. Not a string: null. * 2. A time zone identifier (`isValidTimeZone`): that identifier as written, no shift. Every * argument that was valid before stored offsets were read takes this branch. * 3. Not a stored offset (`±HH:MM[:SS]`): null. * 4. A whole number of minutes (`+05:30:00`): the identifier it canonicalises to (`+05:30`), no * shift. * 5. Otherwise (`-00:44:30`): the offset's whole minutes as the zone (`-00:44`), shifted by its * seconds with the offset's own sign (30 seconds further west). * * - The returned `timeZone` is always a time zone identifier, so it is safe in a bracket or in * `Intl.DateTimeFormat`. A seconds offset never becomes one: a caller that writes the zone into * its result validates with `isValidTimeZone` instead and does not call this. * * @param zone the caller's time zone argument * @returns the frame, or null when `zone` is neither a time zone identifier nor a stored offset * * @example zoneFrame("Asia/Kolkata") // { timeZone: "Asia/Kolkata", shiftNanoseconds: 0n } * @example zoneFrame("+05:30:00") // { timeZone: "+05:30", shiftNanoseconds: 0n } * @example zoneFrame("-00:44:30") // { timeZone: "-00:44", shiftNanoseconds: -30000000000n } * @example zoneFrame("Z") // null */ function zoneFrame(zone) { if (typeof zone !== "string") return null; if (isZoneId(zone)) return { timeZone: zone, shiftNanoseconds: 0n }; const offsetNanoseconds = parseUtcOffsetNanoseconds(zone); if (offsetNanoseconds === null) return null; const shiftNanoseconds = offsetNanoseconds % NANOSECONDS_PER_MINUTE; const timeZone = formatUtcOffset(offsetNanoseconds - shiftNanoseconds); return timeZone === null ? null : { timeZone, shiftNanoseconds }; } /** The real offset of a frame with a shift: its minute zone's offset plus the shift. */ function frameOffsetNanoseconds(frame) { const zoneOffset = parseUtcOffsetNanoseconds(frame.timeZone); if (frame.shiftNanoseconds === 0n || zoneOffset === null) throw new RangeError("the frame carries no stored offset"); return zoneOffset + frame.shiftNanoseconds; } /** * Read the `timeZone` option of a `unix/` or `utc/` function whose result names no zone. * * - Omitted (`undefined`) is `"UTC"`: an epoch and a UTC string name an instant, so no host zone * is read unless asked for. * - `"local"` is the system time zone, or null when the host reports none that is valid. * - Anything else is read by `zoneFrame`. * * `normalizeTimeZone` is the twin for a function that writes the zone into its result or hands it * to `Intl.DateTimeFormat`: it accepts a time zone identifier only. * * @param option the caller's `timeZone` option * @returns the frame, or null when the option is invalid * * @example normalizeZoneFrame(undefined) // { timeZone: "UTC", shiftNanoseconds: 0n } * @example normalizeZoneFrame("local") // the system zone, e.g. { timeZone: "Asia/Tokyo", shiftNanoseconds: 0n } * @example normalizeZoneFrame("-00:44:30") // { timeZone: "-00:44", shiftNanoseconds: -30000000000n } * @example normalizeZoneFrame("Asia/Tokio") // null */ function normalizeZoneFrame(option) { if (option === void 0) return { timeZone: "UTC", shiftNanoseconds: 0n }; return zoneFrame(option === "local" ? systemTimeZone() : option); } /** * Read an instant on a frame's local wall clock. For a function that only reads fields. * * - A seconds offset is read with `wallClockAtOffset`, which is exact up to both limits of the * instant range; `frameZoned` is not. * * Throws what Temporal throws; callers wrap it in their own try/catch. * * @param instant the exact time * @param frame the frame from `zoneFrame` * @returns the local wall clock * * @example frameWallClock(Temporal.Instant.from("1970-01-01T12:44:30Z"), zoneFrame("-00:44:30")).toString() // "1970-01-01T12:00:00" */ function frameWallClock(instant, frame) { return frame.shiftNanoseconds === 0n ? instant.toZonedDateTimeISO(frame.timeZone).toPlainDateTime() : wallClockAtOffset(instant, frameOffsetNanoseconds(frame)); } /** * Read the current instant on a frame's local wall clock. * * Throws what Temporal throws; callers wrap it in their own try/catch. * * @param frame the frame from `zoneFrame` * @returns the local wall clock now * * @example frameNowWallClock(zoneFrame("-00:44:30")).toPlainDate().toString() // today's date at -00:44:30 */ function frameNowWallClock(frame) { return frame.shiftNanoseconds === 0n ? nowZonedDateTimeISO(frame.timeZone).toPlainDateTime() : wallClockAtOffset(nowInstant(), frameOffsetNanoseconds(frame)); } /** * Place an instant on a frame's local clock as a `Temporal.ZonedDateTime`, for a body that does * arithmetic on one. * * - For a seconds offset the value sits in the frame's minute zone, at the instant plus the * shift, so its wall clock is the real local one. At a constant offset, arithmetic on the wall * clock is the same in any fixed-offset zone, and a day is always 24 hours. Its own * `epochNanoseconds`, `offset` and `timeZoneId` are not the real ones: turn a result back with * `frameInstant`, and never write it as a string. * - Every value of one call is placed with the same frame, so all of them sit in one zone and * are off by the same shift. Comparing their `epochNanoseconds`, and `until` between them, give * what the real instants give. * - Throws `RangeError` when the instant plus the shift is outside the instant range: within the * shift's length, under a minute, of the last instant for an offset east of UTC, and of the * first for one west. The caller returns its sentinel there, and its JSDoc says so. A function * that only reads fields uses `frameWallClock`, which has no such edge. * * Throws what Temporal throws; callers wrap it in their own try/catch. * * @param instant the exact time * @param frame the frame from `zoneFrame` * @returns the zoned date-time to compute with * * @example frameInstant(frameZoned(Temporal.Instant.from("1970-01-01T12:44:30Z"), zoneFrame("-00:44:30")).startOfDay(), zoneFrame("-00:44:30")).toString() // "1970-01-01T00:44:30Z" */ function frameZoned(instant, frame) { return frame.shiftNanoseconds === 0n ? instant.toZonedDateTimeISO(frame.timeZone) : instant.add({ nanoseconds: Number(frame.shiftNanoseconds) }).toZonedDateTimeISO(frame.timeZone); } /** * The real epoch nanoseconds of a value `frameZoned` produced, or one computed from it. * * @param zoned a zoned date-time on the frame's local clock * @param frame the frame it was placed on * @returns epoch nanoseconds of the instant it names * * @example frameEpochNanoseconds(frameZoned(Temporal.Instant.from("1970-01-01T12:44:30Z"), zoneFrame("-00:44:30")), zoneFrame("-00:44:30")) // 45870000000000n */ function frameEpochNanoseconds(zoned, frame) { return zoned.epochNanoseconds - frame.shiftNanoseconds; } /** * The real instant of a value `frameZoned` produced, or one computed from it. * * Throws `RangeError` when the instant is outside Temporal's range; callers wrap it in their own * try/catch. * * @param zoned a zoned date-time on the frame's local clock * @param frame the frame it was placed on * @returns the instant it names * * @example frameInstant(frameZoned(Temporal.Instant.from("1970-01-01T12:44:30Z"), zoneFrame("-00:44:30")), zoneFrame("-00:44:30")).toString() // "1970-01-01T12:44:30Z" */ function frameInstant(zoned, frame) { return frame.shiftNanoseconds === 0n ? zoned.toInstant() : Temporal.Instant.fromEpochNanoseconds(frameEpochNanoseconds(zoned, frame)); } /** * A frame's stored offset as text: `±HH:MM:SS` for a seconds offset. * * Throws `RangeError` for a frame with no shift, whose offset is its zone's to give; callers wrap * it in their own try/catch. * * @param frame a frame whose `shiftNanoseconds` is not `0n` * @returns the offset string * * @example frameOffset(zoneFrame("-00:44:30")) // "-00:44:30" */ function frameOffset(frame) { const offset = formatUtcOffset(frameOffsetNanoseconds(frame)); if (offset === null) throw new RangeError("the frame carries no stored offset"); return offset; } /** * The instant a local wall time names at a frame's stored offset. * * The wall time and the offset are joined into one date-time string and read by Temporal, which * reads an offset with seconds there (TC39 Temporal §13.31, * `DateTimeUTCOffset[Z] ::: [+Z] UTCDesignator | UTCOffset[+SubMinutePrecision]`). A fixed offset has no * transition, so the wall time is never repeated or skipped and there is nothing to disambiguate. * Exact up to both limits of the instant range. * * Throws what Temporal throws; callers wrap it in their own try/catch. * * @param wallClock an ISO 8601 `<date>T<time>` body with no offset and no annotation * @param frame a frame whose `shiftNanoseconds` is not `0n` * @returns the instant * * @example instantOfWallClock("1970-01-01T12:00:00", zoneFrame("-00:44:30")).toString() // "1970-01-01T12:44:30Z" */ function instantOfWallClock(wallClock, frame) { return instantFrom(`${wallClock}${frameOffset(frame)}`); } export { frameEpochNanoseconds, frameInstant, frameNowWallClock, frameOffset, frameWallClock, frameZoned, instantOfWallClock, normalizeZoneFrame, zoneFrame };