Skip to main content

deep_time/dt/
from_gps.rs

1use crate::{ATTOS_PER_WEEK, Dt, Real, Scale};
2
3impl Dt {
4    /// Creates a `Dt` in GPS Time (GPS) from a GPS week number and
5    /// Time of Week (TOW).
6    ///
7    /// This is the exact inverse of [`Self::to_gps_week_and_tow`].
8    ///
9    /// - `week`: Full GPS week number (can be negative for dates before 1980).
10    /// - `tow`: Time of Week as a [`Dt`]. Values ≥ 604800 seconds are
11    ///   automatically carried into the week number.
12    ///
13    /// The resulting `Dt` is always in `Scale::GPS`.
14    #[inline]
15    pub const fn from_gps_wk_and_tow(wk: i64, tow: Dt) -> Self {
16        let total_attos = (wk as i128) * ATTOS_PER_WEEK + tow.to_attos();
17        Self::GPS_EPOCH.add(Dt::from_attos(total_attos, Scale::TAI))
18    }
19
20    /// Creates a `Dt` in GPS Time from a GPS week number and
21    /// floating-point Time of Week.
22    ///
23    /// This is the floating-point counterpart to [`Self::from_gps_wk_and_tow`].
24    #[inline]
25    pub const fn from_gps_wk_and_tow_f(week: i64, tow: Real) -> Self {
26        let tow_span = Dt::from_sec_f(tow);
27        Self::from_gps_wk_and_tow(week, tow_span)
28    }
29
30    /// Inverse of [`Self::to_gps`].
31    pub const fn from_gps(elapsed: Dt) -> Self {
32        Self::GPS_EPOCH.add(elapsed)
33    }
34
35    /// Floating-point version of [`Self::from_gps`].
36    #[inline]
37    pub const fn from_gps_f(elapsed_sec: Real) -> Self {
38        Self::from_gps(Dt::from_sec_f(elapsed_sec))
39    }
40
41    /// Inverse of [`Self::to_cxcsec`].
42    pub const fn from_cxcsec(elapsed: Dt) -> Self {
43        Self::CXC_EPOCH.add(elapsed)
44    }
45
46    /// Floating-point counterpart of [`Self::from_cxcsec`].
47    #[inline]
48    pub const fn from_cxcsec_f(elapsed_sec: Real) -> Self {
49        Self::from_cxcsec(Dt::from_sec_f(elapsed_sec))
50    }
51
52    /// Inverse of [`Self::to_galexsec`].
53    pub const fn from_galexsec(elapsed: Dt) -> Self {
54        let epoch_utc = Self::GPS_EPOCH.to(Scale::TAI, Scale::UTC);
55        epoch_utc.add(elapsed).to(Scale::UTC, Scale::TAI)
56    }
57
58    /// Floating-point counterpart of [`Self::from_galexsec`].
59    #[inline]
60    pub const fn from_galexsec_f(elapsed_sec: Real) -> Self {
61        Self::from_galexsec(Dt::from_sec_f(elapsed_sec))
62    }
63}