Skip to main content

deep_time/dt/
to_gps.rs

1use crate::{ATTOS_PER_SEC_I128, ATTOS_PER_WEEK, Dt, Real, SEC_PER_DAYI64, Scale};
2
3impl Dt {
4    /// Returns the GPS week number and exact Time of Week (TOW) for this instant
5    /// when expressed in GPS Time.
6    pub const fn to_gps_wk_and_tow(&self, current: Scale) -> (i64, Dt) {
7        let total_attos = self.to_gps(current).to_attos();
8
9        let wk = total_attos.div_euclid(ATTOS_PER_WEEK) as i64;
10        let tow_attos = total_attos.rem_euclid(ATTOS_PER_WEEK);
11
12        (wk, Dt::from_attos(tow_attos, Scale::TAI))
13    }
14
15    /// Returns the day of the GPS week (0 = Sunday, 1 = Monday, …, 6 = Saturday).
16    ///
17    /// This is computed directly from GPS Time and is independent of the
18    /// Gregorian calendar.
19    pub const fn to_gps_day_of_wk(&self, current: Scale) -> u8 {
20        let (_, tow) = self.to_gps_wk_and_tow(current);
21        let secs = tow.to_attos() / ATTOS_PER_SEC_I128;
22
23        (secs / SEC_PER_DAYI64 as i128) as u8
24    }
25
26    /// Returns the Time of Week (TOW) as a floating-point value in seconds.
27    ///
28    /// This is a convenience method for code that prefers `f64` / `Real`.
29    /// For full attosecond precision use [`Self::to_gps_wk_and_tow`].
30    #[inline]
31    pub const fn to_gps_tow_f(&self, current: Scale) -> Real {
32        let (_, tow) = self.to_gps_wk_and_tow(current);
33        tow.to_sec_f()
34    }
35
36    /// Returns only the GPS week number.
37    #[inline]
38    pub const fn to_gps_wk(&self, current: Scale) -> i64 {
39        self.to_gps_wk_and_tow(current).0
40    }
41
42    #[inline]
43    pub const fn to_galexsec(&self, current: Scale) -> Dt {
44        self.to(current, Scale::UTC)
45            .to_diff_raw(Dt::GPS_EPOCH.to(Scale::TAI, Scale::UTC))
46    }
47
48    #[inline]
49    pub const fn to_gps(&self, current: Scale) -> Dt {
50        self.to(current, Scale::GPS)
51            .to_diff_raw(Dt::GPS_EPOCH.to(Scale::TAI, Scale::GPS))
52    }
53
54    #[inline]
55    pub const fn to_cxcsec(&self, current: Scale) -> Dt {
56        self.to(current, Scale::TT)
57            .to_diff_raw(Dt::CXC_EPOCH.to(Scale::TAI, Scale::TT))
58    }
59}