@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.
168 lines (166 loc) • 7.99 kB
JavaScript
import { Temporal } from "./temporal.js";
import { plainDate } from "../regex/date.js";
import { EXTENDED_UTC_OFFSET, hasInstantShape, hasZonedInstantShape } from "./isoStringBody.js";
import { utcOffset } from "../regex/utc-offset.js";
import { hasSecond60Field } from "./inputChecks.js";
import { zonedDateTimeFrom } from "./zonedWallClock.js";
import { parseInstantNanoseconds } from "./instantNanoseconds.js";
import { isObject } from "./isObject.js";
import { formatUtcOffset, parseUtcOffsetNanoseconds, writtenOffset } from "./utcOffsetString.js";
import { wallClockAtOffset } from "./wallClockAtOffset.js";
import { frameOffset, zoneFrame } from "./zoneFrame.js";
// src/internal/offsetInstant.ts
/**
* The pair for an instant string: `Z` or an extended offset after `<date>T<time>`, nothing after
* it (GMT's instant shape, `hasInstantShape`). One of the two exact readers, with
* `readZonedOffsetInstant`; a string with any annotation is `null` here. Never throws.
*
* @param value ISO 8601 extended instant string, `…Z` or `…±HH:MM[:SS[.fraction]]`
* @param timeZone the zone to read the offset in, instead of the written one
* @returns the pair, or null when `value` is not an instant or `timeZone` is not a zone
*
* @example readInstantOffsetInstant("2024-07-15T16:00:00+05:30") // { instant: "2024-07-15T10:30:00Z", offset: "+05:30" }
* @example readInstantOffsetInstant("2024-07-15T16:00:00+05:30[Asia/Kolkata]") // null (a zoned string; see `readZonedOffsetInstant`)
* @example readInstantOffsetInstant("2024-06-15T10Z") // null (an hour-only time; see `readFormOffsetInstant`)
*/
function readInstantOffsetInstant(value, timeZone) {
if (typeof value !== "string" || hasSecond60Field(value) || !hasInstantShape(value)) return null;
const epochNanoseconds = parseInstantNanoseconds(value);
if (epochNanoseconds === null) return null;
const frame = timeZone === void 0 ? void 0 : zoneFrame(timeZone);
if (frame === null) return null;
return pairAt(value, epochNanoseconds, frame, null);
}
/**
* The pair for a zoned string that writes its offset: `<date>T<time>` then `Z` or an extended
* offset, then exactly one time zone bracket (`hasZonedInstantShape`), nothing after it. The other
* exact reader, with `readInstantOffsetInstant`; a string with no bracket is `null` here, and
* so is one whose offset contradicts its zone. Never throws.
*
* @param value zoned string that writes its offset, `2024-07-15T16:00:00+05:30[Asia/Kolkata]`
* @param timeZone the zone to read the offset in, instead of the bracketed one
* @returns the pair, or null when `value` is not such a string or `timeZone` is not a zone
*
* @example readZonedOffsetInstant("2024-07-15T16:00:00+05:30[Asia/Kolkata]") // { instant: "2024-07-15T10:30:00Z", offset: "+05:30", timeZone: "Asia/Kolkata" }
* @example readZonedOffsetInstant("2024-07-15T16:00:00+05:30") // null (an instant; see `readInstantOffsetInstant`)
*/
function readZonedOffsetInstant(value, timeZone) {
const bracketed = readZonedInstant(value);
if (bracketed === null) return null;
const frame = timeZone === void 0 ? void 0 : zoneFrame(timeZone);
if (frame === null) return null;
return pairAt(value, bracketed.epochNanoseconds, frame, bracketed);
}
/**
* The `ZonedDateTime` a zoned string that writes its offset names, or `null` when `value` is not
* one. `ZonedDateTime.from` is what checks that the written offset and the zone agree, so a
* self-contradictory string is `null`.
*/
function readZonedInstant(value) {
if (typeof value !== "string" || hasSecond60Field(value) || !hasZonedInstantShape(value)) return null;
try {
return zonedDateTimeFrom(value);
} catch {
return null;
}
}
/** `<date>T<hour>` then `Z` or an extended offset, nothing else: the exact hour-only instant. */
const hourInstant = new RegExp(`^${plainDate.source.slice(1, -1)}T(?:[01]\\d|2[0-3])(?:Z|${EXTENDED_UTC_OFFSET})$`);
/**
* The pair for a string `isoHl7Form` matched, at the precision `isoHl7Precision` read from it.
* Never throws.
*
* An hour-only time (`2024-06-15T10Z`, `…T23-05:00`) is ISO 8601-1 §5.3.1.3 reduced precision.
* `readInstantOffsetInstant` refuses it, as `isValidInstant` does, because GMT's instant shape needs
* a minute. Here the caller has already matched the form, and the string must match the exact
* hour-only instant shape (`hourInstant`) over the whole string, so Temporal reads the hour
* directly (`Temporal.Instant.from("2024-06-15T10Z")` is `2024-06-15T10:00:00Z`). Every finer form
* goes through `readInstantOffsetInstant`.
*
* @param value a string `isoHl7Form` matched, with its offset
* @param written the precision `isoHl7Precision` read
* @returns the pair, or null when the value is not an instant
*
* @example readFormOffsetInstant("2024-06-15T23-05:00", "hour") // { instant: "2024-06-16T04:00:00Z", offset: "-05:00" }
*/
function readFormOffsetInstant(value, written) {
if (written !== "hour") return readInstantOffsetInstant(value);
let epochNanoseconds;
try {
if (typeof value !== "string" || !hourInstant.test(value)) return null;
epochNanoseconds = Temporal.Instant.from(value).epochNanoseconds;
} catch {
return null;
}
return pairAt(value, epochNanoseconds, void 0, null);
}
/** The pair for `value` once its instant is read; `frame` is the caller's zone, if any. */
function pairAt(value, epochNanoseconds, frame, bracketed) {
try {
const bracketedZone = bracketed !== null && !utcOffset.test(bracketed.timeZoneId) ? bracketed.timeZoneId : void 0;
const instant = bracketed !== null ? bracketed.toInstant() : Temporal.Instant.fromEpochNanoseconds(epochNanoseconds);
if (frame !== void 0 && frame.shiftNanoseconds !== 0n) return {
instant: instant.toString(),
offset: frameOffset(frame)
};
const resolvedZone = frame?.timeZone ?? bracketedZone;
if (resolvedZone !== void 0) return offsetInstantInZone(instant, resolvedZone);
if (bracketed !== null) return {
instant: instant.toString(),
offset: bracketed.offset
};
const offset = writtenOffset(instant, value);
return offset === null ? null : {
instant: instant.toString(),
offset
};
} catch {
return null;
}
}
/** The pair read in `zone`; an offset zone names no place, so it fills no `timeZone` field. */
function offsetInstantInZone(instant, zone) {
const zoned = instant.toZonedDateTimeISO(zone);
if (utcOffset.test(zoned.timeZoneId)) return {
instant: instant.toString(),
offset: zoned.offset
};
return {
instant: instant.toString(),
offset: zoned.offset,
timeZone: zoned.timeZoneId
};
}
/**
* The core of `fromOffsetInstant`: the local time an instant-plus-offset pair describes, with the
* pair's own offset, or the bracketed zoned string when the pair names a zone. Never throws.
*
* @param value `{ instant, offset, timeZone? }` pair, as `readInstantOffsetInstant` and `readZonedOffsetInstant` return
* @returns the local ISO 8601 string, or `""` on invalid input or a zone that contradicts the offset
*
* @example writeOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "-04:00" }) // "2024-07-15T12:00:00-04:00"
*/
function writeOffsetInstant(value) {
try {
if (!isObject(value)) return "";
const epochNanoseconds = parseInstantNanoseconds(value.instant);
const offsetNanoseconds = parseUtcOffsetNanoseconds(value.offset);
if (epochNanoseconds === null || offsetNanoseconds === null) return "";
const offset = formatUtcOffset(offsetNanoseconds);
if (offset === null) return "";
try {
const instant = Temporal.Instant.fromEpochNanoseconds(epochNanoseconds);
if (value.timeZone !== void 0) {
const zoned = instant.toZonedDateTimeISO(value.timeZone);
if (utcOffset.test(zoned.timeZoneId)) return "";
return zoned.offset === offset ? zoned.toString() : "";
}
return `${wallClockAtOffset(instant, offsetNanoseconds).toString()}${offset}`;
} catch {
return "";
}
} catch {
return "";
}
}
export { readFormOffsetInstant, readInstantOffsetInstant, readZonedOffsetInstant, writeOffsetInstant };