use std::time::Duration;
pub mod consts;
mod gnss;
mod mjd;
mod utc;
pub use gnss::*;
pub use mjd::*;
pub use utc::*;
pub const MINUTE: Duration = Duration::from_secs(consts::MINUTE_SECS as u64);
pub const HOUR: Duration = Duration::from_secs(consts::HOUR_SECS as u64);
pub const DAY: Duration = Duration::from_secs(consts::DAY_SECS as u64);
pub const WEEK: Duration = Duration::from_secs(consts::WEEK_SECS as u64);
#[must_use]
pub fn is_leap_year(year: u16) -> bool {
(year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_leap_year() {
use super::is_leap_year;
assert!(!is_leap_year(2019));
assert!(is_leap_year(2020));
assert!(!is_leap_year(1900));
assert!(is_leap_year(2000));
}
#[test]
fn conversions() {
const TEST_CASES: [GpsTime; 10] = [
GpsTime::new_unchecked(1234, 567_890.0),
GpsTime::new_unchecked(1234, 567_890.5),
GpsTime::new_unchecked(1234, 567_890.0),
GpsTime::new_unchecked(1234, 0.0),
GpsTime::new_unchecked(1000, 604_578.0),
GpsTime::new_unchecked(1001, 222.222),
GpsTime::new_unchecked(1001, 604_578.0),
GpsTime::new_unchecked(1939, 222.222),
GpsTime::new_unchecked(1930, 16.0),
GpsTime::new_unchecked(1930, 18.0),
];
const TOW_TOL: f64 = 1e-6;
for test_case in TEST_CASES {
let mjd = test_case.to_mjd_hardcoded();
let round_trip = mjd.to_gps_hardcoded();
let diff = test_case.diff(&round_trip).abs();
assert!(
diff < TOW_TOL,
"gps2mjd2gps failure. original: {test_case:?}, round trip: {round_trip:?}, diff: \
{diff}, TOW_TOL: {TOW_TOL}"
);
let (year, month, day, hour, minute, second) = mjd.to_date();
let round_trip = MJD::from_parts(year, month, day, hour, minute, second);
let diff = (mjd.as_f64() - round_trip.as_f64()).abs();
assert!(
diff < TOW_TOL,
"mjd2date2mjd failure. original: {mjd:?}, round trip: {round_trip:?}, diff: \
{diff}, TOW_TOL: {TOW_TOL}"
);
let utc = mjd.to_utc();
let round_trip = utc.to_mjd();
let diff = (mjd.as_f64() - round_trip.as_f64()).abs();
assert!(
diff < TOW_TOL,
"mjd2utc2mjd failure. original: {mjd:?}, round trip: {round_trip:?}, diff: \
{diff}, TOW_TOL: {TOW_TOL}"
);
let (year, month, day, hour, minute, second) = test_case.to_date_hardcoded();
let round_trip = GpsTime::from_parts_hardcoded(year, month, day, hour, minute, second);
let diff = test_case.diff(&round_trip).abs();
assert!(
diff < TOW_TOL,
"gps2date2gps failure. original: {test_case:?}, round trip: {round_trip:?}, diff: \
{diff}, TOW_TOL: {TOW_TOL}"
);
let (year, month, day, hour, minute, second) = utc.to_date();
let round_trip = UtcTime::from_parts(year, month, day, hour, minute, second);
let diff = utc.to_mjd().as_f64() - mjd.as_f64();
assert!(
diff < TOW_TOL,
"utc2date2utc failure. original: {mjd:?}, round trip: {round_trip:?}, diff: \
{diff}, TOW_TOL: {TOW_TOL}"
);
}
}
}