1use crate::{Dt, Scale};
2use core::convert::From;
3use hifitime::{Duration, Epoch};
4
5impl Dt {
6 pub fn to_hifitime_epoch(&self) -> Epoch {
11 let nanos = self.to_tai().to_ns().0;
12
13 let j2000 = Epoch::from_gregorian_tai(2000, 1, 1, 12, 0, 0, 0);
15 let ns_since_j1900 = nanos.saturating_add(j2000.to_tai_duration().total_nanoseconds());
16
17 Epoch::from_tai_duration(Duration::from_total_nanoseconds(ns_since_j1900))
18 }
19
20 pub fn from_hifitime_epoch(epoch: Epoch) -> Dt {
22 let ns_since_j1900 = epoch.to_tai_duration().total_nanoseconds();
23
24 let j2000 = Epoch::from_gregorian_tai(2000, 1, 1, 12, 0, 0, 0);
25 let ns_since_zero_tai =
26 ns_since_j1900.saturating_sub(j2000.to_tai_duration().total_nanoseconds());
27 Self::from_ns(ns_since_zero_tai, 0, Scale::TAI, Scale::TAI)
28 }
29
30 #[inline(always)]
39 pub fn to_hifitime_duration(&self) -> Duration {
40 Duration::from_total_nanoseconds(self.to_ns().0)
41 }
42
43 #[inline(always)]
47 pub fn from_hifitime_duration(dur: Duration) -> Dt {
48 Self::from_ns(dur.total_nanoseconds(), 0, Scale::TAI, Scale::TAI)
49 }
50}
51
52impl From<Epoch> for Dt {
53 #[inline]
54 fn from(epoch: Epoch) -> Self {
55 Self::from_hifitime_epoch(epoch)
56 }
57}
58
59impl From<Dt> for Epoch {
60 #[inline]
61 fn from(dt: Dt) -> Self {
62 dt.to_hifitime_epoch()
63 }
64}
65
66impl From<Duration> for Dt {
67 #[inline]
68 fn from(dur: Duration) -> Self {
69 Self::from_hifitime_duration(dur)
70 }
71}
72
73impl From<Dt> for Duration {
74 #[inline]
75 fn from(dt: Dt) -> Self {
76 dt.to_hifitime_duration()
77 }
78}