[engine] Sun: Compute sunrise, solar noon and sunset on the device, with polar day and night, checked against published tables
This commit is contained in:
+342
@@ -0,0 +1,342 @@
|
||||
{
|
||||
"source": "https://aa.usno.navy.mil/api/rstt/oneday (U.S. Naval Observatory, Astronomical Applications). Times are HH:MM on the local clock at utcOffset; null means the event does not happen that day.",
|
||||
"recorded": "2026-11-01",
|
||||
"rows": [
|
||||
{
|
||||
"city": "Prague",
|
||||
"lat": 50.0755,
|
||||
"lon": 14.4378,
|
||||
"timeZone": "Europe/Prague",
|
||||
"day": "2026-03-28",
|
||||
"utcOffset": 1,
|
||||
"why": "the day before DST starts",
|
||||
"rise": "05:48",
|
||||
"transit": "12:07",
|
||||
"set": "18:28"
|
||||
},
|
||||
{
|
||||
"city": "Prague",
|
||||
"lat": 50.0755,
|
||||
"lon": 14.4378,
|
||||
"timeZone": "Europe/Prague",
|
||||
"day": "2026-03-29",
|
||||
"utcOffset": 2,
|
||||
"why": "DST starts",
|
||||
"rise": "06:46",
|
||||
"transit": "13:07",
|
||||
"set": "19:29"
|
||||
},
|
||||
{
|
||||
"city": "Prague",
|
||||
"lat": 50.0755,
|
||||
"lon": 14.4378,
|
||||
"timeZone": "Europe/Prague",
|
||||
"day": "2026-06-21",
|
||||
"utcOffset": 2,
|
||||
"why": "midsummer",
|
||||
"rise": "04:53",
|
||||
"transit": "13:04",
|
||||
"set": "21:16"
|
||||
},
|
||||
{
|
||||
"city": "Prague",
|
||||
"lat": 50.0755,
|
||||
"lon": 14.4378,
|
||||
"timeZone": "Europe/Prague",
|
||||
"day": "2026-10-24",
|
||||
"utcOffset": 2,
|
||||
"why": "the day before DST ends",
|
||||
"rise": "07:39",
|
||||
"transit": "12:46",
|
||||
"set": "17:54"
|
||||
},
|
||||
{
|
||||
"city": "Prague",
|
||||
"lat": 50.0755,
|
||||
"lon": 14.4378,
|
||||
"timeZone": "Europe/Prague",
|
||||
"day": "2026-10-25",
|
||||
"utcOffset": 1,
|
||||
"why": "DST ends",
|
||||
"rise": "06:40",
|
||||
"transit": "11:46",
|
||||
"set": "16:52"
|
||||
},
|
||||
{
|
||||
"city": "Prague",
|
||||
"lat": 50.0755,
|
||||
"lon": 14.4378,
|
||||
"timeZone": "Europe/Prague",
|
||||
"day": "2026-12-21",
|
||||
"utcOffset": 1,
|
||||
"why": "midwinter",
|
||||
"rise": "07:58",
|
||||
"transit": "12:00",
|
||||
"set": "16:02"
|
||||
},
|
||||
{
|
||||
"city": "Reykjavík",
|
||||
"lat": 64.1466,
|
||||
"lon": -21.9426,
|
||||
"timeZone": "Atlantic/Reykjavik",
|
||||
"day": "2026-03-20",
|
||||
"utcOffset": 0,
|
||||
"why": "equinox",
|
||||
"rise": "07:29",
|
||||
"transit": "13:35",
|
||||
"set": "19:43"
|
||||
},
|
||||
{
|
||||
"city": "Reykjavík",
|
||||
"lat": 64.1466,
|
||||
"lon": -21.9426,
|
||||
"timeZone": "Atlantic/Reykjavik",
|
||||
"day": "2026-06-21",
|
||||
"utcOffset": 0,
|
||||
"why": "midsummer: a sunset near midnight",
|
||||
"rise": "02:55",
|
||||
"transit": "13:30",
|
||||
"set": "00:04"
|
||||
},
|
||||
{
|
||||
"city": "Reykjavík",
|
||||
"lat": 64.1466,
|
||||
"lon": -21.9426,
|
||||
"timeZone": "Atlantic/Reykjavik",
|
||||
"day": "2026-12-21",
|
||||
"utcOffset": 0,
|
||||
"why": "midwinter: four hours of day",
|
||||
"rise": "11:22",
|
||||
"transit": "13:26",
|
||||
"set": "15:29"
|
||||
},
|
||||
{
|
||||
"city": "Tromsø",
|
||||
"lat": 69.6492,
|
||||
"lon": 18.9553,
|
||||
"timeZone": "Europe/Oslo",
|
||||
"day": "2026-03-29",
|
||||
"utcOffset": 2,
|
||||
"why": "DST starts",
|
||||
"rise": "06:03",
|
||||
"transit": "12:49",
|
||||
"set": "19:37"
|
||||
},
|
||||
{
|
||||
"city": "Tromsø",
|
||||
"lat": 69.6492,
|
||||
"lon": 18.9553,
|
||||
"timeZone": "Europe/Oslo",
|
||||
"day": "2026-06-21",
|
||||
"utcOffset": 2,
|
||||
"why": "polar day",
|
||||
"rise": null,
|
||||
"transit": "12:46",
|
||||
"set": null
|
||||
},
|
||||
{
|
||||
"city": "Tromsø",
|
||||
"lat": 69.6492,
|
||||
"lon": 18.9553,
|
||||
"timeZone": "Europe/Oslo",
|
||||
"day": "2026-10-25",
|
||||
"utcOffset": 1,
|
||||
"why": "DST ends",
|
||||
"rise": "07:38",
|
||||
"transit": "11:28",
|
||||
"set": "15:17"
|
||||
},
|
||||
{
|
||||
"city": "Tromsø",
|
||||
"lat": 69.6492,
|
||||
"lon": 18.9553,
|
||||
"timeZone": "Europe/Oslo",
|
||||
"day": "2026-12-21",
|
||||
"utcOffset": 1,
|
||||
"why": "polar night",
|
||||
"rise": null,
|
||||
"transit": null,
|
||||
"set": null
|
||||
},
|
||||
{
|
||||
"city": "Quito",
|
||||
"lat": -0.1807,
|
||||
"lon": -78.4678,
|
||||
"timeZone": "America/Guayaquil",
|
||||
"day": "2026-03-20",
|
||||
"utcOffset": -5,
|
||||
"why": "equinox on the equator",
|
||||
"rise": "06:18",
|
||||
"transit": "12:21",
|
||||
"set": "18:24"
|
||||
},
|
||||
{
|
||||
"city": "Quito",
|
||||
"lat": -0.1807,
|
||||
"lon": -78.4678,
|
||||
"timeZone": "America/Guayaquil",
|
||||
"day": "2026-06-21",
|
||||
"utcOffset": -5,
|
||||
"why": "June solstice",
|
||||
"rise": "06:12",
|
||||
"transit": "12:16",
|
||||
"set": "18:19"
|
||||
},
|
||||
{
|
||||
"city": "Quito",
|
||||
"lat": -0.1807,
|
||||
"lon": -78.4678,
|
||||
"timeZone": "America/Guayaquil",
|
||||
"day": "2026-12-21",
|
||||
"utcOffset": -5,
|
||||
"why": "December solstice",
|
||||
"rise": "06:08",
|
||||
"transit": "12:12",
|
||||
"set": "18:16"
|
||||
},
|
||||
{
|
||||
"city": "Sydney",
|
||||
"lat": -33.8688,
|
||||
"lon": 151.2093,
|
||||
"timeZone": "Australia/Sydney",
|
||||
"day": "2026-04-04",
|
||||
"utcOffset": 11,
|
||||
"why": "the day before DST ends",
|
||||
"rise": "07:09",
|
||||
"transit": "12:58",
|
||||
"set": "18:47"
|
||||
},
|
||||
{
|
||||
"city": "Sydney",
|
||||
"lat": -33.8688,
|
||||
"lon": 151.2093,
|
||||
"timeZone": "Australia/Sydney",
|
||||
"day": "2026-04-05",
|
||||
"utcOffset": 10,
|
||||
"why": "DST ends",
|
||||
"rise": "06:10",
|
||||
"transit": "11:58",
|
||||
"set": "17:45"
|
||||
},
|
||||
{
|
||||
"city": "Sydney",
|
||||
"lat": -33.8688,
|
||||
"lon": 151.2093,
|
||||
"timeZone": "Australia/Sydney",
|
||||
"day": "2026-06-21",
|
||||
"utcOffset": 10,
|
||||
"why": "midwinter",
|
||||
"rise": "07:00",
|
||||
"transit": "11:57",
|
||||
"set": "16:54"
|
||||
},
|
||||
{
|
||||
"city": "Sydney",
|
||||
"lat": -33.8688,
|
||||
"lon": 151.2093,
|
||||
"timeZone": "Australia/Sydney",
|
||||
"day": "2026-10-03",
|
||||
"utcOffset": 10,
|
||||
"why": "the day before DST starts",
|
||||
"rise": "05:30",
|
||||
"transit": "11:44",
|
||||
"set": "17:59"
|
||||
},
|
||||
{
|
||||
"city": "Sydney",
|
||||
"lat": -33.8688,
|
||||
"lon": 151.2093,
|
||||
"timeZone": "Australia/Sydney",
|
||||
"day": "2026-10-04",
|
||||
"utcOffset": 11,
|
||||
"why": "DST starts",
|
||||
"rise": "06:29",
|
||||
"transit": "12:44",
|
||||
"set": "19:00"
|
||||
},
|
||||
{
|
||||
"city": "Sydney",
|
||||
"lat": -33.8688,
|
||||
"lon": 151.2093,
|
||||
"timeZone": "Australia/Sydney",
|
||||
"day": "2026-12-21",
|
||||
"utcOffset": 11,
|
||||
"why": "midsummer",
|
||||
"rise": "05:41",
|
||||
"transit": "12:53",
|
||||
"set": "20:05"
|
||||
},
|
||||
{
|
||||
"city": "Anchorage",
|
||||
"lat": 61.2181,
|
||||
"lon": -149.9003,
|
||||
"timeZone": "America/Anchorage",
|
||||
"day": "2026-03-07",
|
||||
"utcOffset": -9,
|
||||
"why": "the day before DST starts",
|
||||
"rise": "07:41",
|
||||
"transit": "13:10",
|
||||
"set": "18:41"
|
||||
},
|
||||
{
|
||||
"city": "Anchorage",
|
||||
"lat": 61.2181,
|
||||
"lon": -149.9003,
|
||||
"timeZone": "America/Anchorage",
|
||||
"day": "2026-03-08",
|
||||
"utcOffset": -8,
|
||||
"why": "DST starts",
|
||||
"rise": "08:38",
|
||||
"transit": "14:10",
|
||||
"set": "19:44"
|
||||
},
|
||||
{
|
||||
"city": "Anchorage",
|
||||
"lat": 61.2181,
|
||||
"lon": -149.9003,
|
||||
"timeZone": "America/Anchorage",
|
||||
"day": "2026-06-21",
|
||||
"utcOffset": -8,
|
||||
"why": "midsummer",
|
||||
"rise": "04:20",
|
||||
"transit": "14:02",
|
||||
"set": "23:43"
|
||||
},
|
||||
{
|
||||
"city": "Anchorage",
|
||||
"lat": 61.2181,
|
||||
"lon": -149.9003,
|
||||
"timeZone": "America/Anchorage",
|
||||
"day": "2026-10-31",
|
||||
"utcOffset": -8,
|
||||
"why": "the day before DST ends",
|
||||
"rise": "09:26",
|
||||
"transit": "13:43",
|
||||
"set": "18:00"
|
||||
},
|
||||
{
|
||||
"city": "Anchorage",
|
||||
"lat": 61.2181,
|
||||
"lon": -149.9003,
|
||||
"timeZone": "America/Anchorage",
|
||||
"day": "2026-11-01",
|
||||
"utcOffset": -9,
|
||||
"why": "DST ends",
|
||||
"rise": "08:28",
|
||||
"transit": "12:43",
|
||||
"set": "16:57"
|
||||
},
|
||||
{
|
||||
"city": "Anchorage",
|
||||
"lat": 61.2181,
|
||||
"lon": -149.9003,
|
||||
"timeZone": "America/Anchorage",
|
||||
"day": "2026-12-21",
|
||||
"utcOffset": -9,
|
||||
"why": "midwinter",
|
||||
"rise": "10:14",
|
||||
"transit": "12:58",
|
||||
"set": "15:41"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
import fc from 'fast-check';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { invitation, sunDay, type Place, type SunDay } from '../src/sun/sun.js';
|
||||
import { localDay } from '../src/time/localDay.js';
|
||||
|
||||
/** Recorded by scripts/fetch-sun-golden.ts from the U.S. Naval Observatory. */
|
||||
interface Row {
|
||||
readonly city: string;
|
||||
readonly lat: number;
|
||||
readonly lon: number;
|
||||
readonly timeZone: string;
|
||||
readonly day: string;
|
||||
readonly utcOffset: number;
|
||||
readonly why: string;
|
||||
readonly rise: string | null;
|
||||
readonly transit: string | null;
|
||||
readonly set: string | null;
|
||||
}
|
||||
const golden = JSON.parse(readFileSync(new URL('./fixtures/sun-usno.json', import.meta.url), 'utf8')) as { rows: Row[] };
|
||||
|
||||
/** The almanac rounds to the minute, and NOAA's method is good to about a minute below 72°. */
|
||||
const TOLERANCE_MINUTES = 2;
|
||||
const MINUTE = 60_000;
|
||||
const HOUR = 60 * MINUTE;
|
||||
|
||||
const clocks = new Map<string, Intl.DateTimeFormat>();
|
||||
/** Minutes after midnight on the local clock of `timeZone`: what a person there reads. */
|
||||
function clock(epochMs: number, timeZone: string): number {
|
||||
let f = clocks.get(timeZone);
|
||||
if (!f) {
|
||||
f = new Intl.DateTimeFormat('en-GB', { timeZone, hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' });
|
||||
clocks.set(timeZone, f);
|
||||
}
|
||||
const p = Object.fromEntries(f.formatToParts(epochMs).map((x) => [x.type, x.value]));
|
||||
return Number(p.hour) * 60 + Number(p.minute) + Number(p.second) / 60;
|
||||
}
|
||||
const minutes = (hhmm: string): number => Number(hhmm.slice(0, 2)) * 60 + Number(hhmm.slice(3));
|
||||
/** The shortest way round the clock face, so 23:59 and 00:01 are 2 minutes apart. */
|
||||
const apart = (a: number, b: number): number => Math.min(Math.abs(a - b), 1440 - Math.abs(a - b));
|
||||
|
||||
function offsetHours(epochMs: number, timeZone: string): number {
|
||||
const name = new Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'longOffset' }).formatToParts(epochMs).find((p) => p.type === 'timeZoneName')?.value ?? '';
|
||||
const m = /GMT([+-])(\d\d):(\d\d)/.exec(name);
|
||||
return m ? (m[1] === '-' ? -1 : 1) * (Number(m[2]) + Number(m[3]) / 60) : 0;
|
||||
}
|
||||
|
||||
const placeOf = (r: Row): Place => ({ lat: r.lat, lon: r.lon, timeZone: r.timeZone });
|
||||
const UTC = (lat: number, lon: number): Place => ({ lat, lon, timeZone: 'UTC' });
|
||||
/**
|
||||
* A place whose clock follows its longitude, as real zones roughly do. Etc/GMT
|
||||
* signs are inverted: Etc/GMT-5 is UTC+5.
|
||||
*/
|
||||
const zoned = (lat: number, lon: number): Place => {
|
||||
const offset = Math.round(lon / 15);
|
||||
return { lat, lon, timeZone: offset === 0 ? 'Etc/GMT' : `Etc/GMT${offset > 0 ? '-' : '+'}${Math.abs(offset)}` };
|
||||
};
|
||||
|
||||
describe('sunDay — golden, against the U.S. Naval Observatory tables', () => {
|
||||
it('the fixture covers six cities, both hemispheres, the equator, the Arctic and every DST change it names', () => {
|
||||
expect(new Set(golden.rows.map((r) => r.city))).toEqual(new Set(['Prague', 'Reykjavík', 'Tromsø', 'Quito', 'Sydney', 'Anchorage']));
|
||||
expect(golden.rows.filter((r) => /DST (starts|ends)/.test(r.why) && !r.why.startsWith('the day before'))).toHaveLength(8);
|
||||
});
|
||||
|
||||
const sunny = golden.rows.filter((r) => r.rise !== null && r.set !== null);
|
||||
it.each(sunny.map((r) => [r.city, r.day, r.why, r] as const))('%s %s (%s): rise, noon and set within 2 min, on the local clock', (_c, _d, _w, r) => {
|
||||
const s = sunDay(r.day, placeOf(r));
|
||||
if (s.kind !== 'sun-sets') throw new Error(`expected a sunset, got ${s.kind}`);
|
||||
// The table and the zone database agree about the clock that day.
|
||||
expect(offsetHours(s.solarNoon, r.timeZone)).toBe(r.utcOffset);
|
||||
expect(apart(clock(s.sunrise, r.timeZone), minutes(r.rise ?? ''))).toBeLessThanOrEqual(TOLERANCE_MINUTES);
|
||||
expect(apart(clock(s.solarNoon, r.timeZone), minutes(r.transit ?? ''))).toBeLessThanOrEqual(TOLERANCE_MINUTES);
|
||||
expect(apart(clock(s.sunset, r.timeZone), minutes(r.set ?? ''))).toBeLessThanOrEqual(TOLERANCE_MINUTES);
|
||||
expect(localDay(s.solarNoon, r.timeZone)).toBe(r.day);
|
||||
});
|
||||
|
||||
it('Reykjavík at midsummer: tonight\'s sunset falls after midnight, so it is on the next local day (D2)', () => {
|
||||
const r = golden.rows.find((x) => x.city === 'Reykjavík' && x.day === '2026-06-21');
|
||||
const s = sunDay('2026-06-21', placeOf(r!));
|
||||
if (s.kind !== 'sun-sets') throw new Error(s.kind);
|
||||
// The table lists 00:04 on the 21st: that is the evening of the 20th. Ours is the evening of the 21st.
|
||||
expect(localDay(s.sunset, 'Atlantic/Reykjavik')).toBe('2026-06-22');
|
||||
expect(s.sunset - s.solarNoon).toBeGreaterThan(10 * HOUR);
|
||||
});
|
||||
|
||||
it('Tromsø: polar day on 21 June, polar night on 21 December, as the table says', () => {
|
||||
const tromso = golden.rows.filter((r) => r.city === 'Tromsø');
|
||||
const june = tromso.find((r) => r.day === '2026-06-21')!;
|
||||
const december = tromso.find((r) => r.day === '2026-12-21')!;
|
||||
expect([june.rise, june.set, december.rise, december.set]).toEqual([null, null, null, null]);
|
||||
|
||||
const summer = sunDay(june.day, placeOf(june));
|
||||
expect(summer.kind).toBe('polar-day');
|
||||
expect(apart(clock(summer.solarNoon, june.timeZone), minutes(june.transit ?? ''))).toBeLessThanOrEqual(TOLERANCE_MINUTES);
|
||||
expect(sunDay(december.day, placeOf(december)).kind).toBe('polar-night');
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — the clock changes, the sun does not', () => {
|
||||
it('Prague, 29 March: the sunset on the clock jumps an hour, the instant moves about a day', () => {
|
||||
const prague: Place = { lat: 50.0755, lon: 14.4378, timeZone: 'Europe/Prague' };
|
||||
const before = sunDay('2026-03-28', prague);
|
||||
const after = sunDay('2026-03-29', prague);
|
||||
if (before.kind !== 'sun-sets' || after.kind !== 'sun-sets') throw new Error('expected sunsets');
|
||||
const moved = after.sunset - before.sunset;
|
||||
expect(Math.abs(moved - 24 * HOUR)).toBeLessThan(3 * MINUTE); // the sun: a minute or two
|
||||
const onTheClock = clock(after.sunset, prague.timeZone) - clock(before.sunset, prague.timeZone);
|
||||
expect(onTheClock).toBeGreaterThan(58);
|
||||
expect(onTheClock).toBeLessThan(63); // the clock: an hour
|
||||
});
|
||||
|
||||
it('Sydney, 5 April: the clocks go back, and the sunset on the clock comes an hour earlier', () => {
|
||||
const sydney: Place = { lat: -33.8688, lon: 151.2093, timeZone: 'Australia/Sydney' };
|
||||
const before = sunDay('2026-04-04', sydney);
|
||||
const after = sunDay('2026-04-05', sydney);
|
||||
if (before.kind !== 'sun-sets' || after.kind !== 'sun-sets') throw new Error('expected sunsets');
|
||||
const onTheClock = clock(after.sunset, sydney.timeZone) - clock(before.sunset, sydney.timeZone);
|
||||
expect(onTheClock).toBeLessThan(-58);
|
||||
expect(onTheClock).toBeGreaterThan(-63);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — properties', () => {
|
||||
const days2026 = fc.integer({ min: 0, max: 364 }).map((n) => localDay(Date.UTC(2026, 0, 1) + n * 24 * HOUR, 'UTC'));
|
||||
const midLatitudes = fc.double({ min: -60, max: 60, noNaN: true });
|
||||
const longitudes = fc.double({ min: -180, max: 180, noNaN: true });
|
||||
|
||||
it('within ±60°, at any longitude with its own clock: sunrise before noon before sunset, noon midway, noon on the asked day', () => {
|
||||
fc.assert(fc.property(midLatitudes, longitudes, days2026, (lat, lon, day) => {
|
||||
const place = zoned(lat, lon);
|
||||
const s = sunDay(day, place);
|
||||
if (s.kind !== 'sun-sets') return false;
|
||||
const morning = s.solarNoon - s.sunrise;
|
||||
const afternoon = s.sunset - s.solarNoon;
|
||||
return s.sunrise < s.solarNoon && s.solarNoon < s.sunset
|
||||
&& Math.abs(morning - afternoon) < 2 * MINUTE
|
||||
&& s.sunset - s.sunrise < 24 * HOUR
|
||||
&& localDay(s.solarNoon, place.timeZone) === day;
|
||||
}), { numRuns: 500 });
|
||||
});
|
||||
|
||||
it('within ±60°, sunsets on consecutive days are never more than 5 minutes apart on the sun\'s own clock', () => {
|
||||
fc.assert(fc.property(midLatitudes, longitudes, fc.integer({ min: 0, max: 363 }), (lat, lon, n) => {
|
||||
const day = (k: number): string => localDay(Date.UTC(2026, 0, 1) + k * 24 * HOUR, 'UTC');
|
||||
const a = sunDay(day(n), zoned(lat, lon));
|
||||
const b = sunDay(day(n + 1), zoned(lat, lon));
|
||||
if (a.kind !== 'sun-sets' || b.kind !== 'sun-sets') return false;
|
||||
return Math.abs(b.sunset - a.sunset - 24 * HOUR) < 5 * MINUTE;
|
||||
}), { numRuns: 300 });
|
||||
});
|
||||
|
||||
it('the invitation is always a defined instant near the asked day, for every latitude', () => {
|
||||
fc.assert(fc.property(fc.double({ min: -90, max: 90, noNaN: true }), longitudes, days2026, (lat, lon, day) => {
|
||||
const s = sunDay(day, UTC(lat, lon));
|
||||
const at = invitation(s);
|
||||
return Number.isFinite(at) && at >= s.solarNoon - 12 * HOUR && at <= s.solarNoon + 24 * HOUR;
|
||||
}), { numRuns: 500 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — the far north and south (D4)', () => {
|
||||
it.each([
|
||||
[80, '2026-06-21', 'polar-day'], [80, '2026-12-21', 'polar-night'],
|
||||
[-80, '2026-06-21', 'polar-night'], [-80, '2026-12-21', 'polar-day'],
|
||||
[90, '2026-06-21', 'polar-day'], [-90, '2026-06-21', 'polar-night']
|
||||
] as const)('latitude %s on %s is %s', (lat, day, kind) => {
|
||||
expect(sunDay(day, UTC(lat, 0)).kind).toBe(kind);
|
||||
});
|
||||
|
||||
it('polar day invites at the lowest light, about twelve hours after noon; polar night at noon, when the twilight is brightest', () => {
|
||||
const summer = sunDay('2026-06-21', UTC(80, 15));
|
||||
if (summer.kind !== 'polar-day') throw new Error(summer.kind);
|
||||
expect(invitation(summer)).toBe(summer.lowestLight);
|
||||
expect(Math.abs(summer.lowestLight - summer.solarNoon - 12 * HOUR)).toBeLessThan(MINUTE);
|
||||
const winter = sunDay('2026-12-21', UTC(80, 15));
|
||||
expect(invitation(winter)).toBe(winter.solarNoon);
|
||||
});
|
||||
|
||||
it('where the sun sets, the invitation is the sunset', () => {
|
||||
const s: SunDay = sunDay('2026-06-21', { lat: 50.0755, lon: 14.4378, timeZone: 'Europe/Prague' });
|
||||
if (s.kind !== 'sun-sets') throw new Error(s.kind);
|
||||
expect(invitation(s)).toBe(s.sunset);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — the edges of the clock face', () => {
|
||||
it('Kiritimati, UTC+14 at 157° W: noon falls on the next UTC date, and is still on the asked local day', () => {
|
||||
const kiritimati: Place = { lat: 1.87, lon: -157.4, timeZone: 'Pacific/Kiritimati' };
|
||||
for (const day of ['2026-01-01', '2026-06-21', '2026-12-31']) {
|
||||
const s = sunDay(day, kiritimati);
|
||||
expect(localDay(s.solarNoon, kiritimati.timeZone)).toBe(day);
|
||||
expect(localDay(s.solarNoon, 'UTC') < day).toBe(true); // UTC is still on the day before
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — a clock twelve hours off the sun (D8)', () => {
|
||||
it('at 179.9° W on UTC, 26 December has no solar noon of its own; the engine gives the nearest one, never throws', () => {
|
||||
// Found by fast-check. Solar noon sits at midnight there and crosses it in late
|
||||
// December, so one calendar day holds none. No real zone is this far off the sun.
|
||||
const place = UTC(0, -179.92);
|
||||
const noons = ['2026-12-25', '2026-12-26', '2026-12-27'].map((d) => sunDay(d, place).solarNoon);
|
||||
expect(noons.map((n) => localDay(n, 'UTC'))).toEqual(['2026-12-25', '2026-12-27', '2026-12-27']);
|
||||
for (const n of noons) expect(Math.abs(n - Date.UTC(2026, 11, 26, 24))).toBeLessThan(25 * HOUR);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — refusals', () => {
|
||||
it.each([
|
||||
['2026-02-30', UTC(50, 14), /does not exist/],
|
||||
['2026-6-1', UTC(50, 14), /not a LocalDay/],
|
||||
['2026-06-21', UTC(91, 14), /latitude/],
|
||||
['2026-06-21', UTC(50, 181), /longitude/],
|
||||
['2026-06-21', UTC(Number.NaN, 14), /latitude/],
|
||||
['2026-06-21', { lat: 50, lon: 14, timeZone: 'Europe/Pargue' }, /time zone/i]
|
||||
] as const)('%s at %o', (day, place, message) => {
|
||||
expect(() => sunDay(day, place)).toThrow(message);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user