@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
JavaScript
import { NANOSECONDS_PER_SECOND } from "./foreignEpochs.js";
// src/internal/leapSeconds.ts
const ROWS = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
];
/**
* 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 };