[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:
2026-11-01 23:18:25 +01:00
parent 2962b13696
commit 793bcd7701
5 changed files with 889 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
/**
* Records the sun's published times for the golden tests (CLAUDE.md §5.5) into
* packages/engine/test/fixtures/sun-usno.json. Run by hand, never by a test: the
* tests read the recorded file and make no network call.
*
* tsx scripts/fetch-sun-golden.ts
*
* Source: the U.S. Naval Observatory's Astronomical Applications API (one day of
* rise, upper transit and set for a point). Each row asks for the clock offset the
* place keeps on that date after any DST change, so the table is the local clock a
* person there reads. The cities and coordinates are public places, never a player.
*/
import { writeFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
interface City {
readonly name: string;
readonly lat: number;
readonly lon: number;
readonly timeZone: string;
/** [local day, the UTC offset in hours the clocks show that day after any change, why this day]. */
readonly days: readonly (readonly [string, number, string])[];
}
const CITIES: readonly City[] = [
{ name: 'Prague', lat: 50.0755, lon: 14.4378, timeZone: 'Europe/Prague', days: [
['2026-03-28', 1, 'the day before DST starts'], ['2026-03-29', 2, 'DST starts'],
['2026-06-21', 2, 'midsummer'], ['2026-10-24', 2, 'the day before DST ends'],
['2026-10-25', 1, 'DST ends'], ['2026-12-21', 1, 'midwinter']] },
{ name: 'Reykjavík', lat: 64.1466, lon: -21.9426, timeZone: 'Atlantic/Reykjavik', days: [
['2026-03-20', 0, 'equinox'], ['2026-06-21', 0, 'midsummer: a sunset near midnight'], ['2026-12-21', 0, 'midwinter: four hours of day']] },
{ name: 'Tromsø', lat: 69.6492, lon: 18.9553, timeZone: 'Europe/Oslo', days: [
['2026-03-29', 2, 'DST starts'], ['2026-06-21', 2, 'polar day'],
['2026-10-25', 1, 'DST ends'], ['2026-12-21', 1, 'polar night']] },
{ name: 'Quito', lat: -0.1807, lon: -78.4678, timeZone: 'America/Guayaquil', days: [
['2026-03-20', -5, 'equinox on the equator'], ['2026-06-21', -5, 'June solstice'], ['2026-12-21', -5, 'December solstice']] },
{ name: 'Sydney', lat: -33.8688, lon: 151.2093, timeZone: 'Australia/Sydney', days: [
['2026-04-04', 11, 'the day before DST ends'], ['2026-04-05', 10, 'DST ends'],
['2026-06-21', 10, 'midwinter'], ['2026-10-03', 10, 'the day before DST starts'],
['2026-10-04', 11, 'DST starts'], ['2026-12-21', 11, 'midsummer']] },
{ name: 'Anchorage', lat: 61.2181, lon: -149.9003, timeZone: 'America/Anchorage', days: [
['2026-03-07', -9, 'the day before DST starts'], ['2026-03-08', -8, 'DST starts'],
['2026-06-21', -8, 'midsummer'], ['2026-10-31', -8, 'the day before DST ends'],
['2026-11-01', -9, 'DST ends'], ['2026-12-21', -9, 'midwinter']] }
];
const API = 'https://aa.usno.navy.mil/api/rstt/oneday';
interface Phenomenon { readonly phen: string; readonly time: string }
async function oneDay(c: City, day: string, tz: number): Promise<Record<string, string | null>> {
const url = `${API}?date=${day}&coords=${c.lat},${c.lon}&tz=${tz}&dst=false`;
const res = await fetch(url);
if (!res.ok) throw new Error(`${url}: HTTP ${res.status}`);
const body = (await res.json()) as { properties: { data: { sundata: Phenomenon[] } } };
const at = (phen: string): string | null => body.properties.data.sundata.find((p) => p.phen === phen)?.time ?? null;
return { rise: at('Rise'), transit: at('Upper Transit'), set: at('Set') };
}
const rows = [];
for (const c of CITIES) {
for (const [day, tz, why] of c.days) {
rows.push({ city: c.name, lat: c.lat, lon: c.lon, timeZone: c.timeZone, day, utcOffset: tz, why, ...(await oneDay(c, day, tz)) });
}
}
const out = fileURLToPath(new URL('../packages/engine/test/fixtures/sun-usno.json', import.meta.url));
writeFileSync(out, JSON.stringify({
source: `${API} (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: new Date().toISOString().slice(0, 10),
rows
}, null, 2) + '\n');
console.log(`fetch-sun-golden: ${rows.length} days -> ${out}`);