@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.
59 lines (57 loc) • 2.14 kB
JavaScript
import { Temporal } from "./temporal.js";
import { utcEpochNanoseconds } from "./zonedWallClock.js";
import { isOptionsArgument } from "./isObject.js";
import { twoDigitYear } from "./twoDigitYear.js";
import { readYearWindowStart } from "./yearWindowStart.js";
// src/internal/notamDateTime.ts
const NOTAM_DATE_TIME = /^(?:PERM|([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})(EST)?)$/;
/** Read an item B) or C) value, or null when the shape does not match. */
function readNotamDateTimeFields(value) {
const match = NOTAM_DATE_TIME.exec(value);
if (match === null) return null;
const [, yy, month, day, hour, minute, est] = match;
if (yy === void 0) return { kind: "permanent" };
return {
kind: "dateTime",
yy: Number(yy),
month: Number(month),
day: Number(day),
hour: Number(hour),
minute: Number(minute),
estimated: est !== void 0
};
}
/**
* The core of `parseNotamValidity` and `isValidNotamValidity`: the instant an item B) or C) value
* names, with its two-digit year placed by the caller's `yearWindow` option; `PERM` as itself.
* Never throws.
*
* @example readNotamValidity("2403151200", { yearWindow: 2000 }) // { kind: "instant", instant: "2024-03-15T12:00:00Z", estimated: false }
* @example readNotamValidity("2413151200", { yearWindow: 2000 }) // null
*/
function readNotamValidity(value, options) {
try {
if (typeof value !== "string") return null;
if (!isOptionsArgument(options)) return null;
const fields = readNotamDateTimeFields(value);
if (fields === null) return null;
const windowStart = readYearWindowStart(options);
if (windowStart === null) return null;
if (fields.kind === "permanent") return { kind: "permanent" };
const wall = Temporal.PlainDateTime.from({
year: twoDigitYear(fields.yy, windowStart),
month: fields.month,
day: fields.day,
hour: fields.hour,
minute: fields.minute
}, { overflow: "reject" });
return {
kind: "instant",
instant: Temporal.Instant.fromEpochNanoseconds(utcEpochNanoseconds(wall)).toString(),
estimated: fields.estimated
};
} catch {
return null;
}
}
export { readNotamDateTimeFields, readNotamValidity };