coarsetime/
duration.rs

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use std::convert::From;
use std::ops::*;
use std::time;

use super::helpers::*;

/// A duration type to represent an approximate span of time
#[derive(Copy, Clone, Debug, Hash, Ord, Eq, PartialOrd, PartialEq, Default)]
pub struct Duration(u64);

impl Duration {
    /// Creates a new `Duration` from the specified number of seconds and
    /// additional nanosecond precision
    #[inline]
    pub fn new(sec: u64, nanos: u32) -> Duration {
        Duration(_timespec_to_u64(sec, nanos))
    }

    /// Creates a new Duration from the specified number of days
    #[inline]
    pub fn from_days(days: u64) -> Duration {
        Duration(_sec_to_u64(days * 86400))
    }

    /// Creates a new Duration from the specified number of hours
    #[inline]
    pub fn from_hours(hours: u64) -> Duration {
        Duration(_sec_to_u64(hours * 3600))
    }

    /// Creates a new Duration from the specified number of minutes
    #[inline]
    pub fn from_mins(mins: u64) -> Duration {
        Duration(_sec_to_u64(mins * 60))
    }

    /// Creates a new Duration from the specified number of seconds
    #[inline]
    pub fn from_secs(secs: u64) -> Duration {
        Duration(_sec_to_u64(secs))
    }

    /// Creates a new Duration from the specified number of milliseconds
    #[inline]
    pub fn from_millis(millis: u64) -> Duration {
        Duration(_millis_to_u64(millis))
    }

    /// Returns the number of days represented by this duration
    #[inline]
    pub fn as_days(&self) -> u64 {
        self.as_secs() / 86400
    }

    /// Returns the number of minutes represented by this duration
    #[inline]
    pub fn as_hours(&self) -> u64 {
        self.as_secs() / 3600
    }

    /// Returns the number of minutes represented by this duration
    #[inline]
    pub fn as_mins(&self) -> u64 {
        self.as_secs() / 60
    }

    /// Returns the number of whole seconds represented by this duration
    #[inline]
    pub fn as_secs(&self) -> u64 {
        self.0 >> 32
    }

    /// Returns the number of whole milliseconds represented by this duration
    #[inline]
    pub fn as_millis(&self) -> u64 {
        ((self.0 as u128 * 125) >> 29) as u64
    }

    /// Returns the number of whole microseconds represented by this duration
    #[inline]
    pub fn as_micros(&self) -> u64 {
        ((self.0 as u128 * 125_000) >> 29) as u64
    }

    /// Returns the number of whole nanoseconds represented by this duration
    #[inline]
    pub fn as_nanos(&self) -> u64 {
        ((self.0 as u128 * 125_000_000) >> 29) as u64
    }

    /// Returns the nanosecond precision represented by this duration
    #[inline]
    pub fn subsec_nanos(&self) -> u32 {
        ((self.0 as u32 as u64 * 125_000_000) >> 29) as u32
    }

    /// Return this duration as a number of "ticks".
    ///
    /// Note that length of a 'tick' is not guaranteed to represent
    /// the same amount of time across different platforms, or from
    /// one version of `coarsetime` to another.
    #[inline]
    pub fn as_ticks(&self) -> u64 {
        self.as_u64()
    }

    /// Creates a new Duration from the specified number of "ticks".
    ///
    /// Note that length of a 'tick' is not guaranteed to represent
    /// the same amount of time across different platforms, or from
    /// one version of `coarsetime` to another.
    #[inline]
    pub fn from_ticks(ticks: u64) -> Duration {
        Self::from_u64(ticks)
    }

    #[doc(hidden)]
    #[inline]
    pub fn as_u64(&self) -> u64 {
        self.0
    }

    #[doc(hidden)]
    #[inline]
    pub fn from_u64(ts: u64) -> Duration {
        Duration(ts)
    }

    /// Returns the duration as a floating point number, representing the number
    /// of seconds
    #[inline]
    pub fn as_f64(&self) -> f64 {
        (self.0 as f64) / ((1u64 << 32) as f64)
    }

    /// Returns the absolute difference between two `Duration`s
    #[inline]
    pub fn abs_diff(&self, other: Duration) -> Duration {
        Duration(self.0.abs_diff(other.0))
    }

    /// Add two durations, saturating on overflow
    #[inline]
    pub fn saturating_add(self, rhs: Duration) -> Duration {
        Duration(self.0.saturating_add(rhs.0))
    }

    /// Add two durations, returning `None` on overflow
    #[inline]
    pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
        self.0.checked_add(rhs.0).map(Duration)
    }

    /// Subtract two durations, saturating on underflow/overflow
    #[inline]
    pub fn saturating_sub(self, rhs: Duration) -> Duration {
        Duration(self.0.saturating_sub(rhs.0))
    }

    /// Subtract two durations, returning `None` on underflow/overflow
    #[inline]
    pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
        self.0.checked_sub(rhs.0).map(Duration)
    }

    /// Multiply a duration by a scalar, saturating on overflow
    #[inline]
    pub fn saturating_mul(self, rhs: u32) -> Duration {
        Duration(self.0.saturating_mul(rhs as u64))
    }

    /// Multiply a duration by a scalar, returning `None` on overflow
    #[inline]
    pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
        self.0.checked_mul(rhs as u64).map(Duration)
    }

    /// Divide a duration by a scalar, returning `None` for division by zero
    #[inline]
    pub fn checked_div(self, rhs: u32) -> Option<Duration> {
        self.0.checked_div(rhs as u64).map(Duration)
    }
}

#[doc(hidden)]
impl From<u64> for Duration {
    #[doc(hidden)]
    #[inline]
    fn from(ts: u64) -> Duration {
        Duration::from_u64(ts)
    }
}

impl Add for Duration {
    type Output = Duration;

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

impl AddAssign for Duration {
    #[inline]
    fn add_assign(&mut self, rhs: Duration) {
        *self = *self + rhs;
    }
}

impl Sub for Duration {
    type Output = Duration;

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

impl SubAssign for Duration {
    #[inline]
    fn sub_assign(&mut self, rhs: Duration) {
        *self = *self - rhs;
    }
}

impl Mul<u32> for Duration {
    type Output = Duration;

    #[inline]
    fn mul(self, rhs: u32) -> Duration {
        Duration(self.0 * rhs as u64)
    }
}

impl MulAssign<u32> for Duration {
    #[inline]
    fn mul_assign(&mut self, rhs: u32) {
        *self = *self * rhs;
    }
}

impl Div<u32> for Duration {
    type Output = Duration;

    #[inline]
    fn div(self, rhs: u32) -> Duration {
        Duration(self.0 / rhs as u64)
    }
}

impl DivAssign<u32> for Duration {
    #[inline]
    fn div_assign(&mut self, rhs: u32) {
        *self = *self / rhs;
    }
}

impl From<Duration> for time::Duration {
    #[inline]
    fn from(duration: Duration) -> time::Duration {
        time::Duration::new(duration.as_secs(), duration.subsec_nanos())
    }
}

impl From<time::Duration> for Duration {
    #[inline]
    fn from(duration_sys: time::Duration) -> Duration {
        Duration::new(duration_sys.as_secs(), duration_sys.subsec_nanos())
    }
}