@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.
42 lines (40 loc) • 1.97 kB
JavaScript
import { formatUtcReading, parseUtcReading } from "../../internal/utcReading.js";
import { scaleLabelOfUtc } from "../../internal/gnssTimeScales.js";
import { weeksSince } from "../../internal/gnssWeek.js";
import { RINEX_WEEK_EPOCHS, isRinexWeekSystem } from "../../internal/rinexWeek.js";
// src/gnss/convert/utcToRinexWeek.ts
/**
* Return the RINEX navigation-file week and seconds of week of a UTC instant.
*
* - The inverse of `rinexWeekToUtc`; the week is continuous, so there is no era.
* - GPS, QZSS, NavIC and Galileo weeks count from 1980-01-06; BeiDou weeks from 2006-01-01.
* - `null` before the system's epoch, for an instant past 9999-12-31T23:59:59.999999999Z (the
* inverse writes four-digit years only), and on invalid input.
*
* @param system the RINEX record's system: "GPS", "GAL", "BDT", "QZS" or "IRN"
* @param utcString ISO 8601 instant string; a listed leap second is accepted
* @returns `{ week, timeOfWeekSeconds }`, or null on invalid input, before the system's epoch or past year 9999
*
* @example utcToRinexWeek("IRN", "2024-01-01T00:00:00Z") // { week: 2295, timeOfWeekSeconds: 86418 }
* @example utcToRinexWeek("BDT", "2021-07-05T21:59:56Z") // { week: 809, timeOfWeekSeconds: 165600 }
* @example utcToRinexWeek("GPS", "1980-01-05T23:59:59Z") // null
* @example utcToRinexWeek("GPS", "+010000-01-01T00:00:00Z") // null — past year 9999
*/
function utcToRinexWeek(system, utcString) {
try {
if (!isRinexWeekSystem(system)) return null;
const reading = parseUtcReading(utcString);
if (reading === null || formatUtcReading(reading) === "") return null;
const epoch = RINEX_WEEK_EPOCHS[system];
const label = scaleLabelOfUtc(epoch.system, reading);
if (label === null) return null;
const split = weeksSince(epoch.epochLabel, label.labelNanoseconds);
return split === null ? null : {
week: Number(split.fullWeek),
timeOfWeekSeconds: split.timeOfWeekSeconds
};
} catch {
return null;
}
}
export { utcToRinexWeek };