[PATCH thp] sun: replace function local_today_utc_noon with a function that takes a parameter. It makes testing possible using dates other than today's.
Export this patch
Remove call to math::ceilf64 from sunrise_sunset.
Remove call to math::truncf64 from julian_to_gregorian.
---
mods/sun+sun.ha | 18 +++++++++ ---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/mods/sun+sun.ha b/mods/sun+sun.ha
index ef9626e..82b2a00 100644
--- a/mods/sun+sun.ha
+++ b/mods/sun+sun.ha
@@ -1,7 +1,7 @@
// Author: Carlos Une <une@fastmail.fm>
// Maintainer: Carlos Une <une@fastmail.fm>
//
- // SPDX-FileCopyrightText: 2023 Carlos Une <une@fastmail.fm>
+ // SPDX-FileCopyrightText: 2023, 2024 Carlos Une <une@fastmail.fm>
// SPDX-License-Identifier: GPL-3.0-or-later
// Reference: https://en.wikipedia.org/wiki/Sunrise_equation
use config;
@@ -31,7 +31,7 @@ fn readconfig() (f64, f64, str, str, str, str) = (
export fn mod_sunrise(pe: *env::env) str = {
const cfg = readconfig();
- match(sunrise_sunset(local_today_utc_noon(), cfg.0, cfg.1)) {
+ match(sunrise_sunset(local_day_utc_noon(date::nowutc()), cfg.0, cfg.1)) {
case let r: (date::date, date::date) =>
return strings::dup(date::asformat(cfg.2, &date::in(chrono::LOCAL, r.0)!)!);
case midnightsun =>
@@ -43,7 +43,7 @@ export fn mod_sunrise(pe: *env::env) str = {
export fn mod_sunset(pe: *env::env) str = {
const cfg = readconfig();
- match(sunrise_sunset(local_today_utc_noon(), cfg.0, cfg.1)) {
+ match(sunrise_sunset(local_day_utc_noon(date::nowutc()), cfg.0, cfg.1)) {
case let r: (date::date, date::date) =>
return strings::dup(date::asformat(cfg.3, &date::in(chrono::LOCAL, r.1)!)!);
case midnightsun =>
@@ -58,10 +58,10 @@ fn deg(a: f64) f64 = a * 180.0/math::PI;
fn sin(a: f64) f64 = math::sinf64(rad(a));
fn cos(a: f64) f64 = math::cosf64(rad(a));
- // Get today's yyyy/mm/dd in LOCAL tzone, build yyyy/mm/dd 12:00:00 in UTC tzone
- fn local_today_utc_noon() date::date = {
- const now = date::now();
- return date::new(chrono::UTC, 0, date::year(&now), date::month(&now), date::day(&now), 12, 0, 0, 0)!;
+ // Extract yyyy/mm/dd in LOCAL timezone, build yyyy/mm/dd 12:00:00 in UTC timezone
+ fn local_day_utc_noon(utc: date::date) date::date = {
+ const d = date::in(chrono::LOCAL, utc)!;
+ return date::new(chrono::UTC, 0, date::year(&d), date::month(&d), date::day(&d), 12, 0, 0, 0)!;
};
fn julian_day_n(dt: date::date) f64 = {
@@ -71,7 +71,7 @@ fn julian_day_n(dt: date::date) f64 = {
// https://en.wikipedia.org/wiki/Julian_day#Julian_or_Gregorian_calendar_from_Julian_day_number
fn julian_to_gregorian(jday: f64) date::date = {
- const J: int = math::truncf64(jday): int;
+ const J: int = jday: int;
const y: int = 4716;
const v: int = 3;
const j: int = 1401;
@@ -130,7 +130,7 @@ fn hour_angle(phi: f64, delta: f64) (f64, f64) = {
};
fn sunrise_sunset(d: date::date, lat: f64, lon: f64) ((date::date, date::date) | midnightsun | polarnight) = {
- const J = mean_solar_time(math::ceilf64(julian_day_n(d)), lon);
+ const J = mean_solar_time(julian_day_n(d), lon);
const M = solar_mean_anomaly(J);
const C = equation_of_the_center(M);
const lambda = eclyptic_longitude(M, C);
--
2.39.2
thanks! I’ll check my coordinates and if I can reproduce the offset error