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.

97 lines (95 loc) • 3.21 kB
import { Temporal } from "./temporal.js"; import { fhirDateTime } from "../regex/fhir.js"; import { readInstantOffsetInstant } from "./offsetInstant.js"; import { LEAP_SECOND_STEPS, taiOfUtc } from "./leapSeconds.js"; import { parseUtcReading } from "./utcReading.js"; // src/internal/fhirDateTime.ts /** FHIR precisions, coarsest first. */ const FHIR_PRECISIONS = Object.freeze([ "year", "month", "day", "second", "fraction" ]); /** * The precisions each type allows: `date` has no time, `instant` is at least to the second, and * `dateTime` takes all five (R4 Datatypes §2.24.0.1). */ const FHIR_TYPE_PRECISIONS = Object.freeze({ date: Object.freeze([ "year", "month", "day" ]), dateTime: FHIR_PRECISIONS, instant: Object.freeze(["second", "fraction"]) }); /** The longest fraction Temporal holds: nanoseconds. */ const MAX_FRACTION_DIGITS = 9; /** * True when `type` is a FhirDateType and allows `precision`. An own-key check, so `"toString"` * is false. */ function fhirTypeAllows(type, precision) { return typeof type === "string" && Object.hasOwn(FHIR_TYPE_PRECISIONS, type) && FHIR_TYPE_PRECISIONS[type].includes(precision); } /** The date part names a day that exists (§2.24.0.1 "Dates SHALL be valid dates"). */ function isRealCalendarDate(year, month, day) { try { Temporal.PlainDate.from({ year: Number(year), month: month === void 0 ? 1 : Number(month), day: day === void 0 ? 1 : Number(day) }, { overflow: "reject" }); return true; } catch { return false; } } /** * The time and offset are a moment Temporal holds. A second of `60` goes through the leap-second * core (exactly what `isLeapSecond` does), because `readInstantOffsetInstant` refuses every one. */ function isRealMoment(value, second) { if (second === "60") { const reading = parseUtcReading(value); return reading !== null && reading.secondSixty && taiOfUtc(LEAP_SECOND_STEPS, reading) !== null; } return readInstantOffsetInstant(value) !== null; } /** * The core of `parseFHIR`, `isValidFHIR` and `formatFHIR`'s final check. Never throws. * * The pattern reads the form, Temporal reads the values: a month or day that does not exist is * refused, a second of `60` only on an inserted leap second (GMT rule, SPA-48's table), and a * fraction of more than nine digits not at all. */ function readFhirValue(value) { if (typeof value !== "string") return null; try { const m = fhirDateTime.exec(value); if (m === null) return null; const [, year = "", , , , month, , day, , , second, fraction, designator] = m; if (!isRealCalendarDate(year, month, day)) return null; if (designator === void 0) { let precision = "year"; if (day !== void 0) precision = "day"; else if (month !== void 0) precision = "month"; return { value, precision, offset: "" }; } if (fraction !== void 0 && fraction.length - 1 > MAX_FRACTION_DIGITS || !isRealMoment(value, second)) return null; return { value, precision: fraction === void 0 ? "second" : "fraction", offset: designator === "Z" || designator === "-00:00" ? "+00:00" : designator }; } catch { return null; } } export { FHIR_PRECISIONS, FHIR_TYPE_PRECISIONS, fhirTypeAllows, readFhirValue };