@johnmorrisdotca/wakusei-planets 0.17.2 → 0.20.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 (53) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/NOTICE.md +8 -4
  3. package/README.md +9 -5
  4. package/dist/calendar.d.ts +119 -0
  5. package/dist/calendar.js +154 -0
  6. package/dist/clock.d.ts +244 -0
  7. package/dist/clock.js +326 -0
  8. package/dist/coordinates.d.ts +177 -0
  9. package/dist/coordinates.js +169 -0
  10. package/dist/data/lunar.d.ts +4 -0
  11. package/dist/data/lunar.js +548 -0
  12. package/dist/data/orbits.d.ts +5 -1
  13. package/dist/data/orbits.js +26 -26
  14. package/dist/data/pluto.d.ts +6 -0
  15. package/dist/data/pluto.js +29 -0
  16. package/dist/data/tables.d.ts +41 -0
  17. package/dist/data/tables.js +37 -0
  18. package/dist/data/vsop.d.ts +21 -0
  19. package/dist/data/vsop.js +27 -0
  20. package/dist/element.d.ts +5 -3
  21. package/dist/element.js +22 -9
  22. package/dist/ephemeris.d.ts +4 -16
  23. package/dist/ephemeris.js +76 -33
  24. package/dist/live.js +1 -1
  25. package/dist/mars.d.ts +41 -0
  26. package/dist/mars.js +52 -0
  27. package/dist/scales.d.ts +46 -0
  28. package/dist/scales.js +75 -0
  29. package/dist/sidereal.d.ts +66 -0
  30. package/dist/sidereal.js +88 -0
  31. package/dist/sky/engine.d.ts +46 -0
  32. package/dist/sky/engine.js +145 -0
  33. package/dist/sky/events.d.ts +37 -0
  34. package/dist/sky/events.js +133 -0
  35. package/dist/suncalc-legacy.d.ts +187 -0
  36. package/dist/suncalc-legacy.js +168 -0
  37. package/dist/suncalc.d.ts +209 -0
  38. package/dist/suncalc.js +173 -0
  39. package/dist/tables/all.d.ts +2 -0
  40. package/dist/tables/all.js +2 -0
  41. package/dist/tables/ceres.d.ts +1 -0
  42. package/dist/tables/ceres.js +57 -0
  43. package/dist/tables/dwarfs.d.ts +1 -0
  44. package/dist/tables/dwarfs.js +59 -0
  45. package/dist/theory.d.ts +20 -0
  46. package/dist/theory.js +98 -0
  47. package/dist/time.d.ts +6 -0
  48. package/dist/time.js +7 -0
  49. package/dist/version.d.ts +1 -1
  50. package/dist/version.js +1 -1
  51. package/dist/when.d.ts +16 -0
  52. package/dist/when.js +18 -0
  53. package/package.json +71 -5
@@ -0,0 +1,133 @@
1
+ // The searches behind the sun and moon times: the day a time belongs to, the instants when a body crosses an altitude or the meridian, found by sampling and bisection on this package's own positions.
2
+ // Shared by `suncalc` and `suncalc/legacy`. Pure: no DOM, no three.js.
3
+ import { horizontalOf, moonEquatorial, sunEquatorial } from "./engine.js";
4
+ const DAY_MS = 86_400_000;
5
+ const MOON_RADIUS_KM = 1737.4;
6
+ const SAMPLE_MS = 6 * 60_000;
7
+ /** A date or milliseconds since 1970 as milliseconds, throwing on anything that is not a moment. */
8
+ export function moment(date, name = "date") {
9
+ const ms = typeof date === "number" ? date : date.getTime();
10
+ if (!Number.isFinite(ms))
11
+ throw new RangeError(`${name} must be a valid Date or a finite number of milliseconds, got ${String(date)}`);
12
+ return ms;
13
+ }
14
+ export function place(latitude, longitude, heightM = 0) {
15
+ if (!Number.isFinite(latitude) || latitude < -90 || latitude > 90)
16
+ throw new RangeError(`lat must be between -90 and 90 degrees, got ${latitude}`);
17
+ if (!Number.isFinite(longitude))
18
+ throw new RangeError(`lng must be a finite number of degrees, got ${longitude}`);
19
+ if (!Number.isFinite(heightM))
20
+ throw new RangeError(`height must be a finite number of metres, got ${heightM}`);
21
+ return { latitude, longitude, heightM };
22
+ }
23
+ /** The window of a day: the local calendar day when a UTC offset is given, otherwise the solar day from one solar midnight to the next that holds the moment. Milliseconds, the end not included. */
24
+ export function dayWindow(ms, observer, utcOffset) {
25
+ if (utcOffset !== undefined) {
26
+ if (!Number.isFinite(utcOffset))
27
+ throw new RangeError(`utcOffset must be a finite number of minutes east of UTC, got ${utcOffset}`);
28
+ const start = Math.floor((ms + utcOffset * 60_000) / DAY_MS) * DAY_MS - utcOffset * 60_000;
29
+ return [start, start + DAY_MS];
30
+ }
31
+ const nadir = nearestHourAngle(ms, observer, 180);
32
+ const previous = nadir <= ms ? nadir : nearestHourAngle(nadir - DAY_MS, observer, 180);
33
+ const next = nearestHourAngle(previous + DAY_MS, observer, 180);
34
+ return [previous, next];
35
+ }
36
+ /** The Sun's hour angle in degrees from -180 to 180 for an observer (positive west, 0 on the meridian). */
37
+ export function sunHourAngle(ms, observer) {
38
+ return horizontalOf(sunEquatorial(ms), ms, observer).hourAngle;
39
+ }
40
+ /** The instant nearest to `ms` when the Sun's hour angle is `target` degrees (0 noon, 180 midnight), by Newton steps on the Sun's turn of about 360 degrees a day. */
41
+ export function nearestHourAngle(ms, observer, target) {
42
+ let t = ms;
43
+ for (let i = 0; i < 8; i += 1) {
44
+ const difference = ((((sunHourAngle(t, observer) - target) % 360) + 540) % 360) - 180;
45
+ t -= (difference / 360) * DAY_MS;
46
+ if (Math.abs(difference) < 1e-6)
47
+ break;
48
+ }
49
+ return t;
50
+ }
51
+ /** The zero crossings of `f` in [start, end), as [instant, true when f goes up], found by sampling every `step` milliseconds and bisecting each bracket to about a tenth of a second. */
52
+ export function crossings(f, start, end, step) {
53
+ const found = [];
54
+ let t0 = start;
55
+ let f0 = f(t0);
56
+ while (t0 < end) {
57
+ const t1 = Math.min(t0 + step, end);
58
+ const f1 = f(t1);
59
+ if ((f0 < 0 && f1 >= 0) || (f0 >= 0 && f1 < 0)) {
60
+ let lo = t0;
61
+ let hi = t1;
62
+ let flo = f0;
63
+ for (let i = 0; i < 26; i += 1) {
64
+ const mid = (lo + hi) / 2;
65
+ const fm = f(mid);
66
+ if ((fm < 0) === (flo < 0)) {
67
+ lo = mid;
68
+ flo = fm;
69
+ }
70
+ else
71
+ hi = mid;
72
+ }
73
+ const at = (lo + hi) / 2;
74
+ if (at < end)
75
+ found.push([at, f0 < 0]);
76
+ }
77
+ t0 = t1;
78
+ f0 = f1;
79
+ }
80
+ return found;
81
+ }
82
+ /** The height of the horizon below level for an observer `height` metres up, in degrees (the dip, with the refraction along it). */
83
+ export const dip = (heightM) => 0.0347 * Math.sqrt(Math.max(0, heightM));
84
+ /**
85
+ * The Sun's day at a place: `utcOffset` in minutes east of UTC picks the local calendar day holding `ms`, and `undefined` the solar day (one solar midnight to the next).
86
+ * A row at the horizon (between -1 and 0 degrees) is lowered by the dip of the horizon for an observer `heightM` metres up.
87
+ */
88
+ export function sunDay(ms, observer, utcOffset, rows) {
89
+ const [start, end] = dayWindow(ms, observer, utcOffset);
90
+ const altitude = (t) => horizontalOf(sunEquatorial(t), t, observer).altitude;
91
+ const solarNoon = nearestHourAngle(start + 12 * 3_600_000, observer, 0);
92
+ const noon = solarNoon < start - 1000 ? nearestHourAngle(solarNoon + DAY_MS, observer, 0) : solarNoon;
93
+ // the solar midnight that leads into this day's solar noon, as suncalc does: it can fall a few minutes before a calendar day begins
94
+ const nadir = nearestHourAngle(noon - DAY_MS / 2, observer, 180);
95
+ const cache = new Map();
96
+ const sampled = (t) => {
97
+ let v = cache.get(t);
98
+ if (v === undefined) {
99
+ v = altitude(t);
100
+ cache.set(t, v);
101
+ }
102
+ return v;
103
+ };
104
+ const events = new Map();
105
+ const height = observer.heightM ?? 0;
106
+ for (const [angle, morning, evening] of rows) {
107
+ const level = angle - (angle > -1 && angle < 0 ? dip(height) : 0);
108
+ const found = crossings((t) => sampled(t) - level, start, end, 10 * 60_000);
109
+ events.set(morning, found.find(([, rising]) => rising)?.[0] ?? null);
110
+ events.set(evening, found.find(([, rising]) => !rising)?.[0] ?? null);
111
+ }
112
+ const sunrise = events.get("sunrise");
113
+ const sunset = events.get("sunset");
114
+ const none = sunrise === null && sunset === null;
115
+ const high = none && altitude(noon) > -0.833 - dip(height);
116
+ return { noon, nadir, events, alwaysUp: high, alwaysDown: none && !high };
117
+ }
118
+ /** The Moon between two instants at a place: rise and set (the upper limb at the horizon through 34 arc-minutes of refraction), the upper and lower meridian crossings, and whether it stays up or down. */
119
+ export function moonDay(start, end, observer) {
120
+ // the centre's geometric altitude plus the refraction and the radius of the disc is zero at the rise and the set
121
+ const above = (t) => {
122
+ const h = horizontalOf(moonEquatorial(t), t, observer);
123
+ return h.altitude + 34 / 60 + (Math.asin(MOON_RADIUS_KM / h.distanceKm) * 180) / Math.PI;
124
+ };
125
+ const meridian = (t) => Math.sin((horizontalOf(moonEquatorial(t), t, observer).hourAngle * Math.PI) / 180);
126
+ const edges = crossings(above, start, end, SAMPLE_MS);
127
+ const turns = crossings(meridian, start, end, SAMPLE_MS);
128
+ const rise = edges.find(([, rising]) => rising)?.[0] ?? null;
129
+ const set = edges.find(([, rising]) => !rising)?.[0] ?? null;
130
+ const none = rise === null && set === null;
131
+ const up = none && above(start) > 0;
132
+ return { rise, set, transit: turns.find(([, rising]) => rising)?.[0] ?? null, lowerTransit: turns.find(([, rising]) => !rising)?.[0] ?? null, alwaysUp: up, alwaysDown: none && !up };
133
+ }
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Where a body is: the azimuth in radians from south toward west (0 south, a quarter turn west) and the altitude in radians.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { getPosition, type LegacyPosition } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
7
+ *
8
+ * const p: LegacyPosition = getPosition(new Date(Date.UTC(2026, 5, 21, 12)), 51.5, -0.1);
9
+ * console.log(p.altitude > 0);
10
+ * ```
11
+ */
12
+ export interface LegacyPosition {
13
+ azimuth: number;
14
+ altitude: number;
15
+ }
16
+ /**
17
+ * The Moon in a place's sky: the LegacyPosition, the distance in kilometres and the parallactic angle in radians.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { getMoonPosition, type LegacyMoonPosition } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
22
+ *
23
+ * const p: LegacyMoonPosition = getMoonPosition(new Date(Date.UTC(2026, 10, 7, 12)), 35.7, 139.7);
24
+ * console.log(p.distance > 350000);
25
+ * ```
26
+ */
27
+ export interface LegacyMoonPosition extends LegacyPosition {
28
+ distance: number;
29
+ parallacticAngle: number;
30
+ }
31
+ /**
32
+ * The Moon's illumination in suncalc 1's terms: the lit fraction, the phase 0 to 1, and the bright limb's angle in radians, negative while the Moon waxes and positive while it wanes.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { getMoonIllumination, type LegacyMoonIllumination } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
37
+ *
38
+ * const m: LegacyMoonIllumination = getMoonIllumination(new Date(Date.UTC(2026, 10, 24, 14)));
39
+ * console.log(m.fraction >= 0 && m.fraction <= 1);
40
+ * ```
41
+ */
42
+ export interface LegacyMoonIllumination {
43
+ fraction: number;
44
+ phase: number;
45
+ angle: number;
46
+ }
47
+ /**
48
+ * The Sun's times with every one a `Date`: an invalid `Date` (`NaN`) when the Sun does not reach that height that day.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { getTimes, type LegacySunTimes } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
53
+ *
54
+ * const t: LegacySunTimes = getTimes(new Date(Date.UTC(2026, 5, 21, 12)), 51.5, -0.1);
55
+ * console.log(Object.keys(t).length);
56
+ * ```
57
+ */
58
+ export type LegacySunTimes = Record<string, Date>;
59
+ /**
60
+ * The Moon's rise and set; `alwaysUp` or `alwaysDown` is `true` when the Moon never rises or sets in the day.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import { getMoonTimes, type LegacyMoonTimes } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
65
+ *
66
+ * const t: LegacyMoonTimes = getMoonTimes(new Date(Date.UTC(2026, 10, 7, 12)), 35.7, 139.7, true);
67
+ * console.log(t.rise === undefined || t.rise instanceof Date);
68
+ * ```
69
+ */
70
+ export interface LegacyMoonTimes {
71
+ rise?: Date;
72
+ set?: Date;
73
+ alwaysUp?: boolean;
74
+ alwaysDown?: boolean;
75
+ }
76
+ /**
77
+ * The times suncalc 1 gives, as `[altitude in degrees, morning name, evening name]`.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * import { times } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
82
+ *
83
+ * console.log(times.length);
84
+ * ```
85
+ */
86
+ export declare const times: Array<[number, string, string]>;
87
+ /**
88
+ * Add a time to the ones `getTimes` gives: when the Sun reaches an altitude in degrees.
89
+ *
90
+ * @param angle - The altitude in degrees.
91
+ * @param riseName - The name when the Sun goes up through it.
92
+ * @param setName - The name when the Sun goes down through it.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import { addTime, times } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
97
+ *
98
+ * addTime(-4, "morningBlue", "eveningBlue");
99
+ * console.log(times.some((row) => row[1] === "morningBlue"));
100
+ * ```
101
+ */
102
+ export declare function addTime(angle: number, riseName: string, setName: string): void;
103
+ /**
104
+ * The Sun's place in the sky, as suncalc 1 gave it: the altitude has no refraction in it.
105
+ *
106
+ * @param date - The moment.
107
+ * @param lat - Degrees north.
108
+ * @param lng - Degrees east.
109
+ * @returns The azimuth in radians from south toward west and the altitude in radians.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * import { getPosition } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
114
+ *
115
+ * const { altitude } = getPosition(new Date(Date.UTC(2026, 5, 21, 12)), 51.5, -0.1);
116
+ * console.log((altitude * 180 / Math.PI).toFixed(0));
117
+ * ```
118
+ */
119
+ export declare function getPosition(date: Date | number, lat: number, lng: number): LegacyPosition;
120
+ /**
121
+ * The Moon's place in the sky, as suncalc 1 gave it: the altitude includes refraction.
122
+ *
123
+ * @param date - The moment.
124
+ * @param lat - Degrees north.
125
+ * @param lng - Degrees east.
126
+ * @returns The azimuth and altitude in radians, the distance in kilometres and the parallactic angle in radians.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * import { getMoonPosition } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
131
+ *
132
+ * const { distance } = getMoonPosition(new Date(Date.UTC(2026, 10, 7, 12)), 35.7, 139.7);
133
+ * console.log(Math.round(distance / 1000));
134
+ * ```
135
+ */
136
+ export declare function getMoonPosition(date: Date | number, lat: number, lng: number): LegacyMoonPosition;
137
+ /**
138
+ * The Moon's phase, as suncalc 1 gave it.
139
+ *
140
+ * @param date - The moment; now when left out.
141
+ * @returns The lit fraction, the phase 0 to 1 and the bright limb's angle in radians (negative while waxing).
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * import { getMoonIllumination } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
146
+ *
147
+ * const { fraction, angle } = getMoonIllumination(new Date(Date.UTC(2026, 10, 24, 14)));
148
+ * console.log(fraction.toFixed(2), angle < 0);
149
+ * ```
150
+ */
151
+ export declare function getMoonIllumination(date?: Date | number): LegacyMoonIllumination;
152
+ /**
153
+ * The Sun's times for the solar day nearest a moment, as suncalc 1 gave them: every time a `Date`, invalid when it does not happen.
154
+ *
155
+ * @param date - Any moment of the day.
156
+ * @param lat - Degrees north.
157
+ * @param lng - Degrees east.
158
+ * @param height - The observer's height in metres (default 0).
159
+ * @returns The times by name.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { getTimes } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
164
+ *
165
+ * const t = getTimes(new Date(Date.UTC(2026, 5, 21, 12)), 51.5, -0.1);
166
+ * console.log(t.sunrise!.getTime() < t.sunset!.getTime());
167
+ * ```
168
+ */
169
+ export declare function getTimes(date: Date | number, lat: number, lng: number, height?: number): LegacySunTimes;
170
+ /**
171
+ * The Moon's rise and set over the 24 hours from midnight of the moment's day, in the machine's zone or, with `inUTC`, in UTC.
172
+ *
173
+ * @param date - Any moment of the day.
174
+ * @param lat - Degrees north.
175
+ * @param lng - Degrees east.
176
+ * @param inUTC - Search the UTC day instead of the local one.
177
+ * @returns `rise` and `set` when they happen, and `alwaysUp` or `alwaysDown` when the Moon stays up or down.
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * import { getMoonTimes } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
182
+ *
183
+ * const t = getMoonTimes(new Date(Date.UTC(2026, 10, 7, 12)), 35.7, 139.7, true);
184
+ * console.log(t.rise === undefined || t.rise instanceof Date);
185
+ * ```
186
+ */
187
+ export declare function getMoonTimes(date: Date | number, lat: number, lng: number, inUTC?: boolean): LegacyMoonTimes;
@@ -0,0 +1,168 @@
1
+ // The sun and moon from a place with the conventions of suncalc 1.x, so code written for it changes only its import: angles in radians, the azimuth measured from south toward west, the Sun's altitude
2
+ // without refraction and the Moon's with it, an event that does not happen as an invalid `Date`, the Moon's day running from midnight (local, or UTC) for 24 hours, and a bright-limb angle that is
3
+ // negative while the Moon waxes. The numbers come from the same engine as `suncalc` (VSOP87B and ELP2000-82B, seconds of time). Pure: no DOM, no three.js.
4
+ import { horizontalOf, moonEquatorial, moonPhase, refraction, sunEquatorial } from "./sky/engine.js";
5
+ import { moment, moonDay, place, sunDay } from "./sky/events.js";
6
+ const RAD = Math.PI / 180;
7
+ /** An azimuth in degrees clockwise from north as radians from south toward west (south 0, west a quarter turn). */
8
+ const fromSouth = (azimuth) => (((azimuth - 180 + 540) % 360) - 180) * RAD;
9
+ /**
10
+ * The times suncalc 1 gives, as `[altitude in degrees, morning name, evening name]`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { times } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
15
+ *
16
+ * console.log(times.length);
17
+ * ```
18
+ */
19
+ export const times = [
20
+ [-0.833, "sunrise", "sunset"],
21
+ [-0.3, "sunriseEnd", "sunsetStart"],
22
+ [-6, "dawn", "dusk"],
23
+ [-12, "nauticalDawn", "nauticalDusk"],
24
+ [-18, "nightEnd", "night"],
25
+ [6, "goldenHourEnd", "goldenHour"],
26
+ ];
27
+ /**
28
+ * Add a time to the ones `getTimes` gives: when the Sun reaches an altitude in degrees.
29
+ *
30
+ * @param angle - The altitude in degrees.
31
+ * @param riseName - The name when the Sun goes up through it.
32
+ * @param setName - The name when the Sun goes down through it.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { addTime, times } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
37
+ *
38
+ * addTime(-4, "morningBlue", "eveningBlue");
39
+ * console.log(times.some((row) => row[1] === "morningBlue"));
40
+ * ```
41
+ */
42
+ export function addTime(angle, riseName, setName) {
43
+ times.push([angle, riseName, setName]);
44
+ }
45
+ /**
46
+ * The Sun's place in the sky, as suncalc 1 gave it: the altitude has no refraction in it.
47
+ *
48
+ * @param date - The moment.
49
+ * @param lat - Degrees north.
50
+ * @param lng - Degrees east.
51
+ * @returns The azimuth in radians from south toward west and the altitude in radians.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { getPosition } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
56
+ *
57
+ * const { altitude } = getPosition(new Date(Date.UTC(2026, 5, 21, 12)), 51.5, -0.1);
58
+ * console.log((altitude * 180 / Math.PI).toFixed(0));
59
+ * ```
60
+ */
61
+ export function getPosition(date, lat, lng) {
62
+ const ms = moment(date);
63
+ const h = horizontalOf(sunEquatorial(ms), ms, place(lat, lng));
64
+ return { azimuth: fromSouth(h.azimuth), altitude: h.altitude * RAD };
65
+ }
66
+ /**
67
+ * The Moon's place in the sky, as suncalc 1 gave it: the altitude includes refraction.
68
+ *
69
+ * @param date - The moment.
70
+ * @param lat - Degrees north.
71
+ * @param lng - Degrees east.
72
+ * @returns The azimuth and altitude in radians, the distance in kilometres and the parallactic angle in radians.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * import { getMoonPosition } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
77
+ *
78
+ * const { distance } = getMoonPosition(new Date(Date.UTC(2026, 10, 7, 12)), 35.7, 139.7);
79
+ * console.log(Math.round(distance / 1000));
80
+ * ```
81
+ */
82
+ export function getMoonPosition(date, lat, lng) {
83
+ const ms = moment(date);
84
+ const moon = moonEquatorial(ms);
85
+ const h = horizontalOf(moon, ms, place(lat, lng));
86
+ return { azimuth: fromSouth(h.azimuth), altitude: (h.altitude + refraction(h.altitude)) * RAD, distance: moon.distanceKm, parallacticAngle: h.parallacticAngle * RAD };
87
+ }
88
+ /**
89
+ * The Moon's phase, as suncalc 1 gave it.
90
+ *
91
+ * @param date - The moment; now when left out.
92
+ * @returns The lit fraction, the phase 0 to 1 and the bright limb's angle in radians (negative while waxing).
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import { getMoonIllumination } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
97
+ *
98
+ * const { fraction, angle } = getMoonIllumination(new Date(Date.UTC(2026, 10, 24, 14)));
99
+ * console.log(fraction.toFixed(2), angle < 0);
100
+ * ```
101
+ */
102
+ export function getMoonIllumination(date = new Date()) {
103
+ const p = moonPhase(moment(date));
104
+ // suncalc 1 reckons the angle from the Moon's own side of the Sun, which makes it negative while the Moon waxes
105
+ const wrapped = (((p.angle + 180) % 360) + 360) % 360 - 180;
106
+ return { fraction: p.fraction, phase: p.phase, angle: (p.waxing ? -Math.abs(wrapped) : Math.abs(wrapped)) * RAD };
107
+ }
108
+ /**
109
+ * The Sun's times for the solar day nearest a moment, as suncalc 1 gave them: every time a `Date`, invalid when it does not happen.
110
+ *
111
+ * @param date - Any moment of the day.
112
+ * @param lat - Degrees north.
113
+ * @param lng - Degrees east.
114
+ * @param height - The observer's height in metres (default 0).
115
+ * @returns The times by name.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * import { getTimes } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
120
+ *
121
+ * const t = getTimes(new Date(Date.UTC(2026, 5, 21, 12)), 51.5, -0.1);
122
+ * console.log(t.sunrise!.getTime() < t.sunset!.getTime());
123
+ * ```
124
+ */
125
+ export function getTimes(date, lat, lng, height = 0) {
126
+ const day = sunDay(moment(date), place(lat, lng, height), undefined, times);
127
+ const result = { solarNoon: new Date(day.noon), nadir: new Date(day.nadir) };
128
+ for (const [name, at] of day.events)
129
+ result[name] = new Date(at ?? Number.NaN);
130
+ return result;
131
+ }
132
+ /**
133
+ * The Moon's rise and set over the 24 hours from midnight of the moment's day, in the machine's zone or, with `inUTC`, in UTC.
134
+ *
135
+ * @param date - Any moment of the day.
136
+ * @param lat - Degrees north.
137
+ * @param lng - Degrees east.
138
+ * @param inUTC - Search the UTC day instead of the local one.
139
+ * @returns `rise` and `set` when they happen, and `alwaysUp` or `alwaysDown` when the Moon stays up or down.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * import { getMoonTimes } from "@johnmorrisdotca/wakusei-planets/suncalc/legacy";
144
+ *
145
+ * const t = getMoonTimes(new Date(Date.UTC(2026, 10, 7, 12)), 35.7, 139.7, true);
146
+ * console.log(t.rise === undefined || t.rise instanceof Date);
147
+ * ```
148
+ */
149
+ export function getMoonTimes(date, lat, lng, inUTC = false) {
150
+ const day = new Date(moment(date));
151
+ if (inUTC)
152
+ day.setUTCHours(0, 0, 0, 0);
153
+ else
154
+ day.setHours(0, 0, 0, 0);
155
+ const start = day.getTime();
156
+ const observer = place(lat, lng);
157
+ const found = moonDay(start, start + 24 * 3_600_000, observer);
158
+ const result = {};
159
+ if (found.rise !== null)
160
+ result.rise = new Date(found.rise);
161
+ if (found.set !== null)
162
+ result.set = new Date(found.set);
163
+ if (found.alwaysUp)
164
+ result.alwaysUp = true;
165
+ if (found.alwaysDown)
166
+ result.alwaysDown = true;
167
+ return result;
168
+ }