monotonic_clock/
epoch.rs

1use ::std::time::Duration;
2
3/// Provides a starting timestamp in nanoseconds from UNIX_EPOCH.
4#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct Epoch(Duration);
6
7impl Epoch {
8    /// Returns the current time as a UnixTimeStamp.
9    #[inline]
10    pub fn from_unix() -> Self {
11        Self(
12            ::std::time::SystemTime::now()
13                .duration_since(::std::time::UNIX_EPOCH)
14                .unwrap(),
15        )
16    }
17
18    /// Creates Epoch with a base duration.
19    #[inline]
20    pub fn from(duration: Duration) -> Self {
21        Self(duration)
22    }
23
24    /// Returns a zeroed Epoch.
25    #[inline]
26    pub fn from_zero() -> Self {
27        Self::from(Duration::new(0, 0))
28    }
29}
30
31impl ::std::default::Default for Epoch {
32    #[inline]
33    fn default() -> Self {
34        Self::from_unix()
35    }
36}
37
38impl ::std::ops::Add for Epoch {
39    type Output = Self;
40
41    #[inline]
42    fn add(self, rhs: Self) -> Self::Output {
43        Self(self.0 + rhs.0)
44    }
45}
46
47impl ::std::ops::Sub for Epoch {
48    type Output = Self;
49
50    #[inline]
51    fn sub(self, rhs: Self) -> Self::Output {
52        Self(self.0 - rhs.0)
53    }
54}
55
56impl ::std::ops::Add<Duration> for Epoch {
57    type Output = Self;
58
59    #[inline]
60    fn add(self, rhs: Duration) -> Self::Output {
61        Self(self.0 + rhs)
62    }
63}
64
65impl ::std::ops::Sub<Duration> for Epoch {
66    type Output = Self;
67
68    #[inline]
69    fn sub(self, rhs: Duration) -> Self::Output {
70        Self(self.0 - rhs)
71    }
72}
73
74impl ::std::ops::AddAssign for Epoch {
75    #[inline]
76    fn add_assign(&mut self, rhs: Self) {
77        self.0 += rhs.0;
78    }
79}
80
81impl ::std::ops::SubAssign for Epoch {
82    #[inline]
83    fn sub_assign(&mut self, rhs: Self) {
84        self.0 -= rhs.0;
85    }
86}
87
88impl ::std::ops::AddAssign<Duration> for Epoch {
89    #[inline]
90    fn add_assign(&mut self, rhs: Duration) {
91        self.0 += rhs;
92    }
93}
94
95impl ::std::ops::SubAssign<Duration> for Epoch {
96    #[inline]
97    fn sub_assign(&mut self, rhs: Duration) {
98        self.0 -= rhs;
99    }
100}
101
102impl ::std::ops::Deref for Epoch {
103    type Target = Duration;
104
105    #[inline]
106    fn deref(&self) -> &Self::Target {
107        &self.0
108    }
109}
110
111impl ::std::ops::DerefMut for Epoch {
112    #[inline]
113    fn deref_mut(&mut self) -> &mut Self::Target {
114        &mut self.0
115    }
116}
117
118impl ::std::fmt::Display for Epoch {
119    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
120        write!(
121            f,
122            "{}",
123            self.0.as_secs() as f64 + self.0.subsec_nanos() as f64 * 1e-9
124        )
125    }
126}
127
128impl From<Duration> for Epoch {
129    #[inline]
130    fn from(duration: Duration) -> Self {
131        Self(duration)
132    }
133}
134
135impl From<Epoch> for Duration {
136    #[inline]
137    fn from(epoch: Epoch) -> Self {
138        epoch.0
139    }
140}