Skip to main content

deep_time/dt/
hifitime.rs

1use crate::{Dt, Scale};
2use hifitime::{Duration, Epoch};
3
4impl Dt {
5    /// Converts this `Dt` to a `hifitime::Epoch` (TAI scale).
6    ///
7    /// Round-trips perfectly with [`Dt::from_hifitime`].
8    pub fn to_hifitime(&self, current: Scale) -> Epoch {
9        let nanos = self.to(current, Scale::TAI).to_ns();
10
11        let j1900 = Epoch::from_gregorian_tai(1900, 1, 1, 12, 0, 0, 0);
12        let j2000 = Epoch::from_gregorian_tai(2000, 1, 1, 12, 0, 0, 0);
13        let offset_ns = j2000.to_tai_duration().total_nanoseconds()
14            - j1900.to_tai_duration().total_nanoseconds();
15
16        let ns_since_j1900 = nanos + offset_ns;
17
18        let dur = Duration::from_total_nanoseconds(ns_since_j1900);
19        let (centuries, nanos) = dur.to_parts();
20
21        Epoch::from_tai_parts(centuries, nanos)
22    }
23
24    /// Converts this `Span` to a [`hifitime::Duration`] (nanosecond precision).
25    ///
26    /// - Sub-nanosecond attoseconds are **truncated toward zero**.
27    /// - The conversion is fully exact up to the nanosecond (128-bit integer arithmetic).
28    /// - Internally uses [`hifitime::Duration::from_total_nanoseconds`], which
29    ///   automatically normalizes centuries/nanoseconds and saturates at
30    ///   [`Duration::MAX`] / [`Duration::MIN`] if outside hifitime's range
31    ///   (±32,768 centuries).
32    #[inline]
33    pub fn to_hifitime_duration(&self) -> Duration {
34        Duration::from_total_nanoseconds(self.to_attos() / 1_000_000_000i128)
35    }
36
37    /// Creates a `Dt` from a `hifitime::Epoch`.
38    ///
39    /// - The conversion is exact (within hifitime's nanosecond precision).
40    /// - Uses a runtime-computed offset so it always matches whatever
41    ///   calendar math hifitime uses (including negative years).
42    pub fn from_hifitime_epoch(epoch: Epoch) -> Self {
43        let ns_since_j1900 = epoch.to_tai_duration().total_nanoseconds();
44
45        let j1900 = Epoch::from_gregorian_tai(1900, 1, 1, 12, 0, 0, 0);
46        let j2000 = Epoch::from_gregorian_tai(2000, 1, 1, 12, 0, 0, 0);
47        let offset_ns = j2000.to_tai_duration().total_nanoseconds()
48            - j1900.to_tai_duration().total_nanoseconds();
49
50        let ns_since_zero_tai = ns_since_j1900 - offset_ns;
51        Self::from_ns(ns_since_zero_tai, Scale::TAI)
52    }
53
54    /// Creates a `Dt` from a `hifitime::Duration` (nanosecond precision).
55    ///
56    /// This is the **exact reverse** of [`Dt::to_hifitime_duration`].
57    #[inline]
58    pub fn from_hifitime_duration(dur: Duration) -> Self {
59        Self::from_ns(dur.total_nanoseconds(), Scale::TAI)
60    }
61}