1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


use ::std::time::Duration;

/// Provides a starting timestamp in nanoseconds from UNIX_EPOCH.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Epoch(Duration);

impl Epoch {
    /// Returns the current time as a UnixTimeStamp.
    #[inline]
    pub fn from_unix() -> Self {
        Self(::std::time::SystemTime::now().duration_since(::std::time::UNIX_EPOCH).unwrap())
    }

    /// Creates Epoch with a base duration.
    #[inline]
    pub fn from(duration: Duration) -> Self {
        Self(duration)
    }

    /// Returns a zeroed Epoch.
    #[inline]
    pub fn from_zero() -> Self {
        Self::from(Duration::new(0, 0))
    }

}

impl ::std::default::Default for Epoch {
    #[inline]
    fn default() -> Self {
        Self::from_unix()
    }
}

impl ::std::ops::Add for Epoch {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl ::std::ops::Sub for Epoch {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl ::std::ops::Add<Duration> for Epoch {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Duration) -> Self::Output {
        Self(self.0 + rhs)
    }
}

impl ::std::ops::Sub<Duration> for Epoch {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Duration) -> Self::Output {
        Self(self.0 - rhs)
    }
}

impl ::std::ops::AddAssign for Epoch {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

impl ::std::ops::SubAssign for Epoch {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
    }
}

impl ::std::ops::AddAssign<Duration> for Epoch {
    #[inline]
    fn add_assign(&mut self, rhs: Duration) {
        self.0 += rhs;
    }
}

impl ::std::ops::SubAssign<Duration> for Epoch {
    #[inline]
    fn sub_assign(&mut self, rhs: Duration) {
        self.0 -= rhs;
    }
}


impl ::std::ops::Deref for Epoch {
    type Target = Duration;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ::std::ops::DerefMut for Epoch {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl ::std::fmt::Display for Epoch {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{}", self.0.as_secs() as f64 + self.0.subsec_nanos() as f64 * 1e-9)
    }
}

impl From<Duration> for Epoch {
    #[inline]
    fn from(duration: Duration) -> Self {
        Self(duration)
    }
}

impl From<Epoch> for Duration {
    #[inline]
    fn from(epoch: Epoch) -> Self {
        epoch.0
    }
}