use chrono::{NaiveDate, NaiveTime};
const SECONDS_IN_A_DAY: f64 = 86400.;
const UNIX_EPOCH_JULIAN_DAY: f64 = 2440587.5;
const NOON_TIME: NaiveTime = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
pub(crate) fn unix_to_julian(timestamp: i64) -> f64 {
timestamp as f64 / SECONDS_IN_A_DAY + UNIX_EPOCH_JULIAN_DAY
}
pub(crate) fn julian_to_unix(day: f64) -> i64 {
((day - UNIX_EPOCH_JULIAN_DAY) * SECONDS_IN_A_DAY) as i64
}
pub(crate) fn mean_solar_noon(lon: f64, date: NaiveDate) -> f64 {
unix_to_julian(date.and_time(NOON_TIME).and_utc().timestamp()) - lon / 360.
}
#[cfg(test)]
mod tests {
use crate::julian::UNIX_EPOCH_JULIAN_DAY;
use chrono::NaiveDate;
#[test]
fn test_unix_to_julian() {
assert_eq!(super::unix_to_julian(0), UNIX_EPOCH_JULIAN_DAY)
}
#[test]
fn test_julian_to_unix() {
assert_eq!(super::julian_to_unix(UNIX_EPOCH_JULIAN_DAY), 0)
}
#[test]
fn test_solar_noon() {
assert_eq!(
super::mean_solar_noon(0., NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()),
2440588.
);
}
}