@johnmorrisdotca/wakusei-planets 0.19.0 → 0.21.0

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.
Files changed (113) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/NOTICE.md +14 -3
  3. package/README.md +39 -6
  4. package/dist/bodies/earth.js +13 -5
  5. package/dist/calendar.d.ts +119 -0
  6. package/dist/calendar.js +155 -0
  7. package/dist/clock.d.ts +244 -0
  8. package/dist/clock.js +325 -0
  9. package/dist/coordinates.d.ts +172 -0
  10. package/dist/coordinates.js +170 -0
  11. package/dist/craft/cassini.d.ts +1 -0
  12. package/dist/craft/cassini.js +1131 -0
  13. package/dist/craft/galileo.d.ts +1 -0
  14. package/dist/craft/galileo.js +224 -0
  15. package/dist/craft/juno.d.ts +1 -0
  16. package/dist/craft/juno.js +640 -0
  17. package/dist/craft/newhorizons.d.ts +1 -0
  18. package/dist/craft/newhorizons.js +18 -0
  19. package/dist/craft/parker.d.ts +1 -0
  20. package/dist/craft/parker.js +221 -0
  21. package/dist/craft/pioneer10.d.ts +1 -0
  22. package/dist/craft/pioneer10.js +24 -0
  23. package/dist/craft/pioneer11.d.ts +1 -0
  24. package/dist/craft/pioneer11.js +34 -0
  25. package/dist/craft/voyager1.d.ts +1 -0
  26. package/dist/craft/voyager1.js +28 -0
  27. package/dist/craft/voyager2.d.ts +1 -0
  28. package/dist/craft/voyager2.js +43 -0
  29. package/dist/craft/webb.d.ts +1 -0
  30. package/dist/craft/webb.js +28 -0
  31. package/dist/data/craft.d.ts +23 -0
  32. package/dist/data/craft.js +53 -0
  33. package/dist/data/lunar.d.ts +4 -0
  34. package/dist/data/lunar.js +548 -0
  35. package/dist/data/pluto.d.ts +6 -0
  36. package/dist/data/pluto.js +29 -0
  37. package/dist/data/stars.d.ts +4 -0
  38. package/dist/data/stars.js +5088 -0
  39. package/dist/data/tables.d.ts +41 -0
  40. package/dist/data/tables.js +37 -0
  41. package/dist/data/vsop.d.ts +19 -1
  42. package/dist/data/vsop.js +25 -8
  43. package/dist/element.d.ts +2 -23
  44. package/dist/element.js +75 -110
  45. package/dist/ephemeris.d.ts +22 -14
  46. package/dist/ephemeris.js +106 -58
  47. package/dist/index.d.ts +1 -1
  48. package/dist/index.js +1 -1
  49. package/dist/mars.d.ts +41 -0
  50. package/dist/mars.js +52 -0
  51. package/dist/portrait/element.d.ts +1 -0
  52. package/dist/portrait/element.js +27 -39
  53. package/dist/scales.d.ts +46 -0
  54. package/dist/scales.js +75 -0
  55. package/dist/scene/basis.d.ts +3 -0
  56. package/dist/scene/basis.js +4 -0
  57. package/dist/scene/body.d.ts +2 -4
  58. package/dist/scene/body.js +55 -67
  59. package/dist/scene/fallback.d.ts +4 -0
  60. package/dist/scene/fallback.js +5 -0
  61. package/dist/scene/painter.js +10 -23
  62. package/dist/scene/store.d.ts +2 -2
  63. package/dist/scene/textures.d.ts +30 -0
  64. package/dist/scene/textures.js +47 -0
  65. package/dist/sidereal.d.ts +70 -0
  66. package/dist/sidereal.js +94 -0
  67. package/dist/sky/engine.d.ts +37 -0
  68. package/dist/sky/engine.js +116 -0
  69. package/dist/sky/events.d.ts +37 -0
  70. package/dist/sky/events.js +133 -0
  71. package/dist/sky/frames.d.ts +3 -0
  72. package/dist/sky/frames.js +26 -0
  73. package/dist/sky/types.d.ts +20 -0
  74. package/dist/sky/types.js +1 -0
  75. package/dist/space/aim.d.ts +18 -0
  76. package/dist/space/aim.js +28 -0
  77. package/dist/space/element.d.ts +212 -0
  78. package/dist/space/element.js +524 -0
  79. package/dist/space/frame.d.ts +152 -0
  80. package/dist/space/frame.js +169 -0
  81. package/dist/space/index.d.ts +4 -0
  82. package/dist/space/index.js +5 -0
  83. package/dist/space/ride.d.ts +89 -0
  84. package/dist/space/ride.js +58 -0
  85. package/dist/space/scale.d.ts +126 -0
  86. package/dist/space/scale.js +139 -0
  87. package/dist/space/scene.d.ts +189 -0
  88. package/dist/space/scene.js +422 -0
  89. package/dist/spacecraft-facts.d.ts +114 -0
  90. package/dist/spacecraft-facts.js +215 -0
  91. package/dist/spacecraft.d.ts +134 -0
  92. package/dist/spacecraft.js +198 -0
  93. package/dist/stars.d.ts +160 -0
  94. package/dist/stars.js +193 -0
  95. package/dist/suncalc-legacy.d.ts +187 -0
  96. package/dist/suncalc-legacy.js +168 -0
  97. package/dist/suncalc.d.ts +209 -0
  98. package/dist/suncalc.js +167 -0
  99. package/dist/tables/all.d.ts +2 -0
  100. package/dist/tables/all.js +2 -0
  101. package/dist/tables/ceres.d.ts +1 -0
  102. package/dist/tables/ceres.js +57 -0
  103. package/dist/tables/dwarfs.d.ts +1 -0
  104. package/dist/tables/dwarfs.js +59 -0
  105. package/dist/theory.d.ts +20 -0
  106. package/dist/theory.js +98 -0
  107. package/dist/time.d.ts +6 -108
  108. package/dist/time.js +7 -180
  109. package/dist/version.d.ts +1 -1
  110. package/dist/version.js +1 -1
  111. package/dist/when.d.ts +16 -0
  112. package/dist/when.js +13 -0
  113. package/package.json +95 -4
@@ -0,0 +1,244 @@
1
+ import { type When } from "./when.ts";
2
+ export declare const DAY_MS = 86400000;
3
+ /** The Julian date of the Unix epoch, 1970-01-01T00:00:00Z. */
4
+ export declare const UNIX_EPOCH_JD = 2440587.5;
5
+ export declare const TT_MINUS_TAI_SECONDS = 32.184;
6
+ /** The length of a Gregorian year in milliseconds, for turning a time into a decimal year. */
7
+ export declare const YEAR_MS = 31556952000;
8
+ /** TAI minus UTC, in seconds, from the date it took effect (IERS Bulletin C). After the last one, none has been added. */
9
+ export declare const LEAP_SECONDS: readonly (readonly [number, number])[];
10
+ /**
11
+ * A time as a Julian date: the days since noon on 1 January 4713 BC (Julian calendar), as a number. A `Date` or milliseconds since 1970 in, the same scale out (UTC stays UTC).
12
+ * A number near today holds about a tenth of a millisecond; `toJulianDay` and `toMillisecondsInJulianDay` give the same instant with nothing lost.
13
+ * @param when - A `Date` or milliseconds since 1970.
14
+ * @returns The Julian date.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { toJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
19
+ *
20
+ * console.log(toJulianDate(Date.UTC(2013, 10, 21, 10, 47, 2)).toFixed(6));
21
+ * ```
22
+ */
23
+ export declare function toJulianDate(when: When): number;
24
+ /**
25
+ * The time, in milliseconds since 1970, of a Julian date.
26
+ * @param jd - The Julian date.
27
+ * @returns Milliseconds since 1970.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { fromJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
32
+ *
33
+ * console.log(new Date(fromJulianDate(2451545)).toISOString());
34
+ * ```
35
+ */
36
+ export declare function fromJulianDate(jd: number): number;
37
+ /**
38
+ * The modified Julian date: the Julian date less 2,400,000.5, so a day starts at midnight and the numbers are shorter.
39
+ * @param when - A `Date` or milliseconds since 1970.
40
+ * @returns The modified Julian date.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import { modifiedJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
45
+ *
46
+ * console.log(modifiedJulianDate(Date.UTC(2000, 0, 1)));
47
+ * ```
48
+ */
49
+ export declare function modifiedJulianDate(when: When): number;
50
+ /**
51
+ * The whole Julian day of a time. A Julian day starts at noon, so 11:59 and 12:00 are in different days.
52
+ * @param when - A `Date` or milliseconds since 1970.
53
+ * @returns The integer Julian day number.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import { toJulianDay } from "@johnmorrisdotca/wakusei-planets/time";
58
+ *
59
+ * console.log(toJulianDay(Date.UTC(2000, 0, 1, 12)));
60
+ * ```
61
+ */
62
+ export declare function toJulianDay(when: When): number;
63
+ /**
64
+ * The milliseconds since noon, the start of the Julian day, that a time is into it.
65
+ * @param when - A `Date` or milliseconds since 1970.
66
+ * @returns Milliseconds from 0 up to one day.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { toMillisecondsInJulianDay } from "@johnmorrisdotca/wakusei-planets/time";
71
+ *
72
+ * console.log(toMillisecondsInJulianDay(Date.UTC(2000, 0, 1, 18)));
73
+ * ```
74
+ */
75
+ export declare function toMillisecondsInJulianDay(when: When): number;
76
+ /**
77
+ * A time from a Julian day and the milliseconds into it, with no digit lost: `fromJulianDayAndMilliseconds(toJulianDay(t), toMillisecondsInJulianDay(t))` is `t`.
78
+ * @param day - The integer Julian day number.
79
+ * @param msInDay - Milliseconds since the day began at noon.
80
+ * @returns Milliseconds since 1970.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * import { fromJulianDayAndMilliseconds, toJulianDay, toMillisecondsInJulianDay } from "@johnmorrisdotca/wakusei-planets/time";
85
+ *
86
+ * const t = Date.UTC(2026, 10, 7, 9, 30, 15, 123);
87
+ * console.log(fromJulianDayAndMilliseconds(toJulianDay(t), toMillisecondsInJulianDay(t)) === t);
88
+ * ```
89
+ */
90
+ export declare function fromJulianDayAndMilliseconds(day: number, msInDay: number): number;
91
+ export declare function deltaTPolynomial(year: number): number;
92
+ /**
93
+ * Terrestrial time minus the clock's time, in seconds, at a moment given in UTC: 69.184 from the last leap second in 2017 on (until another is added), exact from 1972, and delta T before.
94
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
95
+ * @returns Seconds to add to UTC to get TT.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * import { ttMinusUtc } from "@johnmorrisdotca/wakusei-planets/time";
100
+ *
101
+ * console.log(ttMinusUtc(Date.UTC(2026, 10, 7)));
102
+ * ```
103
+ */
104
+ export declare function ttMinusUtc(when: When): number;
105
+ /**
106
+ * A moment in UTC as the same moment in terrestrial time, on the same scale of milliseconds since 1970.
107
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
108
+ * @returns The same moment in TT, in milliseconds.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * import { toTerrestrialTime } from "@johnmorrisdotca/wakusei-planets/time";
113
+ *
114
+ * const utc = Date.UTC(2000, 0, 1, 12);
115
+ * console.log((toTerrestrialTime(utc) - utc) / 1000);
116
+ * ```
117
+ */
118
+ export declare function toTerrestrialTime(when: When): number;
119
+ /**
120
+ * The Julian date of J2000.0, 2000-01-01 12:00 TT: the epoch of the planets' theories.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * import { J2000_JD, toJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
125
+ *
126
+ * console.log(J2000_JD === toJulianDate(Date.UTC(2000, 0, 1, 12)));
127
+ * ```
128
+ */
129
+ export declare const J2000_JD = 2451545;
130
+ /**
131
+ * J2000.0, 2000-01-01 12:00, as milliseconds since 1970 counted on a clock that reads TT: a terrestrial time is this far from it.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * import { J2000_MS } from "@johnmorrisdotca/wakusei-planets/time";
136
+ *
137
+ * console.log(new Date(J2000_MS).toISOString());
138
+ * ```
139
+ */
140
+ export declare const J2000_MS = 946728000000;
141
+ /**
142
+ * The Julian date of the Besselian epoch B1950.0, 1950 January 0.923 (Meeus).
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * import { B1950_JD } from "@johnmorrisdotca/wakusei-planets/time";
147
+ *
148
+ * console.log(B1950_JD);
149
+ * ```
150
+ */
151
+ export declare const B1950_JD = 2433282.4235;
152
+ /**
153
+ * The Julian date of the Unix epoch, 1970-01-01T00:00:00Z (UTC).
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * import { UNIX_EPOCH_JULIAN_DATE, toJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
158
+ *
159
+ * console.log(UNIX_EPOCH_JULIAN_DATE === toJulianDate(0));
160
+ * ```
161
+ */
162
+ export declare const UNIX_EPOCH_JULIAN_DATE = 2440587.5;
163
+ /**
164
+ * A time as a string of its Julian date with six decimals, as the `julian` package gives it (a tenth of a second).
165
+ * @param when - A `Date` or milliseconds since 1970.
166
+ * @returns The Julian date as a string.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * import { julian } from "@johnmorrisdotca/wakusei-planets/time";
171
+ *
172
+ * console.log(julian(new Date("2013-11-21T10:47:02Z")));
173
+ * ```
174
+ */
175
+ export declare function julian(when: When): string;
176
+ /**
177
+ * The `Date` of a Julian date (a number, or the string `julian` gives), rounded to the millisecond.
178
+ * @param jd - The Julian date.
179
+ * @returns A `Date`.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * import { toDate } from "@johnmorrisdotca/wakusei-planets/time";
184
+ *
185
+ * console.log(toDate("2456617.949335").toISOString());
186
+ * ```
187
+ */
188
+ export declare function toDate(jd: number | string): Date;
189
+ /**
190
+ * Julian centuries (36,525 days) from J2000.0: the time argument of most theories. In terrestrial time unless `scale` says UTC.
191
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
192
+ * @param scale - `"tt"` (the default, what the theories want) or `"utc"` to count the clock's days.
193
+ * @returns Julian centuries, negative before 2000.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * import { julianCenturies } from "@johnmorrisdotca/wakusei-planets/time";
198
+ *
199
+ * console.log(julianCenturies(Date.UTC(2100, 0, 1, 12)).toFixed(6));
200
+ * ```
201
+ */
202
+ export declare function julianCenturies(when: When, scale?: "tt" | "utc"): number;
203
+ /**
204
+ * Julian millennia (365,250 days) from J2000.0, in terrestrial time unless `scale` says UTC: the time argument of the VSOP87 series.
205
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
206
+ * @param scale - `"tt"` (the default) or `"utc"`.
207
+ * @returns Julian millennia, negative before 2000.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * import { julianMillennia } from "@johnmorrisdotca/wakusei-planets/time";
212
+ *
213
+ * console.log(julianMillennia(Date.UTC(3000, 0, 1, 12)).toFixed(6));
214
+ * ```
215
+ */
216
+ export declare function julianMillennia(when: When, scale?: "tt" | "utc"): number;
217
+ /**
218
+ * TAI minus UTC in seconds: the leap seconds from 1972 (10 then, 37 since 2017), and before 1972 delta T less 32.184, which is how far the atomic scale would have been from the Earth's turn.
219
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
220
+ * @returns Seconds to add to UTC to get TAI.
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * import { taiMinusUtc } from "@johnmorrisdotca/wakusei-planets/time";
225
+ *
226
+ * console.log(taiMinusUtc(Date.UTC(2026, 10, 7)));
227
+ * ```
228
+ */
229
+ export declare function taiMinusUtc(when: When): number;
230
+ /**
231
+ * Delta T, terrestrial time minus universal time (UT1), in seconds. From 1972 it is the leap seconds plus 32.184 s less `dut1` (UT1 minus UTC, which the IERS publishes and is under 0.9 s: leave it out and it is taken as 0),
232
+ * before 1972 the polynomials of Espenak and Meeus (NASA). After the last leap second it is held at 69.184 s, the assumption JPL Horizons makes.
233
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
234
+ * @param dut1 - UT1 minus UTC in seconds, from the IERS; default 0.
235
+ * @returns Seconds.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * import { deltaT } from "@johnmorrisdotca/wakusei-planets/time";
240
+ *
241
+ * console.log(deltaT(Date.UTC(1900, 0, 1)).toFixed(1), deltaT(Date.UTC(2026, 10, 7)));
242
+ * ```
243
+ */
244
+ export declare function deltaT(when: When, dut1?: number): number;
package/dist/clock.js ADDED
@@ -0,0 +1,325 @@
1
+ // The clock under the sky: Julian dates, the leap seconds and delta T, and the step from the clock (UTC) to terrestrial time (TT), the dynamical time the planets' theories are written in. Pure: no DOM, no three.js.
2
+ // `time.ts` is the public entry that gathers this with the calendar, the time scales, sidereal time and Mars time.
3
+ // From 1972 the step is exact: TT is TAI plus 32.184 seconds and TAI is UTC plus the leap seconds (IERS Bulletin C). Before 1972 it is delta T, from the polynomials of Espenak and Meeus
4
+ // (NASA, https://eclipse.gsfc.nasa.gov/SEhelp/deltatpoly2004.html, for 1800 to 1972) and Morrison and Stephenson's parabola outside 1800 to 1972.
5
+ import { moment } from "./when.js";
6
+ export const DAY_MS = 86_400_000;
7
+ /** The Julian date of the Unix epoch, 1970-01-01T00:00:00Z. */
8
+ export const UNIX_EPOCH_JD = 2_440_587.5;
9
+ /** The Julian date of the Unix epoch in milliseconds: exact in a 64-bit number, so the day and the milliseconds of the day lose nothing. */
10
+ const JD_EPOCH_MS = 210_866_760_000_000;
11
+ const MJD_OFFSET = 2_400_000.5;
12
+ export const TT_MINUS_TAI_SECONDS = 32.184;
13
+ /** The length of a Gregorian year in milliseconds, for turning a time into a decimal year. */
14
+ export const YEAR_MS = 31_556_952_000;
15
+ /** TAI minus UTC, in seconds, from the date it took effect (IERS Bulletin C). After the last one, none has been added. */
16
+ export const LEAP_SECONDS = [
17
+ [Date.UTC(1972, 0, 1), 10], [Date.UTC(1972, 6, 1), 11], [Date.UTC(1973, 0, 1), 12], [Date.UTC(1974, 0, 1), 13], [Date.UTC(1975, 0, 1), 14], [Date.UTC(1976, 0, 1), 15],
18
+ [Date.UTC(1977, 0, 1), 16], [Date.UTC(1978, 0, 1), 17], [Date.UTC(1979, 0, 1), 18], [Date.UTC(1980, 0, 1), 19], [Date.UTC(1981, 6, 1), 20], [Date.UTC(1982, 6, 1), 21],
19
+ [Date.UTC(1983, 6, 1), 22], [Date.UTC(1985, 6, 1), 23], [Date.UTC(1988, 0, 1), 24], [Date.UTC(1990, 0, 1), 25], [Date.UTC(1991, 0, 1), 26], [Date.UTC(1992, 6, 1), 27],
20
+ [Date.UTC(1993, 6, 1), 28], [Date.UTC(1994, 6, 1), 29], [Date.UTC(1996, 0, 1), 30], [Date.UTC(1997, 6, 1), 31], [Date.UTC(1999, 0, 1), 32], [Date.UTC(2006, 0, 1), 33],
21
+ [Date.UTC(2009, 0, 1), 34], [Date.UTC(2012, 6, 1), 35], [Date.UTC(2015, 6, 1), 36], [Date.UTC(2017, 0, 1), 37],
22
+ ];
23
+ /**
24
+ * A time as a Julian date: the days since noon on 1 January 4713 BC (Julian calendar), as a number. A `Date` or milliseconds since 1970 in, the same scale out (UTC stays UTC).
25
+ * A number near today holds about a tenth of a millisecond; `toJulianDay` and `toMillisecondsInJulianDay` give the same instant with nothing lost.
26
+ * @param when - A `Date` or milliseconds since 1970.
27
+ * @returns The Julian date.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { toJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
32
+ *
33
+ * console.log(toJulianDate(Date.UTC(2013, 10, 21, 10, 47, 2)).toFixed(6));
34
+ * ```
35
+ */
36
+ export function toJulianDate(when) {
37
+ return moment(when) / DAY_MS + UNIX_EPOCH_JD;
38
+ }
39
+ /**
40
+ * The time, in milliseconds since 1970, of a Julian date.
41
+ * @param jd - The Julian date.
42
+ * @returns Milliseconds since 1970.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { fromJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
47
+ *
48
+ * console.log(new Date(fromJulianDate(2451545)).toISOString());
49
+ * ```
50
+ */
51
+ export function fromJulianDate(jd) {
52
+ return (jd - UNIX_EPOCH_JD) * DAY_MS;
53
+ }
54
+ /**
55
+ * The modified Julian date: the Julian date less 2,400,000.5, so a day starts at midnight and the numbers are shorter.
56
+ * @param when - A `Date` or milliseconds since 1970.
57
+ * @returns The modified Julian date.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import { modifiedJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
62
+ *
63
+ * console.log(modifiedJulianDate(Date.UTC(2000, 0, 1)));
64
+ * ```
65
+ */
66
+ export function modifiedJulianDate(when) {
67
+ return toJulianDate(when) - MJD_OFFSET;
68
+ }
69
+ /**
70
+ * The whole Julian day of a time. A Julian day starts at noon, so 11:59 and 12:00 are in different days.
71
+ * @param when - A `Date` or milliseconds since 1970.
72
+ * @returns The integer Julian day number.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * import { toJulianDay } from "@johnmorrisdotca/wakusei-planets/time";
77
+ *
78
+ * console.log(toJulianDay(Date.UTC(2000, 0, 1, 12)));
79
+ * ```
80
+ */
81
+ export function toJulianDay(when) {
82
+ return Math.floor((moment(when) + JD_EPOCH_MS) / DAY_MS);
83
+ }
84
+ /**
85
+ * The milliseconds since noon, the start of the Julian day, that a time is into it.
86
+ * @param when - A `Date` or milliseconds since 1970.
87
+ * @returns Milliseconds from 0 up to one day.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * import { toMillisecondsInJulianDay } from "@johnmorrisdotca/wakusei-planets/time";
92
+ *
93
+ * console.log(toMillisecondsInJulianDay(Date.UTC(2000, 0, 1, 18)));
94
+ * ```
95
+ */
96
+ export function toMillisecondsInJulianDay(when) {
97
+ const t = moment(when) + JD_EPOCH_MS;
98
+ return t - Math.floor(t / DAY_MS) * DAY_MS;
99
+ }
100
+ /**
101
+ * A time from a Julian day and the milliseconds into it, with no digit lost: `fromJulianDayAndMilliseconds(toJulianDay(t), toMillisecondsInJulianDay(t))` is `t`.
102
+ * @param day - The integer Julian day number.
103
+ * @param msInDay - Milliseconds since the day began at noon.
104
+ * @returns Milliseconds since 1970.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * import { fromJulianDayAndMilliseconds, toJulianDay, toMillisecondsInJulianDay } from "@johnmorrisdotca/wakusei-planets/time";
109
+ *
110
+ * const t = Date.UTC(2026, 10, 7, 9, 30, 15, 123);
111
+ * console.log(fromJulianDayAndMilliseconds(toJulianDay(t), toMillisecondsInJulianDay(t)) === t);
112
+ * ```
113
+ */
114
+ export function fromJulianDayAndMilliseconds(day, msInDay) {
115
+ return day * DAY_MS + msInDay - JD_EPOCH_MS;
116
+ }
117
+ export function deltaTPolynomial(year) {
118
+ if (year < 1800 || year >= 1972) {
119
+ const u = (year - 1820) / 100;
120
+ return -20 + 32 * u * u;
121
+ }
122
+ if (year < 1860) {
123
+ const t = year - 1800;
124
+ return 13.72 - 0.332447 * t + 0.0068612 * t ** 2 + 0.0041116 * t ** 3 - 0.00037436 * t ** 4 + 0.0000121272 * t ** 5 - 0.0000001699 * t ** 6 + 0.000000000875 * t ** 7;
125
+ }
126
+ if (year < 1900) {
127
+ const t = year - 1860;
128
+ return 7.62 + 0.5737 * t - 0.251754 * t ** 2 + 0.01680668 * t ** 3 - 0.0004473624 * t ** 4 + t ** 5 / 233174;
129
+ }
130
+ if (year < 1920) {
131
+ const t = year - 1900;
132
+ return -2.79 + 1.494119 * t - 0.0598939 * t ** 2 + 0.0061966 * t ** 3 - 0.000197 * t ** 4;
133
+ }
134
+ if (year < 1941) {
135
+ const t = year - 1920;
136
+ return 21.2 + 0.84493 * t - 0.0761 * t ** 2 + 0.0020936 * t ** 3;
137
+ }
138
+ if (year < 1961) {
139
+ const t = year - 1950;
140
+ return 29.07 + 0.407 * t - t ** 2 / 233 + t ** 3 / 2547;
141
+ }
142
+ const t = year - 1975;
143
+ return 45.45 + 1.067 * t - t ** 2 / 260 - t ** 3 / 718;
144
+ }
145
+ /**
146
+ * Terrestrial time minus the clock's time, in seconds, at a moment given in UTC: 69.184 from the last leap second in 2017 on (until another is added), exact from 1972, and delta T before.
147
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
148
+ * @returns Seconds to add to UTC to get TT.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * import { ttMinusUtc } from "@johnmorrisdotca/wakusei-planets/time";
153
+ *
154
+ * console.log(ttMinusUtc(Date.UTC(2026, 10, 7)));
155
+ * ```
156
+ */
157
+ export function ttMinusUtc(when) {
158
+ const ms = moment(when);
159
+ let i = LEAP_SECONDS.length;
160
+ while (i) {
161
+ const [from, seconds] = LEAP_SECONDS[--i];
162
+ if (ms >= from)
163
+ return seconds + TT_MINUS_TAI_SECONDS;
164
+ }
165
+ return deltaTPolynomial(1970 + ms / YEAR_MS);
166
+ }
167
+ /**
168
+ * A moment in UTC as the same moment in terrestrial time, on the same scale of milliseconds since 1970.
169
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
170
+ * @returns The same moment in TT, in milliseconds.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * import { toTerrestrialTime } from "@johnmorrisdotca/wakusei-planets/time";
175
+ *
176
+ * const utc = Date.UTC(2000, 0, 1, 12);
177
+ * console.log((toTerrestrialTime(utc) - utc) / 1000);
178
+ * ```
179
+ */
180
+ export function toTerrestrialTime(when) {
181
+ const ms = moment(when);
182
+ return ms + ttMinusUtc(ms) * 1000;
183
+ }
184
+ /**
185
+ * The Julian date of J2000.0, 2000-01-01 12:00 TT: the epoch of the planets' theories.
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * import { J2000_JD, toJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
190
+ *
191
+ * console.log(J2000_JD === toJulianDate(Date.UTC(2000, 0, 1, 12)));
192
+ * ```
193
+ */
194
+ export const J2000_JD = 2_451_545;
195
+ /**
196
+ * J2000.0, 2000-01-01 12:00, as milliseconds since 1970 counted on a clock that reads TT: a terrestrial time is this far from it.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * import { J2000_MS } from "@johnmorrisdotca/wakusei-planets/time";
201
+ *
202
+ * console.log(new Date(J2000_MS).toISOString());
203
+ * ```
204
+ */
205
+ export const J2000_MS = 946_728_000_000;
206
+ /**
207
+ * The Julian date of the Besselian epoch B1950.0, 1950 January 0.923 (Meeus).
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * import { B1950_JD } from "@johnmorrisdotca/wakusei-planets/time";
212
+ *
213
+ * console.log(B1950_JD);
214
+ * ```
215
+ */
216
+ export const B1950_JD = 2_433_282.4235;
217
+ /**
218
+ * The Julian date of the Unix epoch, 1970-01-01T00:00:00Z (UTC).
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * import { UNIX_EPOCH_JULIAN_DATE, toJulianDate } from "@johnmorrisdotca/wakusei-planets/time";
223
+ *
224
+ * console.log(UNIX_EPOCH_JULIAN_DATE === toJulianDate(0));
225
+ * ```
226
+ */
227
+ export const UNIX_EPOCH_JULIAN_DATE = UNIX_EPOCH_JD;
228
+ /**
229
+ * A time as a string of its Julian date with six decimals, as the `julian` package gives it (a tenth of a second).
230
+ * @param when - A `Date` or milliseconds since 1970.
231
+ * @returns The Julian date as a string.
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * import { julian } from "@johnmorrisdotca/wakusei-planets/time";
236
+ *
237
+ * console.log(julian(new Date("2013-11-21T10:47:02Z")));
238
+ * ```
239
+ */
240
+ export function julian(when) {
241
+ return toJulianDate(when).toFixed(6);
242
+ }
243
+ /**
244
+ * The `Date` of a Julian date (a number, or the string `julian` gives), rounded to the millisecond.
245
+ * @param jd - The Julian date.
246
+ * @returns A `Date`.
247
+ *
248
+ * @example
249
+ * ```ts
250
+ * import { toDate } from "@johnmorrisdotca/wakusei-planets/time";
251
+ *
252
+ * console.log(toDate("2456617.949335").toISOString());
253
+ * ```
254
+ */
255
+ export function toDate(jd) {
256
+ const value = typeof jd === "string" ? Number(jd) : jd;
257
+ if (!Number.isFinite(value))
258
+ throw new RangeError(`jd must be a finite Julian date, got ${String(jd)}`);
259
+ return new Date(Math.round(fromJulianDate(value)));
260
+ }
261
+ /**
262
+ * Julian centuries (36,525 days) from J2000.0: the time argument of most theories. In terrestrial time unless `scale` says UTC.
263
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
264
+ * @param scale - `"tt"` (the default, what the theories want) or `"utc"` to count the clock's days.
265
+ * @returns Julian centuries, negative before 2000.
266
+ *
267
+ * @example
268
+ * ```ts
269
+ * import { julianCenturies } from "@johnmorrisdotca/wakusei-planets/time";
270
+ *
271
+ * console.log(julianCenturies(Date.UTC(2100, 0, 1, 12)).toFixed(6));
272
+ * ```
273
+ */
274
+ export function julianCenturies(when, scale = "tt") {
275
+ const ms = scale === "tt" ? toTerrestrialTime(when) : moment(when);
276
+ return (ms - J2000_MS) / (36_525 * DAY_MS);
277
+ }
278
+ /**
279
+ * Julian millennia (365,250 days) from J2000.0, in terrestrial time unless `scale` says UTC: the time argument of the VSOP87 series.
280
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
281
+ * @param scale - `"tt"` (the default) or `"utc"`.
282
+ * @returns Julian millennia, negative before 2000.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * import { julianMillennia } from "@johnmorrisdotca/wakusei-planets/time";
287
+ *
288
+ * console.log(julianMillennia(Date.UTC(3000, 0, 1, 12)).toFixed(6));
289
+ * ```
290
+ */
291
+ export function julianMillennia(when, scale = "tt") {
292
+ return julianCenturies(when, scale) / 10;
293
+ }
294
+ /**
295
+ * TAI minus UTC in seconds: the leap seconds from 1972 (10 then, 37 since 2017), and before 1972 delta T less 32.184, which is how far the atomic scale would have been from the Earth's turn.
296
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
297
+ * @returns Seconds to add to UTC to get TAI.
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * import { taiMinusUtc } from "@johnmorrisdotca/wakusei-planets/time";
302
+ *
303
+ * console.log(taiMinusUtc(Date.UTC(2026, 10, 7)));
304
+ * ```
305
+ */
306
+ export function taiMinusUtc(when) {
307
+ return ttMinusUtc(when) - TT_MINUS_TAI_SECONDS;
308
+ }
309
+ /**
310
+ * Delta T, terrestrial time minus universal time (UT1), in seconds. From 1972 it is the leap seconds plus 32.184 s less `dut1` (UT1 minus UTC, which the IERS publishes and is under 0.9 s: leave it out and it is taken as 0),
311
+ * before 1972 the polynomials of Espenak and Meeus (NASA). After the last leap second it is held at 69.184 s, the assumption JPL Horizons makes.
312
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
313
+ * @param dut1 - UT1 minus UTC in seconds, from the IERS; default 0.
314
+ * @returns Seconds.
315
+ *
316
+ * @example
317
+ * ```ts
318
+ * import { deltaT } from "@johnmorrisdotca/wakusei-planets/time";
319
+ *
320
+ * console.log(deltaT(Date.UTC(1900, 0, 1)).toFixed(1), deltaT(Date.UTC(2026, 10, 7)));
321
+ * ```
322
+ */
323
+ export function deltaT(when, dut1 = 0) {
324
+ return ttMinusUtc(when) - dut1;
325
+ }