@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.
147 lines • 7.21 kB
TypeScript
import { JulianDateParts, TimeScale } from "../types/time-scale.js";
import { UtcReading } from "./utcReading.js";
import { LeapSecondSteps } from "./leapSeconds.js";
/**
* The core of the Julian Date family: a UTC reading on one of four scales as a position in a day,
* and back. Pure bigint and number arithmetic: no Temporal call, nothing at import. The only
* loops are bounded: the leap-second scans (the 28-row table) and the trailing-zero reduction in
* `exactDyadic` (at most 52 iterations, a 53-bit significand).
*
* Sources: IAU 1997 Resolution B1 Appendix 2 (JD counts days from noon); ERFA `dtf2d.c` Notes 3-4
* (quasi-JD on UTC); IEEE 754-2019 §4.3 (one correctly rounded division); TC39 Temporal
* `halfExpand` (the nanosecond rounding).
*/
/** MJD of 1970-01-01: the days from 1858-11-17 (ITU-R TF.457-2 Rec. 2; ERFA `ERFA_DJM0`). */
export declare const MJD_OF_UNIX_EPOCH = 40587n;
/** Twice 2440587.5, the JD of 1970-01-01T00:00 (ERFA `ERFA_DJM0` with MJD 40587). */
export declare const JD_OF_UNIX_EPOCH_MIDNIGHT_HALF_DAYS = 4881175n;
/** Days from 1970-01-01 to 2000-01-01 (J2000.0 is at noon of this day, IAU 1994 C7 Rec. 1). */
export declare const J2000_EPOCH_DAY = 10957n;
/** 1972-01-01T00:00:00Z: where whole-second TAI − UTC begins, the lower bound of the domain. */
export declare const UTC_JD_FLOOR_NANOSECONDS: bigint;
/** A position in a day on one scale's own label. */
export interface DayPosition {
/** Days since 1970-01-01 on the scale's own label. */
readonly epochDay: bigint;
/** Nanoseconds into the day, from 0 up to but not including `dayLength`. */
readonly nanosOfDay: bigint;
/** `NANOSECONDS_PER_DAY`, or 86 401 s on a UTC leap-second day. */
readonly dayLength: bigint;
}
/**
* Whether a value is one of the four time scales a Julian Date can be taken on.
*
* @param value candidate scale
* @returns true for `UTC`, `TAI`, `TT` or `TCG`
*/
export declare function isTimeScale(value: unknown): value is TimeScale;
/**
* A UTC reading as a position in a day on a scale.
*
* On TAI, TT and TCG the day is the scale's own label day, 86 400 s long. On UTC it is the UTC
* day, which holds 86 401 s when a second is inserted at its end (ERFA `dtf2d.c` Note 4).
*
* @param steps the leap-second table
* @param reading the UTC reading
* @param scale the scale to count on
* @returns the day position, or null outside 1972-01-01 to 9999-12-31 or for a reading that has no TAI instant
*/
export declare function dayPositionOf(steps: LeapSecondSteps, reading: UtcReading, scale: TimeScale): DayPosition | null;
/**
* The UTC reading of a day position: the inverse of `dayPositionOf`.
*
* @param steps the leap-second table
* @param epochDay days since 1970-01-01 on the scale's label
* @param nanosOfDay rounded nanoseconds into the day, below the day's length
* @param scale the scale the position is on
* @returns the UTC reading, or null before 1972-01-01 (the caller's `formatUtcReading` bounds the other end)
*/
export declare function readingOfDayPosition(steps: LeapSecondSteps, epochDay: bigint, nanosOfDay: bigint, scale: TimeScale): UtcReading | null;
/**
* A finite double as `numerator / 2^exponent`, exactly, with `exponent ≥ 0` and the fraction
* reduced (an odd numerator unless the exponent is 0). Reads the sign, the 11-bit exponent and the
* 52-bit significand (IEEE 754-2019 §3.4); handles subnormals and ±0. Non-finite values are the
* caller's to reject.
*
* @param x a finite number
* @returns the exact numerator and power-of-two exponent
*/
export declare function exactDyadic(x: number): {
numerator: bigint;
exponent: bigint;
};
/**
* The double nearest the exact rational `numerator / denominator`, ties to the even significand
* (IEEE 754-2019 §4.3.1 roundTiesToEven): the one rounding the J2000-seconds output takes.
*
* Why it is exact: with `e = floor(log2(|numerator| / denominator))`, the doubles in `[2^e, 2^(e+1))`
* are exactly `j × 2^-k` for whole `j` in `[2^52, 2^53)`, where `k = 52 − e`. So the nearest double is
* `round(x) × 2^-k` with `x = |numerator| × 2^k / denominator` in `[2^52, 2^53)`, rounded half to
* even once, in bigint. A round up to `2^53` is `2^(e+1)`, the next binade's first double, whose
* significand is even. `j ≤ 2^53` converts to a double exactly, and scaling by a power of two is exact
* while the result stays normal, so no second rounding happens.
*
* The result must be normal (`2^-1022 ≤ |value| < 2^1024`); the J2000 counts, from 1 ns to about
* 2.6 × 10^11 s, are.
*
* @param numerator any bigint
* @param denominator a positive bigint
* @returns the correctly rounded double, `0` for a zero numerator
*/
export declare function nearestDouble(numerator: bigint, denominator: bigint): number;
/**
* Days since 1970-01-01T00:00 on a scale, given as an exact fraction, as a day position rounded to
* the nearest nanosecond (a half rounds up).
*
* @param daysNumerator numerator of the day count
* @param daysDenominator denominator of the day count, positive
* @param steps the leap-second table
* @param scale the scale the count is on
* @returns the day position, or null when the day lies outside 1971-12-31 to 10000-01-01
*/
export declare function dayPositionOfDays(daysNumerator: bigint, daysDenominator: bigint, steps: LeapSecondSteps, scale: TimeScale): Omit<DayPosition, "dayLength"> | null;
/** Days since 1970-01-01T00:00 on a scale's label, as the exact fraction `numerator / denominator`. */
export interface EpochDays {
readonly numerator: bigint;
readonly denominator: bigint;
}
/**
* The exact days since 1970-01-01T00:00 that a Julian Date double names (JD − 2440587.5).
*
* @param jd a finite number
* @returns the exact day count
*/
export declare function epochDaysOfJulianDate(jd: number): EpochDays;
/**
* The exact days since 1970-01-01T00:00 that a Modified Julian Date double names (MJD − 40587).
*
* @param mjd a finite number
* @returns the exact day count
*/
export declare function epochDaysOfModifiedJulianDate(mjd: number): EpochDays;
/**
* The exact days since 1970-01-01T00:00 that a count of seconds since J2000.0 names.
*
* @param seconds a finite number
* @returns the exact day count
*/
export declare function epochDaysOfJ2000Seconds(seconds: number): EpochDays;
/**
* Whether a day count reads back as a UTC instant inside the domain (1972-01-01T00:00:00Z to
* 9999-12-31T23:59:59.999999999Z) on a scale: the same path every `from*` takes, so a `to*` can
* refuse a double its `from*` would reject.
*
* @param days the exact day count
* @param steps the leap-second table
* @param scale the scale the count is on
* @returns true when the day count reads as an instant in the domain
*/
export declare function readsBackInDomain(days: EpochDays, steps: LeapSecondSteps, scale: TimeScale): boolean;
/**
* A day position as two-part Julian Date: `jd1` a whole number, `jd2` in [0, 1), split at noon
* (IAU 1997 B1 App. 2: the JD counts from the preceding noon). `jd2` is one division.
*
* @param position the day position
* @returns the two parts
*/
export declare function julianDatePartsOf(position: DayPosition): JulianDateParts;