Skip to main content

satellite_datetime/
mars.rs

1//! Mars Sol Date and Coordinated Mars Time (Allison & McEwen / NASA GISS Mars24).
2
3use crate::instant::Instant;
4
5/// Julian Date TT of the MSD epoch (1873-12-29 12:00-class definition).
6pub const MSD_EPOCH_JD_TT: f64 = 2_405_522.002_877_9;
7/// Mean Mars sol / Earth day (terrestrial days per sol).
8pub const EARTH_DAYS_PER_SOL: f64 = 1.027_491_251_7;
9
10/// Mars Sol Date (running sol count) and mean solar time at the prime meridian.
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct MarsTime {
13    /// Sequential sol number (MSD).
14    pub msd: f64,
15    /// Mean solar time at 0° longitude, hours in `[0, 24)`.
16    pub mtc_hours: f64,
17}
18
19impl Instant {
20    /// MSD and MTC from this instant's TT Julian Date.
21    ///
22    /// MTC is a UT1 analog (mean solar time), **not** a leap-second UTC analog.
23    /// Accuracy is limited by `f64` JD (~50 µs) plus the published sol-length model.
24    pub fn mars_time(self) -> MarsTime {
25        let jd_tt = self.julian_tt().as_f64();
26        let msd = (jd_tt - MSD_EPOCH_JD_TT) / EARTH_DAYS_PER_SOL;
27        let frac = msd - libm::floor(msd);
28        let frac = if frac < 0.0 { frac + 1.0 } else { frac };
29        MarsTime {
30            msd,
31            mtc_hours: frac * 24.0,
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::constants::NS_PER_DAY;
40    use crate::duration::Duration;
41    use crate::julian::unix_days_from_civil;
42    use crate::TAI_EPOCH_UNIX_DAYS;
43
44    #[test]
45    fn irfpy_sample_2019() {
46        // jd_tt = 2458539.1321896296 → MSD ≈ 51598.61869774 (irfpy/NASA formula)
47        let jd_tt = 2_458_539.132_189_629_6;
48        let tai_jd = jd_tt - Duration::TT_MINUS_TAI.as_seconds_f64() / 86_400.0;
49        let days = tai_jd - crate::TAI_EPOCH_JD;
50        let ns = (days * NS_PER_DAY as f64).round() as i128;
51        let inst = Instant::from_tai_nanos(ns);
52        let mt = inst.mars_time();
53        assert!((mt.msd - 51_598.618_697_74).abs() < 1e-6, "msd={}", mt.msd);
54        let mtc_h = (51_598.618_697_74_f64).fract() * 24.0;
55        assert!((mt.mtc_hours - mtc_h).abs() < 1e-4);
56    }
57
58    #[test]
59    fn mtc_is_fraction_of_msd() {
60        let _ = unix_days_from_civil(2000, 1, 6) - TAI_EPOCH_UNIX_DAYS;
61        let inst = Instant::IAU_1977_TAI;
62        let mt = inst.mars_time();
63        let frac = mt.msd.rem_euclid(1.0);
64        assert!((mt.mtc_hours - frac * 24.0).abs() < 1e-12);
65    }
66}