use crate::core::constants::{DAYS_PER_CENTURY, J2000, PI};
use crate::core::eph0::sun_data::{E_L0, E_L1, E_L2, E_L3, E_L4, E_L5};
pub fn equation_of_time_seconds(jd: f64) -> f64 {
let t = (jd - J2000) / DAYS_PER_CENTURY;
let tk = t / 10.0;
let l0 = (280.4664567 + 36000.76982779 * t + 0.0003032028 * t * t) * PI / 180.0;
fn vsop87_geo_lon(tk: f64) -> f64 {
let mut v = 0.0;
let mut tn = 1.0;
for layer in [
&E_L0[..],
&E_L1[..],
&E_L2[..],
&E_L3[..],
&E_L4[..],
&E_L5[..],
] {
let mut sum = 0.0;
for item in layer.iter() {
sum += item[0] * (item[1] + tk * item[2]).cos();
}
v += sum * tn;
tn *= tk;
}
let t2 = tk * tk;
let t3 = t2 * tk;
v += (-0.0728 - 2.7702 * tk - 1.1019 * t2 - 0.0996 * t3) / 206264.80624709636;
(v + PI).rem_euclid(2.0 * PI)
}
let true_lon = vsop87_geo_lon(tk);
let eps = (23.4392911 - 0.013004167 * t) * PI / 180.0;
let ra = (true_lon.sin() * eps.cos()).atan2(true_lon.cos());
let mut diff = (l0 - ra).rem_euclid(2.0 * PI);
if diff > PI {
diff -= 2.0 * PI;
}
diff * 86400.0 / (2.0 * PI)
}
pub fn local_mean_time(jd: f64, longitude: f64) -> (i32, u32, u32) {
let standard_meridian = (longitude / 15.0).round() * 15.0;
let correction_seconds = (longitude - standard_meridian) / 15.0 * 3600.0;
let jd_adjusted = jd + correction_seconds / 86400.0;
let fraction = (jd_adjusted + 0.5).rem_euclid(1.0);
let total_seconds = (fraction * 86400.0).round() as i64;
let norm = total_seconds.rem_euclid(86400);
let hour = norm / 3600;
let minute = (norm % 3600) / 60;
let second = norm % 60;
(hour as i32, minute as u32, second as u32)
}
pub fn apparent_solar_time(jd: f64, longitude: f64) -> (i32, u32, u32) {
let (h, m, s) = local_mean_time(jd, longitude);
let lmt_seconds = h as f64 * 3600.0 + m as f64 * 60.0 + s as f64;
let eot_seconds = equation_of_time_seconds(jd);
let total_seconds = (lmt_seconds + eot_seconds).round() as i64;
let norm = total_seconds.rem_euclid(86400);
let hour = norm / 3600;
let minute = (norm % 3600) / 60;
let second = norm % 60;
(hour as i32, minute as u32, second as u32)
}
pub fn convert_to_apparent_solar_time(
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
longitude: f64,
) -> (i32, u32, u32, u32, u32) {
let jd = crate::time::jd::JulianDay::from_ymd(year, month, day).0;
let standard_meridian = (longitude / 15.0).round() * 15.0;
let lmt_offset_seconds = (longitude - standard_meridian) / 15.0 * 3600.0;
let jd_std = jd + hour as f64 / 24.0 + minute as f64 / 1440.0;
let jd_lmt = jd_std + lmt_offset_seconds / 86400.0;
let eot_seconds = equation_of_time_seconds(jd_lmt);
let jd_apparent = jd_lmt + eot_seconds / 86400.0;
let (ay, am, ad) = crate::time::jd::JulianDay(jd_apparent).to_ymd();
let day_frac = jd_apparent - crate::time::jd::JulianDay::from_ymd(ay, am, ad).0;
let total_minutes = (day_frac * 1440.0).round() as u32;
(ay, am, ad, total_minutes / 60, total_minutes % 60)
}
pub fn calc_utc_offset(longitude: f64) -> f64 {
(longitude / 15.0).round()
}
pub fn calc_time_correction(longitude: f64, tz_offset: f64) -> i64 {
let standard_meridian = tz_offset * 15.0;
((longitude - standard_meridian) / 15.0 * 3600.0) as i64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_eot_magnitude() {
let jds = [2451545.0, 2460310.5, 2460482.5, 2460576.5];
for &jd in &jds {
let eot = equation_of_time_seconds(jd);
assert!(eot.abs() < 1200.0, "EOT={} 超出范围", eot);
}
}
#[test]
fn test_eot_approximately_minus_3_minutes() {
let eot = equation_of_time_seconds(2451545.0);
assert!((eot + 189.0).abs() < 60.0, "eot={eot}");
}
#[test]
fn test_eot_range_plausible() {
let data: &[(f64, &str)] = &[
(2451545.0, "2000-01"),
(2460493.5, "2024-07"),
(2460978.5, "2025-11"),
];
for &(jd, label) in data {
let eot = equation_of_time_seconds(jd);
assert!(eot.abs() < 1200.0, "{label}: EOT={eot:.1}s 超出范围");
}
}
#[test]
fn test_lmt_uses_midnight_based_clock() {
assert_eq!(local_mean_time(2451544.5, 120.0), (0, 0, 0));
assert_eq!(local_mean_time(2451545.0, 120.0), (12, 0, 0));
}
#[test]
fn test_lmt_shanghai_six_minutes_ahead() {
let jd = 2460310.5;
let (h, m, _) = local_mean_time(jd, 121.5);
assert_eq!((h, m), (0, 6));
}
#[test]
fn test_lmt_normalizes_across_midnight() {
assert_eq!(local_mean_time(2451544.5, 127.5), (23, 30, 0));
assert_eq!(local_mean_time(2451544.5, 100.0), (23, 40, 0));
assert_eq!(local_mean_time(2451544.5 - 1.0 / 48.0, 120.0), (23, 30, 0));
}
#[test]
fn test_ast_uses_civil_clock() {
let (hour, minute, _) = apparent_solar_time(2451544.5, 120.0);
assert_eq!((hour, minute), (23, 56));
}
#[test]
fn test_ast_differs_from_lmt_by_eot() {
let jd = 2460310.5;
let (lm_h, lm_m, _) = local_mean_time(jd, 120.0);
let (as_h, as_m, _) = apparent_solar_time(jd, 120.0);
let lmt = lm_h * 3600 + lm_m as i32 * 60;
let ast = as_h * 3600 + as_m as i32 * 60;
let diff = (ast - lmt + 43_200).rem_euclid(86_400) - 43_200;
assert!(
diff.abs() <= 1200,
"LMT=({lm_h},{lm_m}) AST=({as_h},{as_m}) diff={diff}s"
);
}
#[test]
fn test_calc_utc_offset_east() {
assert_eq!(calc_utc_offset(120.0), 8.0);
}
#[test]
fn test_calc_utc_offset_west() {
assert_eq!(calc_utc_offset(-75.0), -5.0);
}
#[test]
fn test_calc_utc_offset_prime_meridian() {
assert_eq!(calc_utc_offset(0.0), 0.0);
}
}