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.

204 lines (202 loc) • 8.16 kB
import { nowInstant } from "./temporalCompat/clock.js"; import { isObject } from "./isObject.js"; import { NANOSECONDS_PER_SECOND, NANOSECONDS_PER_WEEK } from "./foreignEpochs.js"; import { parseUtcReading } from "./utcReading.js"; import { LEAP_SECOND_STEPS, taiOfUtc } from "./leapSeconds.js"; import { GNSS_TIME_SCALES, scaleLabelOfUtc } from "./gnssTimeScales.js"; // src/internal/gnssWeek.ts /** * The broadcast week counters, each with exactly one width. GLONASS broadcasts no week number, * so it has none. */ const GNSS_WEEK_COUNTERS = Object.freeze({ "GPS-LNAV": Object.freeze({ system: "GPS", modulus: 1024n }), "GPS-CNAV": Object.freeze({ system: "GPS", modulus: 8192n }), GAL: Object.freeze({ system: "GAL", modulus: 4096n }), BDT: Object.freeze({ system: "BDT", modulus: 8192n }), "QZS-LNAV": Object.freeze({ system: "QZS", modulus: 1024n }), "QZS-CNAV": Object.freeze({ system: "QZS", modulus: 8192n }), "IRN-LNAV": Object.freeze({ system: "IRN", modulus: 1024n }), "IRN-L1NV": Object.freeze({ system: "IRN", modulus: 8192n }) }); /** * Whether a value names one of the eight broadcast week counters. * * @param value candidate counter * @returns true for a `GnssWeekCounter` member */ function isGnssWeekCounter(value) { return typeof value === "string" && Object.hasOwn(GNSS_WEEK_COUNTERS, value); } /** * A week number as a bigint: any safe integer from 0. The caller checks a counter's modulus. * * @param week candidate week number * @returns the week, or null when it is not a safe integer ≥ 0 */ function weekNumber(week) { return Number.isSafeInteger(week) && week >= 0 ? BigInt(week) : null; } /** Nanoseconds in one second, as a `number`, for reading a seconds-of-week `number`. */ const NANOSECONDS_PER_SECOND_NUMBER = Number(NANOSECONDS_PER_SECOND); /** * Seconds of week as nanoseconds, rounded to the nearest nanosecond (TC39 Temporal's resolution; * a GMT rule). Valid from 0 up to but not including one week, 604 800 s (IS-GPS-200N §3.3.4). * * @param timeOfWeekSeconds candidate seconds of week * @returns the time of week in nanoseconds, or null when it is not a finite number in range or * rounds to a whole week */ function timeOfWeekNanoseconds(timeOfWeekSeconds) { if (typeof timeOfWeekSeconds !== "number" || !Number.isFinite(timeOfWeekSeconds) || timeOfWeekSeconds < 0) return null; const nanoseconds = BigInt(Math.round(timeOfWeekSeconds * NANOSECONDS_PER_SECOND_NUMBER)); return nanoseconds < 604800000000000n ? nanoseconds : null; } /** * The label `fullWeek` weeks and `timeOfWeek` nanoseconds after an epoch label. * * @param epochLabel week 0's first label, label nanoseconds * @param fullWeek weeks since the epoch, never wrapped * @param timeOfWeek nanoseconds into that week * @returns the label, label nanoseconds */ function labelOfFullWeek(epochLabel, fullWeek, timeOfWeek) { return epochLabel + fullWeek * NANOSECONDS_PER_WEEK + timeOfWeek; } /** * Split a label into whole weeks since an epoch label and seconds into the week. The seconds are * a `number`: the nearest double to the exact nanosecond count. * * @param epochLabel week 0's first label, label nanoseconds * @param labelNanoseconds the label to split * @returns the full week and seconds of week, or null before the epoch */ function weeksSince(epochLabel, labelNanoseconds) { const elapsed = labelNanoseconds - epochLabel; if (elapsed < 0n) return null; return { fullWeek: elapsed / NANOSECONDS_PER_WEEK, timeOfWeekSeconds: Number(elapsed % NANOSECONDS_PER_WEEK) / NANOSECONDS_PER_SECOND_NUMBER }; } /** * The era that puts a week within half a counter period of the current one: * `Fnow − M/2 < F ≤ Fnow + M/2`, clamped at era 0 (a GMT rule, after the rolling two-digit-year * window). Reads `nowInstant()` once; call it inside `try`, as the clock read can throw. * * @param counter the week counter * @param week the week number the counter shows * @returns the era, or null when the clock reads before the counter's epoch */ function rollingEra(counter, week) { const { system, modulus } = GNSS_WEEK_COUNTERS[counter]; const now = scaleLabelOfUtc(system, { epochNanoseconds: nowInstant().epochNanoseconds, secondSixty: false }); if (now === null) return null; const numerator = (now.labelNanoseconds - GNSS_TIME_SCALES[system].epochLabel) / NANOSECONDS_PER_WEEK - week + modulus / 2n; return numerator < 0n ? 0n : numerator / modulus; } /** * The era of the earliest occurrence of a week time whose label is at or after a pivot's label: * `Lp ≤ L < Lp + period`. Labels are compared, not UTC instants: every scale with a week counter * is continuous, so label order is instant order and a listed leap second keeps its place. * * @param counter the week counter * @param week the week number the counter shows * @param timeOfWeek nanoseconds into the week * @param notBefore the pivot, an ISO 8601 UTC instant string; a listed second 60 is accepted * @returns the era (0 for a pivot before the counter's epoch), or null when the pivot is not an * instant string or is a second 60 the leap-second table does not list */ function pivotEra(counter, week, timeOfWeek, notBefore) { const reading = parseUtcReading(notBefore); if (reading === null) return null; const { system, modulus } = GNSS_WEEK_COUNTERS[counter]; const pivot = scaleLabelOfUtc(system, reading); if (pivot === null) return taiOfUtc(LEAP_SECOND_STEPS, reading) === null && reading.secondSixty ? null : 0n; const eraZero = labelOfWeek(counter, week, timeOfWeek, 0n); if (pivot.labelNanoseconds <= eraZero) return 0n; const period = modulus * NANOSECONDS_PER_WEEK; return (pivot.labelNanoseconds - eraZero + period - 1n) / period; } /** * The era a week number belongs to. * * - An integer from 0 is the era itself. * - `"rolling"` reads `nowInstant()` once and picks the era within half a counter * period of the current week, clamped at era 0. Call it inside `try`: the clock read can throw. * - `{ notBefore }` reads `notBefore` once and picks the earliest occurrence at or after it, so a * pivot before the counter's epoch gives era 0; other keys are ignored. It never reads the clock. * * @param counter the week counter * @param week the week number the counter shows * @param timeOfWeek nanoseconds into the week * @param rolloverEra an integer era from 0, "rolling", or `{ notBefore }` * @returns the era, or null for any other value, when the clock reads before the counter's epoch, * or for a pivot that is not an instant string or is an unlisted second 60 */ function resolveRolloverEra(counter, week, timeOfWeek, rolloverEra) { if (rolloverEra === "rolling") return rollingEra(counter, week); if (isObject(rolloverEra)) { const { notBefore } = rolloverEra; return pivotEra(counter, week, timeOfWeek, notBefore); } return weekNumber(rolloverEra); } /** * The label of a week time on a counter: epoch + (era × modulus + week) weeks + time of week. * * @param counter the week counter * @param week the week number the counter shows * @param timeOfWeek nanoseconds into the week * @param era how many times the counter has wrapped * @returns the label on the counter's scale, label nanoseconds */ function labelOfWeek(counter, week, timeOfWeek, era) { const { system, modulus } = GNSS_WEEK_COUNTERS[counter]; return labelOfFullWeek(GNSS_TIME_SCALES[system].epochLabel, era * modulus + week, timeOfWeek); } /** * The week, time of week and era of a label on a counter's scale. * * @param counter the week counter * @param labelNanoseconds a label on the counter's scale * @returns a new `GnssWeek`, or null before the counter's epoch */ function weekOfLabel(counter, labelNanoseconds) { const { system, modulus } = GNSS_WEEK_COUNTERS[counter]; const split = weeksSince(GNSS_TIME_SCALES[system].epochLabel, labelNanoseconds); if (split === null) return null; return { week: Number(split.fullWeek % modulus), timeOfWeekSeconds: split.timeOfWeekSeconds, era: Number(split.fullWeek / modulus) }; } export { GNSS_WEEK_COUNTERS, isGnssWeekCounter, labelOfFullWeek, labelOfWeek, resolveRolloverEra, timeOfWeekNanoseconds, weekNumber, weekOfLabel, weeksSince };