From e32f2b7cfde097f3b1091acd67988dde132bc41d Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 9 Nov 2026 05:01:45 +0000 Subject: [PATCH] chore(deps): update dependency suncalc to v2.1.3 --- package-lock.json | 8 ++-- package.json | 2 +- public/vendor/suncalc/suncalc.js | 75 +++++++++++++++++++++++--------- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 20dc374..f63a3de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "maplibre-gl": "6.16.0", "maplibre-gl-lanes": "1.4.0", "pmtiles": "4.5.0", - "suncalc": "2.1.1" + "suncalc": "2.1.3" } }, "node_modules/@mapbox/jsonlint-lines-primitives": { @@ -304,9 +304,9 @@ } }, "node_modules/suncalc": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/suncalc/-/suncalc-2.1.1.tgz", - "integrity": "sha512-ufMOPzYRk5aomEs3g+qeJGdCPZWC3TL94E0HQagoSusFnNXCvfuewWBroY3lxRHqA7IA8UGvgg7dcrSSDJsuEg==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/suncalc/-/suncalc-2.1.3.tgz", + "integrity": "sha512-7DWCsxPKRPMtylyGqSrgNBr+pseyczJGqb4NCpW2EziwNHaSuHc9w1lL4i91IyDw5CnPUWqBNBWyd/vFjr4rTw==", "dev": true }, "node_modules/tinyqueue": { diff --git a/package.json b/package.json index 24c0a03..96229f4 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,6 @@ "maplibre-gl": "6.16.0", "maplibre-gl-lanes": "1.4.0", "pmtiles": "4.5.0", - "suncalc": "2.1.1" + "suncalc": "2.1.3" } } diff --git a/public/vendor/suncalc/suncalc.js b/public/vendor/suncalc/suncalc.js index 6eb2907..11ca8b8 100644 --- a/public/vendor/suncalc/suncalc.js +++ b/public/vendor/suncalc/suncalc.js @@ -139,22 +139,36 @@ function solarTransit(dt, lw) { return dt; } -// time the Sun reaches altitude h0 on the given side of transit (sign -1 = rise, +1 = set); -// starts from the hour angle at transit and converges with Meeus' altitude correction (15.2). -function getSetJ(h0, dt, sign, lw, phi, decT) { - const cosH0 = (sin(h0) - sin(phi) * sin(decT)) / (cos(phi) * cos(decT)); - if (cosH0 < -1 || cosH0 > 1) return NaN; // sun stays above / below this altitude all day +// time the Sun reaches altitude h0 within the half day [a, b] between transit and nadir, given the +// sine of its altitude at both ends; NaN if it doesn't cross h0 there (altitude is monotonic over the +// half day, so a sign change means exactly one crossing). Seeds from the hour angle at transit +// declination, then Newton on sin(h) kept inside a shrinking bracket, bisecting whenever a step +// leaves it — on near-grazing days the slope is tiny and plain Newton steps land days away. +function getSetJ(h0, a, b, sa, sb, dt, lw, phi, decT) { + const s0 = sin(h0); + const rising = sa < sb; + if ((sa - s0) * (sb - s0) > 0) return NaN; // sun stays above / below this altitude all half day - let d = dt + sign * acos(cosH0) / (2 * PI); - for (let i = 0; i < 2; i++) { + const cosH0 = (s0 - sin(phi) * sin(decT)) / (cos(phi) * cos(decT)); + const H0 = cosH0 <= -1 ? PI : cosH0 >= 1 ? 0 : acos(cosH0); + let d = dt + (rising ? -H0 : H0) / (2 * PI); + + for (;;) { const c = sunCoords(toDaysTT(d)); - const H = wrapPi(siderealTime(d, lw) - c.ra); - const h = altitude(H, phi, c.dec); - const sinH = cos(phi) * cos(c.dec) * sin(H); - if (abs(sinH) < 1e-6) break; // grazing the horizon — correction is ill-conditioned - d += (h - h0) / (2 * PI * sinH); + const H = siderealTime(d, lw) - c.ra; + const k = cos(phi) * cos(c.dec); + const f = sin(phi) * sin(c.dec) + k * cos(H) - s0; + if ((f < 0) === rising) a = d; else b = d; + + const step = f / (2 * PI * k * sin(H)); + if (d + step > a && d + step < b) { + d += step; + if (abs(step) < 1e-5) return d; // ~1 s step, so quadratic convergence leaves ~µs error + } else { + d = (a + b) / 2; + if (b - a < 1e-8) return d; + } } - return d; } // start of the civil day containing date in a zone utcOffset minutes ahead of UTC, as a ms timestamp @@ -182,24 +196,45 @@ export function getTimes(date, lat, lng, height = 0, utcOffset) { const dt = solarDayTransit(date, lw, utcOffset); const dec = sunCoords(toDaysTT(dt)).dec; // declination at transit, shared by every rise/set solve + // sine of the Sun's altitude at transit and at the nadirs either side (H = 0 and π), bracketing + // every rise and set; the nadirs use their own declination, which matters on regime-boundary days + const sNoon = cos(phi - dec); + const sNadir1 = -cos(phi + sunCoords(toDaysTT(dt - 0.5)).dec); + const sNadir2 = -cos(phi + sunCoords(toDaysTT(dt + 0.5)).dec); + + // the window is the solar day [dt - 0.5, dt + 0.5), or with an offset the civil day, which is just as + // long but shifted by up to half a day: it then also takes in the evening of the previous solar day + // or the morning of the next one, where events cut off from this solar day are looked up instead + const start = utcOffset === undefined ? dt - 0.5 : toDays(civilMidnight(date, utcOffset)); + const end = start + 1; + const prev = start < dt - 0.5; + const next = end > dt + 0.5; + const dt2 = prev ? solarTransit(dt - 1, lw) : next ? solarTransit(dt + 1, lw) : dt; + const dec2 = dt2 === dt ? dec : sunCoords(toDaysTT(dt2)).dec; + const sNoon2 = cos(phi - dec2); + + // solar days run up to ~30 s longer than 24 h, so a civil day can miss a nadir by seconds; keep + // this solar day's one then const result = { solarNoon: fromJulian(dt + J2000), - nadir: fromJulian(dt + J2000 - 0.5) + nadir: fromJulian((next && dt2 - 0.5 < end ? dt2 : dt) - 0.5 + J2000) }; for (const [angle, riseName, setName] of times) { const h0 = (angle + dh) * rad; - const jrise = getSetJ(h0, dt, -1, lw, phi, dec); - const jset = getSetJ(h0, dt, 1, lw, phi, dec); + let jrise = getSetJ(h0, dt - 0.5, dt, sNadir1, sNoon, dt, lw, phi, dec); + let jset = getSetJ(h0, dt, dt + 0.5, sNoon, sNadir2, dt, lw, phi, dec); + if (next && !(jrise >= start)) jrise = getSetJ(h0, dt + 0.5, dt2, sNadir2, sNoon2, dt2, lw, phi, dec2); + if (prev && !(jset < end)) jset = getSetJ(h0, dt2, dt - 0.5, sNoon2, sNadir1, dt2, lw, phi, dec2); - // a NaN means the Sun never reaches this altitude on this day — report null, not Invalid Date - result[riseName] = Number.isNaN(jrise) ? null : fromJulian(jrise + J2000); - result[setName] = Number.isNaN(jset) ? null : fromJulian(jset + J2000); + // NaN (the Sun never reaches this altitude) or outside the window: report null, not Invalid Date + result[riseName] = jrise >= start && jrise < end ? fromJulian(jrise + J2000) : null; + result[setName] = jset >= start && jset < end ? fromJulian(jset + J2000) : null; } // polar day/night: when the Sun never crosses the standard rise/set altitude, flag which side it // stays on by comparing its altitude at solar noon (its daily maximum) against that threshold. - if (result.sunrise === null) { + if (result.sunrise === null && result.sunset === null) { const noonAlt = altitude(0, phi, dec); const riseSetAlt = (times[0][0] + dh) * rad; result.alwaysUp = noonAlt > riseSetAlt; -- 2.54.1