@interstellar-tools/temporal 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +804 -0
- package/dist/convert-temporal-unit.d.ts +9 -8
- package/dist/convert-temporal-unit.d.ts.map +1 -1
- package/dist/convert-temporal-unit.js +39 -14
- package/dist/convert-temporal-unit.js.map +1 -1
- package/dist/normalize-temporal-unit.d.ts +2 -0
- package/dist/normalize-temporal-unit.d.ts.map +1 -1
- package/dist/normalize-temporal-unit.js +6 -1
- package/dist/normalize-temporal-unit.js.map +1 -1
- package/dist/time-scales.d.ts.map +1 -1
- package/dist/time-scales.js +4 -0
- package/dist/time-scales.js.map +1 -1
- package/package.json +6 -3
- package/src/convert-temporal-unit.ts +141 -0
- package/src/index.ts +7 -0
- package/src/mars-time.ts +204 -0
- package/src/normalize-temporal-unit.ts +142 -0
- package/src/time-scales.ts +291 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TemporalUnitAliasType,
|
|
3
|
+
TemporalUnitType
|
|
4
|
+
} from '@interstellar-tools/types';
|
|
5
|
+
|
|
6
|
+
/* node:coverage disable */
|
|
7
|
+
/**
|
|
8
|
+
* Lookup table that **normalizes user-facing time unit aliases** (and the canonical
|
|
9
|
+
* symbols themselves) to a single, canonical {@link TemporalUnitType}.
|
|
10
|
+
*
|
|
11
|
+
* ## Purpose
|
|
12
|
+
* - Centralizes all **string → symbol** normalization (e.g., `"seconds"` → `"s"`, `"us"` → `"μs"`).
|
|
13
|
+
* - Ensures downstream math uses only **canonical units** (`s`, `ms`, `μs`, `min`, `h`, `d`, `yr`, `kyr`, `Myr`, `Gyr`).
|
|
14
|
+
*
|
|
15
|
+
* ## Conventions & semantics
|
|
16
|
+
* - **Durations only** (no timestamp/"ago" meaning). Geology-style tokens (`Ma`, `Ga`, `megaannum`, `gigaannum`)
|
|
17
|
+
* are treated as **durations** and normalized to `Myr`/`Gyr`.
|
|
18
|
+
* - **Day** is [SI](https://en.wikipedia.org/wiki/International_System_of_Units) day: exactly **86 400 s**.
|
|
19
|
+
* - **Year family** (`yr`, `kyr`, `Myr`, `Gyr`) is the **Julian year** convention: **365.25 d = 31 557 600 s**.
|
|
20
|
+
* - **Case-sensitive** and exact matching. The ASCII alias `"us"` is accepted and normalized to the Unicode `μs`.
|
|
21
|
+
*
|
|
22
|
+
* ## Usage pattern
|
|
23
|
+
* - Accept `TemporalUnitAliasType | TemporalUnitType` at your boundaries.
|
|
24
|
+
* - Normalize with this map.
|
|
25
|
+
* - Store/compute exclusively with canonical {@link TemporalUnitType}.
|
|
26
|
+
*
|
|
27
|
+
* The table is frozen: it can be read, but not changed at runtime.
|
|
28
|
+
*
|
|
29
|
+
* @see https://www.bipm.org/en/publications/si-brochure ([SI](https://en.wikipedia.org/wiki/International_System_of_Units) Brochure - second & day)
|
|
30
|
+
* @see https://en.wikipedia.org/wiki/Julian_year_(astronomy) (Julian year for yr/kyr/Myr/Gyr)
|
|
31
|
+
*/
|
|
32
|
+
/* node:coverage enable */
|
|
33
|
+
export const NORMALIZE_TEMPORAL_UNIT = Object.freeze({
|
|
34
|
+
// canonical already map to themselves:
|
|
35
|
+
s: 's',
|
|
36
|
+
ms: 'ms',
|
|
37
|
+
μs: 'μs',
|
|
38
|
+
ns: 'ns',
|
|
39
|
+
ps: 'ps',
|
|
40
|
+
fs: 'fs',
|
|
41
|
+
as: 'as',
|
|
42
|
+
zs: 'zs',
|
|
43
|
+
ys: 'ys',
|
|
44
|
+
min: 'min',
|
|
45
|
+
h: 'h',
|
|
46
|
+
d: 'd',
|
|
47
|
+
yr: 'yr',
|
|
48
|
+
kyr: 'kyr',
|
|
49
|
+
Myr: 'Myr',
|
|
50
|
+
Gyr: 'Gyr',
|
|
51
|
+
|
|
52
|
+
// aliases → canonical
|
|
53
|
+
second: 's',
|
|
54
|
+
seconds: 's',
|
|
55
|
+
millisecond: 'ms',
|
|
56
|
+
milliseconds: 'ms',
|
|
57
|
+
microsecond: 'μs',
|
|
58
|
+
microseconds: 'μs',
|
|
59
|
+
us: 'μs',
|
|
60
|
+
minute: 'min',
|
|
61
|
+
minutes: 'min',
|
|
62
|
+
hour: 'h',
|
|
63
|
+
hours: 'h',
|
|
64
|
+
day: 'd',
|
|
65
|
+
days: 'd',
|
|
66
|
+
year: 'yr',
|
|
67
|
+
years: 'yr',
|
|
68
|
+
yrs: 'yr',
|
|
69
|
+
|
|
70
|
+
kiloyear: 'kyr',
|
|
71
|
+
kiloyears: 'kyr',
|
|
72
|
+
megayear: 'Myr',
|
|
73
|
+
megayears: 'Myr',
|
|
74
|
+
megaannum: 'Myr',
|
|
75
|
+
megaannums: 'Myr',
|
|
76
|
+
Ma: 'Myr',
|
|
77
|
+
gigayear: 'Gyr',
|
|
78
|
+
gigayears: 'Gyr',
|
|
79
|
+
gigaannum: 'Gyr',
|
|
80
|
+
gigaannums: 'Gyr',
|
|
81
|
+
Ga: 'Gyr'
|
|
82
|
+
} as const satisfies Record<
|
|
83
|
+
TemporalUnitAliasType | TemporalUnitType,
|
|
84
|
+
TemporalUnitType
|
|
85
|
+
>);
|
|
86
|
+
|
|
87
|
+
/* node:coverage disable */
|
|
88
|
+
/**
|
|
89
|
+
* Normalize a user-provided **temporal unit string** into a canonical {@link TemporalUnitType}.
|
|
90
|
+
*
|
|
91
|
+
* - Trims surrounding whitespace.
|
|
92
|
+
* - Accepts both **canonical symbols** (e.g., `"s"`, `"ms"`, `"μs"`, `"min"`, `"h"`, `"d"`, `"yr"`, `"kyr"`, `"Myr"`, `"Gyr"`)
|
|
93
|
+
* and **aliases** (e.g., `"seconds"`, `"milliseconds"`, `"us"`, `"hours"`, `"years"`, `"Ma"`, `"Ga"`),
|
|
94
|
+
* using {@link NORMALIZE_TEMPORAL_UNIT} as the single source of truth.
|
|
95
|
+
* - Returns a **canonical** unit suitable for arithmetic, storage, and serialization.
|
|
96
|
+
*
|
|
97
|
+
* ### Conventions
|
|
98
|
+
* - **Durations only** (no timestamp or "ago" semantics). Geology-style tokens like `"Ma"`/`"Ga"` are
|
|
99
|
+
* interpreted as durations and normalized to `"Myr"`/`"Gyr"`.
|
|
100
|
+
* - **Case-sensitive** exact match against {@link NORMALIZE_TEMPORAL_UNIT}. For example, `"Seconds"` (capital S)
|
|
101
|
+
* will fail; use `"seconds"` instead, or pre-normalize casing upstream if desired.
|
|
102
|
+
* - The ASCII alias `"us"` is accepted and normalized to the Unicode microsecond symbol `"μs"`.
|
|
103
|
+
* - The input is NFKC-normalized, so the micro sign `"µs"` (U+00B5, as keyboards type it) is read as `"μs"` (U+03BC).
|
|
104
|
+
*
|
|
105
|
+
* @param u - The input unit string. Leading/trailing whitespace is ignored; content is **case-sensitive**.
|
|
106
|
+
* @returns The canonical {@link TemporalUnitType} (e.g., `"s"`, `"ms"`, `"μs"`, `"min"`, `"h"`, `"d"`, `"yr"`, `"kyr"`, `"Myr"`, `"Gyr"`).
|
|
107
|
+
*
|
|
108
|
+
* @throws {TypeError} If `u` is not a string.
|
|
109
|
+
* @throws {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError | RangeError} If `u` does not match any known key in {@link NORMALIZE_TEMPORAL_UNIT}.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* // Canonical conversion using the normalized unit:
|
|
114
|
+
* const unit = normalizeTemporalUnit(' seconds '); // → 's'
|
|
115
|
+
* const valueInSeconds = 2 * (unit === 's' ? 1 : NaN); // 2
|
|
116
|
+
*
|
|
117
|
+
* normalizeTemporalUnit('us'); // → 'μs'
|
|
118
|
+
* normalizeTemporalUnit('Myr'); // → 'Myr' (already canonical)
|
|
119
|
+
* normalizeTemporalUnit('Ma'); // → 'Myr' (geology-style alias)
|
|
120
|
+
* normalizeTemporalUnit('Ga'); // → 'Gyr'
|
|
121
|
+
* // normalizeTemporalUnit('Seconds'); // throws (case-sensitive)
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @see {@link NORMALIZE_TEMPORAL_UNIT} for the complete alias→canonical mapping.
|
|
125
|
+
* @see https://www.bipm.org/en/publications/si-brochure (SI Brochure - second/day)
|
|
126
|
+
* @see https://en.wikipedia.org/wiki/Julian_year_(astronomy) (Julian year for yr/kyr/Myr/Gyr)
|
|
127
|
+
*/
|
|
128
|
+
/* node:coverage enable */
|
|
129
|
+
export const normalizeTemporalUnit = (u: string): TemporalUnitType => {
|
|
130
|
+
if (typeof u !== 'string')
|
|
131
|
+
throw new TypeError(`unit must be a string. Received: ${String(u)}`);
|
|
132
|
+
|
|
133
|
+
// NFKC turns the micro sign, U+00B5, into the Greek mu the table uses
|
|
134
|
+
const k = u.trim().normalize('NFKC');
|
|
135
|
+
const v = Object.prototype.hasOwnProperty.call(NORMALIZE_TEMPORAL_UNIT, k)
|
|
136
|
+
? NORMALIZE_TEMPORAL_UNIT[k as keyof typeof NORMALIZE_TEMPORAL_UNIT]
|
|
137
|
+
: undefined;
|
|
138
|
+
|
|
139
|
+
if (!v) throw new RangeError(`Unsupported unit: ${u}`);
|
|
140
|
+
|
|
141
|
+
return v;
|
|
142
|
+
};
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JULIAN_DATE_UNIX_EPOCH,
|
|
3
|
+
LEAP_SECONDS,
|
|
4
|
+
MILLISECONDS_PER_DAY,
|
|
5
|
+
MODIFIED_JULIAN_DATE_OFFSET,
|
|
6
|
+
TT_MINUS_TAI_SECONDS
|
|
7
|
+
} from '@interstellar-tools/constants';
|
|
8
|
+
import type { TimeScaleType } from '@interstellar-tools/types';
|
|
9
|
+
|
|
10
|
+
// The leap second table as Unix milliseconds, for lookups
|
|
11
|
+
const STEPS = LEAP_SECONDS.map(({ date, taiMinusUtc }) => ({
|
|
12
|
+
ms: Date.parse(`${date}T00:00:00Z`),
|
|
13
|
+
taiMinusUtc
|
|
14
|
+
}));
|
|
15
|
+
const SCALES: readonly TimeScaleType[] = [
|
|
16
|
+
'UTC',
|
|
17
|
+
'TAI',
|
|
18
|
+
'TT',
|
|
19
|
+
'UT1',
|
|
20
|
+
'TDB',
|
|
21
|
+
'GPS'
|
|
22
|
+
];
|
|
23
|
+
// GPS time was set to UTC on 1980-01-06, when TAI − UTC was 19 s, and has
|
|
24
|
+
// taken no leap seconds since
|
|
25
|
+
const TAI_MINUS_GPS_SECONDS = 19;
|
|
26
|
+
// Julian date of J2000.0
|
|
27
|
+
const JD_J2000 = 2_451_545;
|
|
28
|
+
const assertDate = (date: Date): number => {
|
|
29
|
+
if (!(date instanceof Date) || !Number.isFinite(date.getTime()))
|
|
30
|
+
throw new TypeError(`date must be a valid Date. Received: ${String(date)}`);
|
|
31
|
+
|
|
32
|
+
return date.getTime();
|
|
33
|
+
};
|
|
34
|
+
const assertScale = (scale: TimeScaleType) => {
|
|
35
|
+
if (!SCALES.includes(scale))
|
|
36
|
+
throw new RangeError(
|
|
37
|
+
`scale must be one of ${SCALES.join(', ')}. Received: ${String(scale)}`
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
// TAI − UTC in s at a UTC instant in Unix ms
|
|
41
|
+
const leapSecondsAt = (ms: number): number => {
|
|
42
|
+
if (ms < STEPS[0]!.ms)
|
|
43
|
+
throw new RangeError(
|
|
44
|
+
`TAI, TT, TDB and GPS time are only defined here from 1972-01-01, when UTC began to step by whole leap seconds. Received: ${new Date(ms).toISOString()}`
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
let taiMinusUtc = STEPS[0]!.taiMinusUtc;
|
|
48
|
+
|
|
49
|
+
for (const step of STEPS) {
|
|
50
|
+
if (ms < step.ms) break;
|
|
51
|
+
|
|
52
|
+
taiMinusUtc = step.taiMinusUtc;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return taiMinusUtc;
|
|
56
|
+
};
|
|
57
|
+
// TDB − TT in ms at a TT instant in Unix ms: USNO Circular 179, eq. 2.6,
|
|
58
|
+
// good to 10 µs
|
|
59
|
+
const tdbMinusTt = (ms: number): number => {
|
|
60
|
+
const t =
|
|
61
|
+
(ms / MILLISECONDS_PER_DAY + JULIAN_DATE_UNIX_EPOCH - JD_J2000) / 36_525;
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
1.657 * Math.sin(628.3076 * t + 6.2401) +
|
|
65
|
+
0.022 * Math.sin(575.3385 * t + 4.297) +
|
|
66
|
+
0.014 * Math.sin(1256.6152 * t + 6.1969) +
|
|
67
|
+
0.005 * Math.sin(606.9777 * t + 4.0212) +
|
|
68
|
+
0.005 * Math.sin(52.9691 * t + 0.4444) +
|
|
69
|
+
0.002 * Math.sin(21.3299 * t + 5.5431) +
|
|
70
|
+
0.01 * t * Math.sin(628.3076 * t + 4.249)
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
// The scale's lead over UTC, in ms, at a UTC instant in Unix ms
|
|
74
|
+
const leadOverUtc = (ms: number, scale: TimeScaleType): number => {
|
|
75
|
+
if (scale === 'UTC' || scale === 'UT1') return 0;
|
|
76
|
+
|
|
77
|
+
const tai = 1000 * leapSecondsAt(ms);
|
|
78
|
+
|
|
79
|
+
if (scale === 'TAI') return tai;
|
|
80
|
+
|
|
81
|
+
if (scale === 'GPS') return tai - 1000 * TAI_MINUS_GPS_SECONDS;
|
|
82
|
+
|
|
83
|
+
const tt = tai + 1000 * TT_MINUS_TAI_SECONDS;
|
|
84
|
+
|
|
85
|
+
return scale === 'TT' ? tt : tt + tdbMinusTt(ms + tt);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* **TAI − UTC**, in **s**, at a moment: the number of leap seconds so far, plus
|
|
90
|
+
* the 10 s UTC started with in 1972.
|
|
91
|
+
*
|
|
92
|
+
* $$
|
|
93
|
+
* \mathrm{TAI} = \mathrm{UTC} + \Delta_{\mathrm{AT}},
|
|
94
|
+
* \qquad
|
|
95
|
+
* \mathrm{TT} = \mathrm{TAI} + 32.184\ \mathrm{s}
|
|
96
|
+
* $$
|
|
97
|
+
*
|
|
98
|
+
* Where:
|
|
99
|
+
* - $ \Delta_{\mathrm{AT}} $ is TAI − UTC, from the leap second table `LEAP_SECONDS` (s)
|
|
100
|
+
*
|
|
101
|
+
* ::: info Notes
|
|
102
|
+
*
|
|
103
|
+
* - The table in `@interstellar-tools/constants` holds the leap seconds the
|
|
104
|
+
* IERS has announced. A moment after a leap second that isn't in it yet gets
|
|
105
|
+
* an offset 1 s short: keep the package up to date.
|
|
106
|
+
* - A JavaScript `Date` can't hold 23:59:60, the leap second itself: it goes
|
|
107
|
+
* from 23:59:59.999 to 00:00:00 of the next day, where the new offset starts.
|
|
108
|
+
*
|
|
109
|
+
* :::
|
|
110
|
+
*
|
|
111
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01).
|
|
112
|
+
* @returns TAI − UTC in **s**: a whole number, 37 since 2017.
|
|
113
|
+
* @throws {TypeError} If `date` is not a valid `Date`.
|
|
114
|
+
* @throws {RangeError} If `date` is before 1972-01-01.
|
|
115
|
+
* @group Time scales
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* taiMinusUtc(new Date('2016-12-31T23:59:59Z')); // 36
|
|
120
|
+
* taiMinusUtc(new Date('2017-01-01T00:00:00Z')); // 37
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export const taiMinusUtc = (date: Date): number =>
|
|
124
|
+
leapSecondsAt(assertDate(date));
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The **Julian date** of a moment, in a time scale: the number of days since
|
|
128
|
+
* noon on 1 January 4713 BC, counted on that scale's clock.
|
|
129
|
+
*
|
|
130
|
+
* $$
|
|
131
|
+
* \mathrm{JD} = \frac{t_{\mathrm{Unix}} + \Delta}{86\,400\,000} + 2\,440\,587.5
|
|
132
|
+
* $$
|
|
133
|
+
*
|
|
134
|
+
* Where:
|
|
135
|
+
* - $ t_{\mathrm{Unix}} $ is the `Date`'s milliseconds since 1970-01-01T00:00:00Z (ms)
|
|
136
|
+
* - $ \Delta $ is the scale's lead over UTC: 0 for UTC and UT1, TAI − UTC for TAI, TAI − UTC − 19 s for GPS, TAI − UTC + 32.184 s for TT, and that plus TDB − TT for TDB (ms)
|
|
137
|
+
*
|
|
138
|
+
* ::: info Notes
|
|
139
|
+
*
|
|
140
|
+
* - **UT1 is taken as UTC.** They differ by up to 0.9 s; the exact difference
|
|
141
|
+
* is measured and published by the IERS, and the library fetches nothing.
|
|
142
|
+
* - **TDB − TT** comes from the series in USNO Circular 179 (eq. 2.6), good
|
|
143
|
+
* to 10 µs: within 1.7 ms, largest in early April and early October.
|
|
144
|
+
* - A Julian date as a double is precise to about 40 µs today.
|
|
145
|
+
* - UTC Julian dates step over leap seconds, like the `Date` itself: the
|
|
146
|
+
* difference between two of them in days can be 1 s short. Take differences
|
|
147
|
+
* in TAI or TT.
|
|
148
|
+
*
|
|
149
|
+
* :::
|
|
150
|
+
*
|
|
151
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01 for TAI, TT, TDB and GPS).
|
|
152
|
+
* @param scale - The time scale to count in. Defaults to `UTC`.
|
|
153
|
+
* @returns The Julian date in **days**.
|
|
154
|
+
* @throws {TypeError} If `date` is not a valid `Date`.
|
|
155
|
+
* @throws {RangeError} If `scale` is not a {@link TimeScaleType}, or `date` is before 1972-01-01 for TAI, TT, TDB and GPS.
|
|
156
|
+
* @group Time scales
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { J2000_UTC } from '@interstellar-tools/constants';
|
|
161
|
+
*
|
|
162
|
+
* julianDate(J2000_UTC, 'TT'); // 2451545: J2000 is JD 2451545.0 TT
|
|
163
|
+
* julianDate(J2000_UTC); // 2451544.999257: 64.184 s earlier on the UTC clock
|
|
164
|
+
* julianDate(J2000_UTC, 'GPS'); // 2451544.999408: 13 s ahead of UTC in 2000
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* @see https://aa.usno.navy.mil/publications/Circular_179 USNO Circular 179, for TDB
|
|
168
|
+
*/
|
|
169
|
+
export const julianDate = (
|
|
170
|
+
date: Date,
|
|
171
|
+
scale: TimeScaleType = 'UTC'
|
|
172
|
+
): number => {
|
|
173
|
+
const ms = assertDate(date);
|
|
174
|
+
|
|
175
|
+
assertScale(scale);
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
(ms + leadOverUtc(ms, scale)) / MILLISECONDS_PER_DAY +
|
|
179
|
+
JULIAN_DATE_UNIX_EPOCH
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The **modified Julian date** of a moment, in a time scale: the Julian date
|
|
185
|
+
* minus 2 400 000.5, so days start at midnight and MJD 0 is 1858-11-17.
|
|
186
|
+
*
|
|
187
|
+
* $$
|
|
188
|
+
* \mathrm{MJD} = \mathrm{JD} - 2\,400\,000.5
|
|
189
|
+
* $$
|
|
190
|
+
*
|
|
191
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01 for TAI, TT, TDB and GPS).
|
|
192
|
+
* @param scale - The time scale to count in. Defaults to `UTC`.
|
|
193
|
+
* @returns The modified Julian date in **days**.
|
|
194
|
+
* @throws {TypeError} If `date` is not a valid `Date`.
|
|
195
|
+
* @throws {RangeError} If `scale` is not a {@link TimeScaleType}, or `date` is before 1972-01-01 for TAI, TT, TDB and GPS.
|
|
196
|
+
* @group Time scales
|
|
197
|
+
* @see {@link julianDate}
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* modifiedJulianDate(new Date('2000-01-01T00:00:00Z')); // 51544
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export const modifiedJulianDate = (
|
|
205
|
+
date: Date,
|
|
206
|
+
scale: TimeScaleType = 'UTC'
|
|
207
|
+
): number => {
|
|
208
|
+
const ms = assertDate(date);
|
|
209
|
+
|
|
210
|
+
assertScale(scale);
|
|
211
|
+
|
|
212
|
+
// Subtract the offsets first, so the result keeps its precision
|
|
213
|
+
return (
|
|
214
|
+
(ms + leadOverUtc(ms, scale)) / MILLISECONDS_PER_DAY +
|
|
215
|
+
(JULIAN_DATE_UNIX_EPOCH - MODIFIED_JULIAN_DATE_OFFSET)
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The **UTC `Date`** of a Julian date in a time scale: the inverse of
|
|
221
|
+
* {@link julianDate}.
|
|
222
|
+
*
|
|
223
|
+
* ::: info Notes
|
|
224
|
+
*
|
|
225
|
+
* - The result is rounded to the millisecond, the precision of a `Date`.
|
|
226
|
+
* - A TAI or TT Julian date that falls inside a leap second, 23:59:60 UTC,
|
|
227
|
+
* has no `Date`: it becomes 00:00:00 of the next day.
|
|
228
|
+
*
|
|
229
|
+
* :::
|
|
230
|
+
*
|
|
231
|
+
* @param jd - The Julian date in **days** (finite).
|
|
232
|
+
* @param scale - The time scale the Julian date counts in. Defaults to `UTC`.
|
|
233
|
+
* @returns The moment as a `Date`.
|
|
234
|
+
* @throws {TypeError} If `jd` is not a finite number.
|
|
235
|
+
* @throws {RangeError} If `scale` is not a {@link TimeScaleType}, the moment is before 1972-01-01 for TAI, TT, TDB and GPS, or it's outside the range of a `Date`.
|
|
236
|
+
* @group Time scales
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* dateFromJulianDate(2451545, 'TT').toISOString(); // '2000-01-01T11:58:55.816Z'
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
export const dateFromJulianDate = (
|
|
244
|
+
jd: number,
|
|
245
|
+
scale: TimeScaleType = 'UTC'
|
|
246
|
+
): Date => {
|
|
247
|
+
if (!Number.isFinite(jd))
|
|
248
|
+
throw new TypeError(`jd must be a finite number. Received: ${jd}`);
|
|
249
|
+
|
|
250
|
+
assertScale(scale);
|
|
251
|
+
|
|
252
|
+
// Milliseconds since 1970-01-01T00:00:00 on the scale's clock, TT for TDB
|
|
253
|
+
const exact = (jd - JULIAN_DATE_UNIX_EPOCH) * MILLISECONDS_PER_DAY;
|
|
254
|
+
const onScale = Math.round(
|
|
255
|
+
scale === 'TDB' ? exact - tdbMinusTt(exact) : exact
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
// Further back than a Date reaches: say so here, as the leap-second check
|
|
259
|
+
// below can't print a date that far back
|
|
260
|
+
if (onScale < -8.64e15)
|
|
261
|
+
throw new RangeError(`jd is outside the range of a Date. Received: ${jd}`);
|
|
262
|
+
|
|
263
|
+
let ms = onScale;
|
|
264
|
+
|
|
265
|
+
if (scale !== 'UTC' && scale !== 'UT1') {
|
|
266
|
+
// Back to TAI, then to UTC. The leap seconds depend on the UTC moment:
|
|
267
|
+
// guess with TAI, then settle on the offset at the guess
|
|
268
|
+
const tai =
|
|
269
|
+
scale === 'TAI'
|
|
270
|
+
? onScale
|
|
271
|
+
: scale === 'GPS'
|
|
272
|
+
? onScale + 1000 * TAI_MINUS_GPS_SECONDS
|
|
273
|
+
: onScale - 1000 * TT_MINUS_TAI_SECONDS;
|
|
274
|
+
const guess = tai - 1000 * leapSecondsAt(tai);
|
|
275
|
+
const offset = leapSecondsAt(guess);
|
|
276
|
+
|
|
277
|
+
ms = tai - 1000 * offset;
|
|
278
|
+
|
|
279
|
+
// Inside a leap second, no UTC moment has this offset: take the start of
|
|
280
|
+
// the next day, where the new offset begins
|
|
281
|
+
if (leapSecondsAt(ms) !== offset)
|
|
282
|
+
ms = STEPS.find((step) => step.ms > guess)!.ms;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const date = new Date(ms);
|
|
286
|
+
|
|
287
|
+
if (!Number.isFinite(date.getTime()))
|
|
288
|
+
throw new RangeError(`jd is outside the range of a Date. Received: ${jd}`);
|
|
289
|
+
|
|
290
|
+
return date;
|
|
291
|
+
};
|