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.

140 lines (138 loc) • 5.48 kB
import { NANOSECONDS_PER_SECOND } from "./foreignEpochs.js"; // src/internal/leapSeconds.ts const ROWS = [ [63072000000000000n, 10n], [78796800000000000n, 11n], [94694400000000000n, 12n], [126230400000000000n, 13n], [157766400000000000n, 14n], [189302400000000000n, 15n], [220924800000000000n, 16n], [252460800000000000n, 17n], [283996800000000000n, 18n], [315532800000000000n, 19n], [362793600000000000n, 20n], [394329600000000000n, 21n], [425865600000000000n, 22n], [489024000000000000n, 23n], [567993600000000000n, 24n], [631152000000000000n, 25n], [662688000000000000n, 26n], [709948800000000000n, 27n], [741484800000000000n, 28n], [773020800000000000n, 29n], [820454400000000000n, 30n], [867715200000000000n, 31n], [915148800000000000n, 32n], [1136073600000000000n, 33n], [1230768000000000000n, 34n], [1341100800000000000n, 35n], [1435708800000000000n, 36n], [1483228800000000000n, 37n] ]; /** * IERS Earth Orientation Center, Leap_Second.dat (https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat), * updated through IERS Bulletin C 72, 6 July 2026. Closed: CGPM 2026 Resolution 5 makes UTC * continuous from 20 May 2027, and Bulletin C 72 rules out the end of December 2026. After the last * row TAI − UTC stays 37 s. Checked weekly against IERS by scripts/leap-seconds.mjs. */ const LEAP_SECOND_STEPS = Object.freeze({ at: Object.freeze(ROWS.map(([at]) => at)), taiMinusUtc: Object.freeze(ROWS.map(([, offset]) => offset)) }); /** Index of the last row that starts at or before `u`, or −1 before the first row. */ function rowAt(steps, u) { let found = -1; for (let index = 0; index < steps.at.length && steps.at[index] <= u; index += 1) found = index; return found; } /** TAI − UTC of row `index`, in nanoseconds. */ function offsetOf(steps, index) { return steps.taiMinusUtc[index] * NANOSECONDS_PER_SECOND; } /** The step from row `index − 1` to row `index`, in nanoseconds: `+S` inserts, `−S` deletes. */ function stepAt(steps, index) { return offsetOf(steps, index) - offsetOf(steps, index - 1); } /** * TAI of a UTC reading, as label nanoseconds (the TAI wall clock read as if UTC), or `null` when * the reading has no TAI instant. * * - `null` before the first row: TAI − UTC was not a whole number of seconds before 1972 (BIPM * Annual Report vol. 15, Table 2). * - A second-60 reading is valid only in the second before a positive step, where it maps to * that inserted second: old offset + 1 s (ITU-R TF.460-6 Annex 1 §2.2). * - An ordinary reading inside a deleted second (before a negative step) has no instant. * - No upper bound: after the last row its offset holds. * * @param steps the leap-second table * @param reading the UTC reading * @returns TAI label nanoseconds, or null */ function taiOfUtc(steps, reading) { const u = reading.epochNanoseconds; if (u < steps.at[0]) return null; if (reading.secondSixty) { for (let index = 1; index < steps.at.length; index += 1) if (steps.at[index] - 1000000000n <= u && u < steps.at[index] && stepAt(steps, index) === 1000000000n) return u + offsetOf(steps, index - 1) + NANOSECONDS_PER_SECOND; return null; } const index = rowAt(steps, u); const next = index + 1; if (next < steps.at.length && stepAt(steps, next) === -1000000000n && u >= steps.at[next] - 1000000000n) return null; return u + offsetOf(steps, index); } /** * The UTC reading of a TAI instant given as label nanoseconds, or `null` before the table's * first TAI instant (1972-01-01T00:00:10 TAI). * * - Inside an inserted second the reading is second 60: its `epochNanoseconds` is the * `…T23:59:59.f` instant Temporal would clamp `…:60.f` to. * - A negative step needs no special case: the deleted second has no TAI instant to map. * * @param steps the leap-second table * @param tai TAI label nanoseconds * @returns the UTC reading, or null */ function utcOfTai(steps, tai) { let index = -1; for (let row = 0; row < steps.at.length && steps.at[row] + offsetOf(steps, row) <= tai; row += 1) index = row; if (index === -1) return null; const u = tai - offsetOf(steps, index); if (index + 1 < steps.at.length && u >= steps.at[index + 1]) return { epochNanoseconds: u - NANOSECONDS_PER_SECOND, secondSixty: true }; return { epochNanoseconds: u, secondSixty: false }; } /** * The UTC reading of step `index`'s own second, the second before `at[index]` (ITU-R TF.460-6 * Annex 1 §2.2): for a positive step the inserted `…T23:59:60` (a second-60 reading); for a * negative step the omitted `…T23:59:59` (an ordinary reading of its label). * * @param steps the leap-second table * @param index a row index from 1 * @returns the reading of that step's second */ function stepReading(steps, index) { return { epochNanoseconds: steps.at[index] - NANOSECONDS_PER_SECOND, secondSixty: stepAt(steps, index) > 0n }; } /** * Index of the first step (row 1 on) that starts after a UTC reading, or −1 when there is none. * A second-60 reading counts as the instant one second later, so its own step is behind it. * * @param steps the leap-second table * @param reading the UTC reading * @returns the row index, or −1 */ function nextStepIndex(steps, reading) { const u = reading.secondSixty ? reading.epochNanoseconds + NANOSECONDS_PER_SECOND : reading.epochNanoseconds; for (let index = 1; index < steps.at.length; index += 1) if (steps.at[index] > u) return index; return -1; } export { LEAP_SECOND_STEPS, nextStepIndex, stepReading, taiOfUtc, utcOfTai };