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.

84 lines (82 loc) • 5.9 kB
import { Temporal } from "../../internal/temporal.js"; import { utcOffset } from "../../regex/utc-offset.js"; import { isObject } from "../../internal/isObject.js"; import { formatUtcOffset, parseUtcOffsetNanoseconds } from "../../internal/utcOffsetString.js"; import { parseInstantNanoseconds } from "../../internal/instantNanoseconds.js"; import { wallClockAtOffset } from "../../internal/wallClockAtOffset.js"; // src/instant/convert/fromOffsetInstant.ts /** * Render an instant-plus-offset pair back to the local time it describes. * * The inverse of `toOffsetInstant`: `toOffsetInstant(fromOffsetInstant(pair))` returns * `pair`, for zones with and without DST. * * - Without `timeZone`, returns `<date>T<time>±HH:MM` — the wall clock the event carried and * the offset it carried it in, which is all an offset-only pair knows. * - With `timeZone`, returns the bracketed zoned string `<date>T<time>±HH:MM[Zone]`, so the * zone survives the round trip instead of being silently dropped. * - A `timeZone` whose offset at `instant` is not `offset` returns `""`. The pair is stating * two things that cannot both be true, and the whole point of carrying both fields is that * a contradiction between them is visible. * - `timeZone` accepts every identifier Temporal does, as `toOffsetInstant`'s bracket does — * including the single-component IANA names (`EST5EDT`, `Zulu`). A `timeZone` that is really * an offset (`"-04:00"`, or the `"-0400"`/`"+05"` spellings Temporal canonicalises to one) * returns `""`: it names no place, and no pair `toOffsetInstant` produces carries one. * - `offset` is canonicalised, so `"+05:45:00"` renders as `+05:45` and `"-00:00"` as * `+00:00`. `Z` is not an accepted offset — for an event that happened at UTC the pair's * offset is `+00:00`. RFC 9557 §2.2 gives `Z` and `-00:00` a different meaning from `+00:00` * ("UTC known, local offset unknown"); a pair cannot carry that, so all three render as * `+00:00`. * - With a `timeZone`, a sub-minute offset is *written* rounded to the minute, because RFC * 9557 has no field for the seconds — `Africa/Monrovia`'s `-00:44:30` renders as `-00:45`. * The bracketed zone is what resolves it, so the instant stays exact and the round trip * returns `-00:44:30`. Two exceptions, both Temporal's own: a wall time repeated inside * a sub-minute offset change reads back as its first pass (see `isValidInstant`), and a * string whose local date is −271821-04-19 is refused by `toOffsetInstant`. Without a `timeZone` there is no zone to resolve against, so the * full `±HH:MM:SS` is written instead; that is ISO 8601, not RFC 9557. * - Returns `""` on invalid input. * - A `timeZone` that is an offset with seconds (`-00:44:30`) returns `""`, as any offset there * does. A written zone cannot carry seconds (RFC 9557 §4.1), so the result could not name it. * Pass the IANA name as `timeZone`, or leave `timeZone` out for the local time with its full * offset and no bracket. * * @param value `{ instant, offset, timeZone? }` pair, as produced by `toOffsetInstant` * @returns local ISO 8601 string carrying the pair's own offset, or "" on invalid input * * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "-04:00" }) // "2024-07-15T12:00:00-04:00" * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "-04:00", timeZone: "America/Santiago" }) // "2024-07-15T12:00:00-04:00[America/Santiago]" * @example fromOffsetInstant({ instant: "2024-10-27T00:30:00Z", offset: "+01:00", timeZone: "Europe/London" }) // "2024-10-27T01:30:00+01:00[Europe/London]" — the first 01:30 of the fall-back night * @example fromOffsetInstant({ instant: "2024-10-27T01:30:00Z", offset: "+00:00", timeZone: "Europe/London" }) // "2024-10-27T01:30:00+00:00[Europe/London]" — the second, an hour later * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "+00:00" }) // "2024-07-15T16:00:00+00:00" * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "-00:00" }) // "2024-07-15T16:00:00+00:00" (RFC 9557's "offset unknown" is not kept) * @example fromOffsetInstant({ instant: "1970-01-01T00:00:00Z", offset: "-00:44:30", timeZone: "Africa/Monrovia" }) // "1969-12-31T23:15:30-00:45[Africa/Monrovia]" — RFC 9557 rounds the written offset; the zone keeps the instant exact * @example fromOffsetInstant({ instant: "1970-01-01T00:00:00Z", offset: "-00:44:30" }) // "1969-12-31T23:15:30-00:44:30" — no zone to round against, so the seconds are written * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "+11:00", timeZone: "Australia/Sydney" }) // "" (offset contradicts the zone: Sydney is +10:00 in July) * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "-04:00", timeZone: "-04:00" }) // "" (an offset is not a zone) * @example fromOffsetInstant({ instant: "2024-07-15T16:00:00Z", offset: "Z" }) // "" (a designator, not an offset) * @example fromOffsetInstant({ instant: "not an instant", offset: "-04:00" }) // "" */ function fromOffsetInstant(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 { fromOffsetInstant };