@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.
152 lines (150 loc) • 7.94 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { parseUtcOffsetNanoseconds } from "../../internal/utcOffsetString.js";
import { readFormOffsetInstant } from "../../internal/offsetInstant.js";
import { hl7PrecisionRank, isoHl7Form, isoHl7Precision } from "../../internal/hl7DateTime.js";
import { FHIR_PRECISIONS, fhirTypeAllows, readFhirValue } from "../../internal/fhirDateTime.js";
// src/healthcare/format/formatFHIR.ts
const NANOSECONDS_PER_MINUTE = 60000000000n;
const RANK_DAY = hl7PrecisionRank("day");
const RANK_SECOND = hl7PrecisionRank("second");
/**
* Write an ISO 8601 string as a FHIR R4 `date`, `dateTime` or `instant`: the inverse of `parseFHIR`.
*
* FHIR R4 Datatypes §2.24.0.1 limits each type: `date` is `YYYY`, `YYYY-MM` or `YYYY-MM-DD` with
* no time zone; `dateTime` adds a time that SHALL carry seconds and a zone; `instant` is at least
* to the second and SHALL include a zone.
*
* | Type | Precisions written | Default precision |
* | --- | --- | --- |
* | `date` | year, month, day | the input's, or day for an input with a time |
* | `dateTime` | year, month, day, second, fraction | the input's |
* | `instant` | second, fraction | the input's |
*
* - It cuts and never rounds, so the result never names a later second, day or month than the
* input's own wall clock. `options.precision` only lowers the precision; a finer one returns `""`.
* - Under `date`, the result is the wall-clock date at the input's own offset
* (`2024-06-15T23:30:00-05:00` is `2024-06-15`), because §2.24.0.1 forbids a zone on a `date`.
* - The designator is written as the input spells it (`Z`, `+00:00` and `-00:00` stay as
* written; `+05:30:00` is written `+05:30`). The fraction always uses `.`.
* - A second of `60` returns `""`: §2.24.0.1 says "applications producing them MAY choose to avoid
* encoding leap seconds".
* - There is no hour or minute precision, and nothing is zero-filled: a value written to the hour
* or minute, with or without an offset, is written only when cut to a day or coarser
* (`2024-06-15T23-05:00` is `2024-06-15` under `date`). An hour-only time is ISO 8601-1
* §5.3.1.3 reduced precision, which Temporal reads.
* - An offset that is not a whole number of minutes returns `""` (`+02:00:30`; Monrovia before
* 1972, `Africa/Monrovia`, was −0:44:30). This applies under `date` too, even though a `date`
* never writes the offset.
* - Disambiguation: none, in the vocabulary of `LOCAL_TIME_RESOLUTION.md`. A wall time with no
* offset is written only as a date.
* In a repeated hour at the end of DST, the offset decides which one is written.
* - Annotations: none is read. A bracket of any kind (a zone, `[u-ca=iso8601]`, `[foo=bar]`) returns
* `""`; write the wall clock with the zone's offset instead.
* - Never throws: any failure, for input of any type, is `""`.
*
* The matching reader is `parseFHIR`.
*
* @param value ISO 8601 date, date-time, or date-time with an offset
* @param type The FHIR type to write
* @param options How much of the value is written
* @returns the FHIR value, or `""` when the value cannot be written as `type`
*
* @example formatFHIR("2024-06-15T23:30:00-05:00", "date") // "2024-06-15"
* @example formatFHIR("2024-06-15T23-05:00", "date") // "2024-06-15" (an hour with an offset)
* @example formatFHIR("2024-01-15T10:30:00Z[Europe/London]", "dateTime") // "" (an annotation)
* @example formatFHIR("2024-06-15T23:30:00-05:00", "instant") // "2024-06-15T23:30:00-05:00"
* @example formatFHIR("2024-06-15T10:30:00", "instant") // "" (an instant needs a zone)
* @example formatFHIR("2024-06-15T10:30:00Z", "dateTime", { precision: "month" }) // "2024-06"
* @example formatFHIR("2024-06", "dateTime", { precision: "day" }) // "" (finer than the value carries)
* @example formatFHIR("2016-12-31T23:59:60Z", "instant") // "" (a leap second)
*/
function formatFHIR(value, type, options) {
try {
if (typeof value !== "string" || !isOptionsArgument(options)) return "";
const plan = planWrite(value, type, options?.precision);
if (plan === null) return "";
const { target, written, rank, fractionDigits, designator, groups } = plan;
const pair = designator === void 0 ? null : readOffsetPair(value, written);
if (designator !== void 0 && pair === null) return "";
let result = writeDigits(readWallClock(groups, pair), target, rank, fractionDigits);
if (rank >= RANK_SECOND && pair !== null) result += keepSpelling(designator, pair.offset);
const check = readFhirValue(result);
return check !== null && fhirTypeAllows(type, check.precision) ? result : "";
} catch {
return "";
}
}
/** The checks that need no Temporal: the form, the precision and the type; null when refused. */
function planWrite(value, type, requested) {
if (requested !== void 0 && !FHIR_PRECISIONS.includes(requested)) return null;
const match = isoHl7Form.exec(value);
if (match === null) return null;
const groups = match.groups;
const { precision: written, fractionDigits } = isoHl7Precision(match);
const designator = groups["offset"];
if (groups["second"] === "60") return null;
const target = pickTarget(type, requested, written);
if (target === null || !fhirTypeAllows(type, target)) return null;
const rank = hl7PrecisionRank(target);
if (rank >= RANK_SECOND && designator === void 0) return null;
return {
target,
written,
rank,
fractionDigits,
designator,
groups
};
}
/**
* The instant and offset `toOffsetInstant` reads (`readFormOffsetInstant`, which also reads an
* hour-only time), or null when it refuses or the offset is not a whole number of minutes (as
* `formatHL7`): Temporal cannot show an instant at an offset with seconds.
*/
function readOffsetPair(value, written) {
const pair = readFormOffsetInstant(value, written);
if (pair === null) return null;
const nanoseconds = parseUtcOffsetNanoseconds(pair.offset);
return nanoseconds === null || nanoseconds % NANOSECONDS_PER_MINUTE !== 0n ? null : pair;
}
/** The wall clock as written: the instant seen at its offset, or the fields with `reject`. */
function readWallClock(groups, pair) {
if (pair !== null) return Temporal.Instant.from(pair.instant).toZonedDateTimeISO(pair.offset).toPlainDateTime();
return Temporal.PlainDateTime.from({
year: Number(groups["year"]),
month: Number(groups["month"] ?? 1),
day: Number(groups["day"] ?? 1),
hour: Number(groups["hour"] ?? 0),
minute: Number(groups["minute"] ?? 0),
second: Number(groups["second"] ?? 0)
}, { overflow: "reject" });
}
/** The precision to write; null when it is finer than the input carries. */
function pickTarget(type, requested, written) {
let target = written;
if (requested !== void 0) target = requested;
else if (type === "date" && hl7PrecisionRank(written) > RANK_DAY) target = "day";
return hl7PrecisionRank(target) > hl7PrecisionRank(written) ? null : target;
}
/**
* The input's own spelling when it names the offset Temporal read (`Z` and `-00:00` name
* `+00:00`), and that offset when it does not.
*/
function keepSpelling(designator, offset) {
return (designator === "Z" || designator === "-00:00" ? "+00:00" : designator) === offset ? designator : offset;
}
/** The digits of the wall clock up to the target: a cut, never a rounding. */
function writeDigits(wallClock, target, rank, fractionDigits) {
const pad = (n, width) => String(n).padStart(width, "0");
let text = pad(wallClock.year, 4);
if (rank >= 1) text += `-${pad(wallClock.month, 2)}`;
if (rank >= RANK_DAY) text += `-${pad(wallClock.day, 2)}`;
if (rank >= RANK_SECOND) text += `T${pad(wallClock.hour, 2)}:${pad(wallClock.minute, 2)}:${pad(wallClock.second, 2)}`;
if (target === "fraction") {
const subSecond = pad(wallClock.millisecond, 3) + pad(wallClock.microsecond, 3) + pad(wallClock.nanosecond, 3);
text += `.${subSecond.slice(0, fractionDigits)}`;
}
return text;
}
export { formatFHIR };