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.

85 lines (83 loc) • 3.32 kB
import { Temporal } from "./temporal.js"; import { isValidEpoch, utcEpochNanoseconds } from "./zonedWallClock.js"; import { floorDivide } from "./floorDivide.js"; import { parseInstantNanoseconds } from "./instantNanoseconds.js"; import { epochDayOf, isoFieldsOf } from "./epochDay.js"; // src/internal/metarTime.ts const METAR_TIME = /^([0-9]{2})([0-9]{2})([0-9]{2})Z$/; /** Read the fields of a `YYGGggZ` group, or null when the shape does not match. */ function readMetarTimeFields(value) { const match = METAR_TIME.exec(value); if (match === null) return null; const [, dd, hh, mm] = match; return { day: Number(dd), hour: Number(hh), minute: Number(mm) }; } const NS_PER_MINUTE = 60000000000n; const NS_PER_HOUR = 60n * NS_PER_MINUTE; const NS_PER_DAY = 24n * NS_PER_HOUR; /** The instant of `day` `hour`:`minute` UTC in a month, or null when the month lacks the day or the instant is out of range. */ function candidate(year, month, day, timeNs) { const epochDay = epochDayOf(year, month, day); const [y, m, d] = isoFieldsOf(epochDay); if (y !== year || m !== month || d !== day) return null; const ns = BigInt(epochDay) * NS_PER_DAY + timeNs; return isValidEpoch(ns) ? ns : null; } /** * The core of `parseMetarTime` and `isValidMetarTime`: the instant a `YYGGggZ` group names, in the * month nearest `referenceInstant` that has the day, or `""`. Never throws. * * @example readMetarTime("151200Z", "2024-03-16T00:00:00Z") // "2024-03-15T12:00:00Z" * @example readMetarTime("151260Z", "2024-03-16T00:00:00Z") // "" */ function readMetarTime(value, referenceInstant) { try { if (typeof value !== "string" || typeof referenceInstant !== "string") return ""; const fields = readMetarTimeFields(value); if (fields === null) return ""; const { day, hour, minute } = fields; if (day < 1 || day > 31) return ""; Temporal.PlainTime.from({ hour, minute }, { overflow: "reject" }); const reference = parseInstantNanoseconds(referenceInstant); if (reference === null) return ""; const timeNs = BigInt(hour) * NS_PER_HOUR + BigInt(minute) * NS_PER_MINUTE; const [year, month] = isoFieldsOf(Number(floorDivide(reference, NS_PER_DAY))); let before = null; for (let k = 0; k >= -2 && before === null; k--) { const index = year * 12 + (month - 1) + k; const c = candidate(Math.floor(index / 12), (index % 12 + 12) % 12 + 1, day, timeNs); if (c !== null && c <= reference) before = c; } let after = null; for (let k = 0; k <= 2 && after === null; k++) { const index = year * 12 + (month - 1) + k; const c = candidate(Math.floor(index / 12), (index % 12 + 12) % 12 + 1, day, timeNs); if (c !== null && c > reference) after = c; } let chosen; if (before !== null && after !== null) chosen = reference - before <= after - reference ? before : after; else if (before !== null) chosen = before; else if (after !== null) chosen = after; else return ""; const chosenDay = Number(floorDivide(chosen, NS_PER_DAY)); const [y, m, d] = isoFieldsOf(chosenDay); const wall = Temporal.PlainDateTime.from({ year: y, month: m, day: d, hour, minute }, { overflow: "reject" }); return Temporal.Instant.fromEpochNanoseconds(utcEpochNanoseconds(wall)).toString(); } catch { return ""; } } export { readMetarTime, readMetarTimeFields };