@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.
100 lines (98 loc) • 3.62 kB
JavaScript
import { NANOSECONDS_PER_SECOND } from "./foreignEpochs.js";
import { LEAP_SECOND_STEPS, taiOfUtc, utcOfTai } from "./leapSeconds.js";
// src/internal/gnssTimeScales.ts
/**
* The satellite time scales by RINEX 4.02 §5.2.8 time-system identifier. Each `epochLabel` is
* `Temporal.PlainDateTime.from(<label>).toZonedDateTime("UTC").epochNanoseconds` of the label in
* its comment; gnssTimeScales.test.ts asserts each one. The offsets are nominal: the broadcast
* corrections (C0, A0/A1, GGTO), tens of nanoseconds, are not inputs.
*/
const GNSS_TIME_SCALES = Object.freeze({
GPS: Object.freeze({
taiMinusScale: 19n,
epochLabel: 315964800000000000n
}),
GAL: Object.freeze({
taiMinusScale: 19n,
epochLabel: 935280000000000000n
}),
BDT: Object.freeze({
taiMinusScale: 33n,
epochLabel: 1136073600000000000n
}),
QZS: Object.freeze({
taiMinusScale: 19n,
epochLabel: 315964800000000000n
}),
GLO: Object.freeze({
taiMinusScale: null,
epochLabel: 63072000000000000n
}),
IRN: Object.freeze({
taiMinusScale: 19n,
epochLabel: 935280000000000000n
})
});
/**
* Whether a value is one of the six RINEX 4.02 §5.2.8 time-system identifiers GMT reads.
*
* @param value candidate system
* @returns true for `GPS`, `GAL`, `BDT`, `QZS`, `GLO` or `IRN`
*/
function isGnssSystem(value) {
return typeof value === "string" && Object.hasOwn(GNSS_TIME_SCALES, value);
}
/**
* A UTC reading as a label on a satellite system's scale.
*
* - TAI-based scales: TAI from the leap-second table, minus the scale's fixed offset. A listed
* inserted second maps to its own label; the result is never second 60.
* - `GLO`: the reading itself, second 60 included, once the table has checked it.
* - `null` when the reading has no TAI instant (before 1972, or a second 60 on a day with no
* insertion), or when the label falls before the scale's epoch.
*
* @param system the satellite time scale
* @param reading the UTC reading
* @returns the scale's label, or null
*/
function scaleLabelOfUtc(system, reading) {
const scale = GNSS_TIME_SCALES[system];
const tai = taiOfUtc(LEAP_SECOND_STEPS, reading);
if (tai === null) return null;
const label = scale.taiMinusScale === null ? {
labelNanoseconds: reading.epochNanoseconds,
secondSixty: reading.secondSixty
} : {
labelNanoseconds: tai - scale.taiMinusScale * NANOSECONDS_PER_SECOND,
secondSixty: false
};
return label.labelNanoseconds < scale.epochLabel ? null : label;
}
/**
* A label on a satellite system's scale as a UTC reading.
*
* - TAI-based scales: the label plus the scale's fixed offset is TAI, read back through the
* leap-second table; inside an inserted second the reading is second 60.
* - `GLO`: the label itself is the UTC reading; a second 60 stands only where the table lists an
* insertion.
* - `null` before the scale's epoch, for a second-60 label on a continuous scale, and for a `GLO`
* second 60 on a day with no insertion.
*
* @param system the satellite time scale
* @param label the scale's label
* @returns the UTC reading, or null
*/
function utcOfScaleLabel(system, label) {
const scale = GNSS_TIME_SCALES[system];
if (label.labelNanoseconds < scale.epochLabel) return null;
if (scale.taiMinusScale === null) {
const reading = {
epochNanoseconds: label.labelNanoseconds,
secondSixty: label.secondSixty
};
return taiOfUtc(LEAP_SECOND_STEPS, reading) === null ? null : reading;
}
if (label.secondSixty) return null;
return utcOfTai(LEAP_SECOND_STEPS, label.labelNanoseconds + scale.taiMinusScale * NANOSECONDS_PER_SECOND);
}
export { GNSS_TIME_SCALES, isGnssSystem, scaleLabelOfUtc, utcOfScaleLabel };