1use crate::{Dt, Scale};
2use hifitime::{Duration, Epoch};
3
4impl Dt {
5 pub fn to_hifitime_epoch(&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 pub fn from_hifitime_epoch(epoch: Epoch) -> Self {
30 let ns_since_j1900 = epoch.to_tai_duration().total_nanoseconds();
31
32 let j1900 = Epoch::from_gregorian_tai(1900, 1, 1, 12, 0, 0, 0);
33 let j2000 = Epoch::from_gregorian_tai(2000, 1, 1, 12, 0, 0, 0);
34 let offset_ns = j2000.to_tai_duration().total_nanoseconds()
35 - j1900.to_tai_duration().total_nanoseconds();
36
37 let ns_since_zero_tai = ns_since_j1900 - offset_ns;
38 Self::from_ns(ns_since_zero_tai, Scale::TAI)
39 }
40
41 #[inline]
50 pub fn to_hifitime_duration(&self) -> Duration {
51 Duration::from_total_nanoseconds(self.to_attos() / 1_000_000_000i128)
52 }
53
54 #[inline]
58 pub fn from_hifitime_duration(dur: Duration) -> Self {
59 Self::from_ns(dur.total_nanoseconds(), Scale::TAI)
60 }
61}