@interstellar-tools/temporal 1.3.1 → 2.0.1
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 +790 -0
- package/README.md +2 -1
- package/package.json +8 -5
- package/src/convert-temporal-unit.ts +115 -0
- package/src/index.ts +7 -0
- package/src/mars-time.ts +204 -0
- package/src/normalize-temporal-unit.ts +136 -0
- package/src/time-scales.ts +286 -0
package/README.md
CHANGED
|
@@ -17,7 +17,8 @@ The full documentation lives at
|
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
19
19
|
|
|
20
|
-
- [Node.js](https://nodejs.org/) version 22.
|
|
20
|
+
- [Node.js](https://nodejs.org/) version 22.12.0 or higher
|
|
21
|
+
- ES modules; `require()` needs Node 22.12 or later
|
|
21
22
|
|
|
22
23
|
```shell [npm]
|
|
23
24
|
npm i --save @interstellar-tools/temporal
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interstellar-tools/temporal",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Time and epoch utilities for space applications (e.g., building blocks toward Julian dates, conversions, and ephemeris-friendly time handling).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"temporal",
|
|
@@ -100,7 +100,10 @@
|
|
|
100
100
|
}
|
|
101
101
|
},
|
|
102
102
|
"files": [
|
|
103
|
-
"dist"
|
|
103
|
+
"dist",
|
|
104
|
+
"src",
|
|
105
|
+
"!src/**/__tests__",
|
|
106
|
+
"CHANGELOG.md"
|
|
104
107
|
],
|
|
105
108
|
"sideEffects": false,
|
|
106
109
|
"scripts": {
|
|
@@ -112,8 +115,8 @@
|
|
|
112
115
|
"test:ci": "glob-bin -c \"node --import tsx --test --no-warnings --experimental-test-coverage --test-coverage-include='src/**' --test-coverage-exclude='**/__tests__/**' --test-coverage-lines=100 --test-coverage-branches=100 --test-coverage-functions=100 --test-reporter=cobertura --test-reporter-destination=coverage/cobertura-coverage.xml --test-reporter=spec --test-reporter-destination=stdout\" \"**/__tests__/**/*.spec.ts\""
|
|
113
116
|
},
|
|
114
117
|
"dependencies": {
|
|
115
|
-
"@interstellar-tools/constants": "^
|
|
116
|
-
"@interstellar-tools/types": "^
|
|
118
|
+
"@interstellar-tools/constants": "^3.0.0",
|
|
119
|
+
"@interstellar-tools/types": "^3.0.0"
|
|
117
120
|
},
|
|
118
121
|
"devDependencies": {
|
|
119
122
|
"@types/node": "^24.19.2",
|
|
@@ -122,7 +125,7 @@
|
|
|
122
125
|
"typescript": "^6.0.3"
|
|
123
126
|
},
|
|
124
127
|
"engines": {
|
|
125
|
-
"node": ">=22.
|
|
128
|
+
"node": ">=22.12.0"
|
|
126
129
|
},
|
|
127
130
|
"publishConfig": {
|
|
128
131
|
"access": "public"
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JULIAN_YEAR_SECONDS,
|
|
3
|
+
SECONDS_PER_DAY
|
|
4
|
+
} from '@interstellar-tools/constants';
|
|
5
|
+
import type {
|
|
6
|
+
TemporalInterface,
|
|
7
|
+
TemporalUnitAliasType,
|
|
8
|
+
TemporalUnitType
|
|
9
|
+
} from '@interstellar-tools/types';
|
|
10
|
+
|
|
11
|
+
import { normalizeTemporalUnit } from './normalize-temporal-unit.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Seconds-per-unit lookup for all canonical {@link TemporalUnitType} symbols.
|
|
15
|
+
*
|
|
16
|
+
* ### Conventions
|
|
17
|
+
* - **Base**: conversions are performed via **seconds**.
|
|
18
|
+
* - **SI day**: `d = 86 400 s` (from {@link SECONDS_PER_DAY}).
|
|
19
|
+
* - **Julian year**: `yr = 365.25 d = 31 557 600 s`
|
|
20
|
+
* (from {@link JULIAN_YEAR_SECONDS}); `kyr/Myr/Gyr` are powers of 10 thereof.
|
|
21
|
+
* - Units are **canonical** and **case-sensitive** (e.g., `'μs'` uses the Unicode micro sign).
|
|
22
|
+
* - The table is frozen: it can be read, but not changed at runtime.
|
|
23
|
+
*
|
|
24
|
+
* $$
|
|
25
|
+
* \text{seconds}(u) \;=\; \text{value}\times \mathrm{SECONDS\_PER}[u]
|
|
26
|
+
* $$
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* // seconds per hour:
|
|
31
|
+
* SECONDS_PER.h; // 3600
|
|
32
|
+
*
|
|
33
|
+
* // seconds in a Julian year:
|
|
34
|
+
* SECONDS_PER.yr; // 31_557_600
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export const SECONDS_PER: Readonly<Record<TemporalUnitType, number>> =
|
|
38
|
+
Object.freeze({
|
|
39
|
+
// SI base & submultiples
|
|
40
|
+
s: 1,
|
|
41
|
+
ms: 1e-3,
|
|
42
|
+
μs: 1e-6,
|
|
43
|
+
ns: 1e-9,
|
|
44
|
+
ps: 1e-12,
|
|
45
|
+
fs: 1e-15,
|
|
46
|
+
as: 1e-18,
|
|
47
|
+
zs: 1e-21,
|
|
48
|
+
ys: 1e-24,
|
|
49
|
+
|
|
50
|
+
// convenient larger units
|
|
51
|
+
min: 60,
|
|
52
|
+
h: 60 * 60,
|
|
53
|
+
d: SECONDS_PER_DAY,
|
|
54
|
+
|
|
55
|
+
// year family (Julian-year-based)
|
|
56
|
+
yr: JULIAN_YEAR_SECONDS,
|
|
57
|
+
kyr: JULIAN_YEAR_SECONDS * 1e3,
|
|
58
|
+
Myr: JULIAN_YEAR_SECONDS * 1e6,
|
|
59
|
+
Gyr: JULIAN_YEAR_SECONDS * 1e9
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Convert a temporal **duration** between any two canonical {@link TemporalUnitType} units.
|
|
64
|
+
*
|
|
65
|
+
* - Uses {@link SECONDS_PER} with **seconds** as the intermediate unit.
|
|
66
|
+
* - Interprets `d` as the **SI day** and `yr/kyr/Myr/Gyr` via the **Julian year** convention.
|
|
67
|
+
* - The `time.unit` is expected to be **canonical**; if you accept aliases upstream,
|
|
68
|
+
* normalize them before calling (e.g., with `normalizeTemporalUnit`).
|
|
69
|
+
* - The `targetUnit` may be a canonical unit or an alias such as `'days'`; it is
|
|
70
|
+
* normalized to a canonical symbol.
|
|
71
|
+
*
|
|
72
|
+
* $$
|
|
73
|
+
* \text{value}_{\text{target}}
|
|
74
|
+
* \;=\;
|
|
75
|
+
* \frac{\text{value}_{\text{in}} \cdot \mathrm{SECONDS\_PER}[u_{\text{in}}]}
|
|
76
|
+
* {\mathrm{SECONDS\_PER}[u_{\text{out}}]}
|
|
77
|
+
* $$
|
|
78
|
+
*
|
|
79
|
+
* @param time Input duration `{ value, unit }` in canonical units.
|
|
80
|
+
* @param targetUnit Desired output unit, canonical or an alias (will be normalized).
|
|
81
|
+
* @returns A new `{ value, unit }` where `unit` is the canonical target symbol.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* convertTemporalUnit({ value: 2, unit: 'h' }, 'min');
|
|
86
|
+
* // → { value: 120, unit: 'min' }
|
|
87
|
+
*
|
|
88
|
+
* convertTemporalUnit({ value: 1, unit: 'd' }, 's');
|
|
89
|
+
* // → { value: 86_400, unit: 's' }
|
|
90
|
+
*
|
|
91
|
+
* convertTemporalUnit({ value: 3, unit: 'Myr' }, 'kyr');
|
|
92
|
+
* // → { value: 3000, unit: 'kyr' }
|
|
93
|
+
*
|
|
94
|
+
* convertTemporalUnit({ value: 1, unit: 'yr' }, 'd');
|
|
95
|
+
* // → { value: 365.25, unit: 'd' }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @throws {RangeError} If `targetUnit` is not a known unit or alias.
|
|
99
|
+
* @see SECONDS_PER_DAY
|
|
100
|
+
* @see JULIAN_YEAR_SECONDS
|
|
101
|
+
* @see normalizeTemporalUnit
|
|
102
|
+
*/
|
|
103
|
+
export const convertTemporalUnit = (
|
|
104
|
+
time: TemporalInterface,
|
|
105
|
+
targetUnit: TemporalUnitType | TemporalUnitAliasType
|
|
106
|
+
): TemporalInterface => {
|
|
107
|
+
const normalizedTargetUnit = normalizeTemporalUnit(targetUnit);
|
|
108
|
+
const { value, unit } = time;
|
|
109
|
+
// Convert input to seconds
|
|
110
|
+
const inSeconds = value * SECONDS_PER[unit];
|
|
111
|
+
// Convert seconds to target
|
|
112
|
+
const convertedValue = inSeconds / SECONDS_PER[normalizedTargetUnit];
|
|
113
|
+
|
|
114
|
+
return { value: convertedValue, unit: normalizedTargetUnit };
|
|
115
|
+
};
|
package/src/index.ts
ADDED
package/src/mars-time.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { MARS_SOL_DAYS } from '@interstellar-tools/constants';
|
|
2
|
+
import type { Radians } from '@interstellar-tools/types';
|
|
3
|
+
|
|
4
|
+
import { julianDate } from './time-scales.js';
|
|
5
|
+
|
|
6
|
+
// Mars24's reference: MSD 44 796.0 − 0.0009626 at JD 2451549.5 TT, the start
|
|
7
|
+
// of 6 January 2000 on Earth
|
|
8
|
+
const MSD_REFERENCE_JD_TT = 2_451_549.5;
|
|
9
|
+
const MSD_AT_REFERENCE = 44_796 - 0.000_962_6;
|
|
10
|
+
const TWO_PI = 2 * Math.PI;
|
|
11
|
+
const fraction = (x: number) => x - Math.floor(x);
|
|
12
|
+
const assertLongitude = (longitude: number) => {
|
|
13
|
+
if (!Number.isFinite(longitude))
|
|
14
|
+
throw new TypeError(
|
|
15
|
+
`longitude must be a finite number. Received: ${longitude}`
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The **Mars Sol Date** of a moment: a continuous count of sols, Mars's mean
|
|
21
|
+
* solar days, like the Julian date for Earth.
|
|
22
|
+
*
|
|
23
|
+
* $$
|
|
24
|
+
* \mathrm{MSD} = \frac{\mathrm{JD_{TT}} - 2\,451\,549.5}{1.0274912517} + 44\,796.0 - 0.0009626
|
|
25
|
+
* $$
|
|
26
|
+
*
|
|
27
|
+
* Where:
|
|
28
|
+
* - $ \mathrm{JD_{TT}} $ is the Julian date in Terrestrial Time (days)
|
|
29
|
+
* - $ 1.0274912517 $ is the sol in Earth days, {@link "@interstellar-tools/constants"!MARS_SOL_DAYS | MARS_SOL_DAYS}
|
|
30
|
+
*
|
|
31
|
+
* ::: info Notes
|
|
32
|
+
*
|
|
33
|
+
* - The count starts on 29 December 1873, at about noon in Greenwich.
|
|
34
|
+
* - Each new sol begins at mean midnight at Airy-0, when
|
|
35
|
+
* {@link coordinatedMarsTime} wraps to 0.
|
|
36
|
+
* - From NASA GISS's Mars24 algorithm, refined from Allison & McEwen (2000).
|
|
37
|
+
*
|
|
38
|
+
* :::
|
|
39
|
+
*
|
|
40
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01).
|
|
41
|
+
* @returns The Mars Sol Date in **sols**.
|
|
42
|
+
* @throws {TypeError} If `date` is not a valid `Date`.
|
|
43
|
+
* @throws {RangeError} If `date` is before 1972-01-01, where TT isn't defined from the leap seconds.
|
|
44
|
+
* @group Mars time
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* marsSolDate(new Date('2000-01-06T00:00:00Z')); // ≈ 44 795.99976
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @see {@link coordinatedMarsTime}
|
|
52
|
+
* @see https://www.giss.nasa.gov/tools/mars24/help/algorithm.html Mars24 algorithm
|
|
53
|
+
*/
|
|
54
|
+
export const marsSolDate = (date: Date): number =>
|
|
55
|
+
(julianDate(date, 'TT') - MSD_REFERENCE_JD_TT) / MARS_SOL_DAYS +
|
|
56
|
+
MSD_AT_REFERENCE;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* **Coordinated Mars Time** (MTC): the mean solar time at Mars's prime
|
|
60
|
+
* meridian, Airy-0, in hours, like UTC is at Greenwich.
|
|
61
|
+
*
|
|
62
|
+
* $$
|
|
63
|
+
* \mathrm{MTC} = 24 \times (\mathrm{MSD} \bmod 1)
|
|
64
|
+
* $$
|
|
65
|
+
*
|
|
66
|
+
* ::: info Notes
|
|
67
|
+
*
|
|
68
|
+
* - A Mars hour is a 24th of a sol: 1 h 1 m 39 s of Earth time.
|
|
69
|
+
* - It's mean time: the real Sun can be up to about 50 minutes ahead of or behind it,
|
|
70
|
+
* because of Mars's eccentric orbit and tilt, the equation of time.
|
|
71
|
+
*
|
|
72
|
+
* :::
|
|
73
|
+
*
|
|
74
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01).
|
|
75
|
+
* @returns The time at Airy-0 in **Mars hours**, from 0 up to 24.
|
|
76
|
+
* @throws {TypeError} If `date` is not a valid `Date`.
|
|
77
|
+
* @throws {RangeError} If `date` is before 1972-01-01.
|
|
78
|
+
* @group Mars time
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* // Mars24's first worked example: 23:59:39 at Airy-0
|
|
83
|
+
* coordinatedMarsTime(new Date('2000-01-06T00:00:00Z')); // ≈ 23.99425 h
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @see {@link marsSolDate}
|
|
87
|
+
* @see {@link marsLocalMeanSolarTime}
|
|
88
|
+
*/
|
|
89
|
+
export const coordinatedMarsTime = (date: Date): number =>
|
|
90
|
+
24 * fraction(marsSolDate(date));
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The **local mean solar time on Mars** at a longitude, in hours: Coordinated
|
|
94
|
+
* Mars Time plus the east longitude in hours, 15° to the hour.
|
|
95
|
+
*
|
|
96
|
+
* $$
|
|
97
|
+
* \mathrm{LMST} = \left( \mathrm{MTC} + \lambda \, \frac{24\ \mathrm{h}}{2\pi} \right) \bmod 24\ \mathrm{h}
|
|
98
|
+
* $$
|
|
99
|
+
*
|
|
100
|
+
* Where:
|
|
101
|
+
* - $ \lambda $ is the planetocentric east longitude (rad)
|
|
102
|
+
*
|
|
103
|
+
* ::: info Notes
|
|
104
|
+
*
|
|
105
|
+
* - **East longitude**, as the IAU gives it for Mars. Mars24 counts west
|
|
106
|
+
* longitude, so 184.702° W there is 175.298° E here.
|
|
107
|
+
* - It's mean time, by a Sun that moves evenly. The real Sun can be up to about
|
|
108
|
+
* 50 minutes ahead of or behind it.
|
|
109
|
+
*
|
|
110
|
+
* :::
|
|
111
|
+
*
|
|
112
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01).
|
|
113
|
+
* @param longitude - The east longitude in **radians** (finite; any turn).
|
|
114
|
+
* @returns The local mean solar time in **Mars hours**, from 0 up to 24.
|
|
115
|
+
* @throws {TypeError} If `date` is not a valid `Date`, or `longitude` is not a finite number.
|
|
116
|
+
* @throws {RangeError} If `date` is before 1972-01-01.
|
|
117
|
+
* @group Mars time
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const DEG = Math.PI / 180;
|
|
122
|
+
*
|
|
123
|
+
* // Spirit's site, at local midnight on Earth's clock: 00:51 mean time
|
|
124
|
+
* marsLocalMeanSolarTime(
|
|
125
|
+
* new Date('2004-01-03T13:46:31Z'),
|
|
126
|
+
* (175.298 * DEG) as Radians
|
|
127
|
+
* ); // ≈ 0.8519 h
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @see {@link coordinatedMarsTime}
|
|
131
|
+
* @see {@link missionSol}
|
|
132
|
+
*/
|
|
133
|
+
export const marsLocalMeanSolarTime = (
|
|
134
|
+
date: Date,
|
|
135
|
+
longitude: Radians
|
|
136
|
+
): number => {
|
|
137
|
+
assertLongitude(longitude);
|
|
138
|
+
|
|
139
|
+
return 24 * fraction(marsSolDate(date) + longitude / TWO_PI);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The **mission sol** at a landing site: the number of the sol a rover is on,
|
|
144
|
+
* counted from its landing.
|
|
145
|
+
*
|
|
146
|
+
* $$
|
|
147
|
+
* \text{sol} = s_0 + \lfloor \mathrm{MSD} + \lambda / 2\pi \rfloor - \lfloor \mathrm{MSD_{landing}} + \lambda / 2\pi \rfloor
|
|
148
|
+
* $$
|
|
149
|
+
*
|
|
150
|
+
* Where:
|
|
151
|
+
* - $ s_0 $ is the number of the landing sol
|
|
152
|
+
* - $ \lambda $ is the east longitude of the mission's clock (rad)
|
|
153
|
+
*
|
|
154
|
+
* ::: info Notes
|
|
155
|
+
*
|
|
156
|
+
* - A new sol begins at local mean midnight at the site.
|
|
157
|
+
* - **Sol 0 or sol 1?** Curiosity and Perseverance call their landing sol 0.
|
|
158
|
+
* Spirit and Opportunity called it sol 1.
|
|
159
|
+
* - Before landing, the count goes below the landing sol.
|
|
160
|
+
* - Spirit and Opportunity ran on a hybrid of true solar time, a few minutes
|
|
161
|
+
* off mean time: close to their midnights, their count can differ by one.
|
|
162
|
+
*
|
|
163
|
+
* :::
|
|
164
|
+
*
|
|
165
|
+
* @param date - The moment, as a UTC `Date` (on or after 1972-01-01).
|
|
166
|
+
* @param landingDate - The moment of landing, as a UTC `Date` (on or after 1972-01-01).
|
|
167
|
+
* @param longitude - The east longitude of the mission's clock in **radians** (finite).
|
|
168
|
+
* @param firstSol - The number of the landing sol: 0 or 1 (an integer). Defaults to 0.
|
|
169
|
+
* @returns The sol number, an integer.
|
|
170
|
+
* @throws {TypeError} If a date is not a valid `Date`, `longitude` is not a finite number, or `firstSol` is not an integer.
|
|
171
|
+
* @throws {RangeError} If a date is before 1972-01-01.
|
|
172
|
+
* @group Mars time
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* const DEG = Math.PI / 180;
|
|
177
|
+
*
|
|
178
|
+
* // Curiosity landed in Gale crater, its clock at 137.42° E
|
|
179
|
+
* missionSol(
|
|
180
|
+
* new Date('2015-05-30T12:00:00Z'),
|
|
181
|
+
* new Date('2012-08-06T05:17:57Z'),
|
|
182
|
+
* (137.42 * DEG) as Radians
|
|
183
|
+
* ); // 1000
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* @see {@link marsLocalMeanSolarTime}
|
|
187
|
+
* @see https://www.giss.nasa.gov/tools/mars24/help/notes.html Mars24 technical notes, on mission sols
|
|
188
|
+
*/
|
|
189
|
+
export const missionSol = (
|
|
190
|
+
date: Date,
|
|
191
|
+
landingDate: Date,
|
|
192
|
+
longitude: Radians,
|
|
193
|
+
firstSol = 0
|
|
194
|
+
): number => {
|
|
195
|
+
assertLongitude(longitude);
|
|
196
|
+
|
|
197
|
+
if (!Number.isInteger(firstSol))
|
|
198
|
+
throw new TypeError(`firstSol must be an integer. Received: ${firstSol}`);
|
|
199
|
+
|
|
200
|
+
const local = (moment: Date) =>
|
|
201
|
+
Math.floor(marsSolDate(moment) + longitude / TWO_PI);
|
|
202
|
+
|
|
203
|
+
return firstSol + local(date) - local(landingDate);
|
|
204
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
*
|
|
104
|
+
* @param u - The input unit string. Leading/trailing whitespace is ignored; content is **case-sensitive**.
|
|
105
|
+
* @returns The canonical {@link TemporalUnitType} (e.g., `"s"`, `"ms"`, `"μs"`, `"min"`, `"h"`, `"d"`, `"yr"`, `"kyr"`, `"Myr"`, `"Gyr"`).
|
|
106
|
+
*
|
|
107
|
+
* @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}.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* // Canonical conversion using the normalized unit:
|
|
112
|
+
* const unit = normalizeTemporalUnit(' seconds '); // → 's'
|
|
113
|
+
* const valueInSeconds = 2 * (unit === 's' ? 1 : NaN); // 2
|
|
114
|
+
*
|
|
115
|
+
* normalizeTemporalUnit('us'); // → 'μs'
|
|
116
|
+
* normalizeTemporalUnit('Myr'); // → 'Myr' (already canonical)
|
|
117
|
+
* normalizeTemporalUnit('Ma'); // → 'Myr' (geology-style alias)
|
|
118
|
+
* normalizeTemporalUnit('Ga'); // → 'Gyr'
|
|
119
|
+
* // normalizeTemporalUnit('Seconds'); // throws (case-sensitive)
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @see {@link NORMALIZE_TEMPORAL_UNIT} for the complete alias→canonical mapping.
|
|
123
|
+
* @see https://www.bipm.org/en/publications/si-brochure (SI Brochure - second/day)
|
|
124
|
+
* @see https://en.wikipedia.org/wiki/Julian_year_(astronomy) (Julian year for yr/kyr/Myr/Gyr)
|
|
125
|
+
*/
|
|
126
|
+
/* node:coverage enable */
|
|
127
|
+
export const normalizeTemporalUnit = (u: string): TemporalUnitType => {
|
|
128
|
+
const k = u.trim();
|
|
129
|
+
const v = Object.prototype.hasOwnProperty.call(NORMALIZE_TEMPORAL_UNIT, k)
|
|
130
|
+
? NORMALIZE_TEMPORAL_UNIT[k as keyof typeof NORMALIZE_TEMPORAL_UNIT]
|
|
131
|
+
: undefined;
|
|
132
|
+
|
|
133
|
+
if (!v) throw new RangeError(`Unsupported unit: ${u}`);
|
|
134
|
+
|
|
135
|
+
return v;
|
|
136
|
+
};
|