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.

58 lines (56 loc) • 3.53 kB
import { NANOSECONDS_PER_DAY, NANOSECONDS_PER_SECOND } from "../../internal/foreignEpochs.js"; import { LEAP_SECOND_STEPS } from "../../internal/leapSeconds.js"; import { parseUtcReading } from "../../internal/utcReading.js"; import { J2000_EPOCH_DAY, dayPositionOf, epochDaysOfJ2000Seconds, isTimeScale, nearestDouble, readsBackInDomain } from "../../internal/julianDate.js"; // src/timescale/convert/toJ2000Seconds.ts /** * Convert a UTC instant to seconds since J2000.0 on a time scale. **The scale defaults to * `"TT"`, unlike the Julian Date and MJD functions, which default to `"UTC"`**: J2000.0 is * defined as 2000-01-01T12:00:00 TT (IAU 1994 Resolution C7). * * - The result is (JD on the scale − 2451545.0) × 86 400 (ERFA `ERFA_DJ00`, `ERFA_DAYSEC`). * On `"TT"` it counts TT seconds from J2000.0 itself; on `"TAI"` and `"TCG"` it counts from * the label `2000-01-01T12:00:00` on that scale. * - On `"UTC"` it is the quasi-JD count (ERFA `dtf2d.c` Notes 4 and 7). On every day without a * leap second it equals Unix seconds minus 946 728 000. On a day with one, the 86 401 s are * scaled to fill 86 400, so, like Unix time, the difference between two UTC values is not an * exact interval. * - TDB is not available, so this is not the "ephemeris seconds past J2000", which count on TDB. * - Precision: the result is the double nearest the exact count (rounded once, ties to even), so * the error is at most half an ulp of the result. Seconds since J2000 in one double keep * a spacing (ulp) of at most 2⁻²³ s (0.12 µs) for |seconds| < 2³⁰ (about 1966 to 2034), so a * value is off by at most 0.06 µs (half an ulp), and a spacing of 30.5 µs at year 9999, a * value off by at most 15 µs. Callers * needing nanoseconds should use `toJulianDateParts`. * - Accepts any offset or bracketed zone, and a listed leap second `…T23:59:60Z`. * - Domain: 1972-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z UTC; `null` outside it. Within * about 15 µs (half an ulp) of the end of 9999 (on UTC, TAI and TCG) it also returns `null`, because the * nearest double lies past the domain and `fromJ2000Seconds` would reject it; * `toJulianDateParts` covers the whole domain exactly. * * @param isoString ISO 8601 instant string * @param scale the time scale to count on, default `"TT"` * @returns seconds since J2000.0 (negative before it), or null on invalid input, an unknown scale or an instant outside the domain * * @example toJ2000Seconds("2000-01-01T11:58:55.816Z") // 0 — J2000.0 itself * @example toJ2000Seconds("2000-01-01T12:00:00Z") // 64.184 * @example toJ2000Seconds("2000-01-01T12:00:00Z", "UTC") // 0 * @example toJ2000Seconds("2026-01-01T00:00:00Z", "TAI") // 820497637 * @example toJ2000Seconds("1971-12-31T23:59:59Z") // null — before 1972 */ function toJ2000Seconds(isoString, scale = "TT") { try { if (!isTimeScale(scale)) return null; const reading = parseUtcReading(isoString); if (reading === null) return null; const position = dayPositionOf(LEAP_SECOND_STEPS, reading, scale); if (position === null) return null; const { epochDay, nanosOfDay, dayLength } = position; const start = (epochDay - J2000_EPOCH_DAY) * NANOSECONDS_PER_DAY - NANOSECONDS_PER_DAY / 2n; const seconds = nearestDouble(start * dayLength + nanosOfDay * NANOSECONDS_PER_DAY, dayLength * NANOSECONDS_PER_SECOND); return readsBackInDomain(epochDaysOfJ2000Seconds(seconds), LEAP_SECOND_STEPS, scale) ? seconds : null; } catch { return null; } } export { toJ2000Seconds };