coarsetime/
duration.rs

1use std::convert::From;
2use std::ops::*;
3use std::time;
4
5use super::helpers::*;
6
7/// A duration type to represent an approximate span of time
8#[derive(Copy, Clone, Debug, Hash, Ord, Eq, PartialOrd, PartialEq, Default)]
9pub struct Duration(u64);
10
11impl Duration {
12    /// Creates a new `Duration` from the specified number of seconds and
13    /// additional nanosecond precision
14    #[inline]
15    pub fn new(sec: u64, nanos: u32) -> Duration {
16        Duration(_timespec_to_u64(sec, nanos))
17    }
18
19    /// Creates a new Duration from the specified number of days
20    #[inline]
21    pub fn from_days(days: u64) -> Duration {
22        Duration(_sec_to_u64(days * 86400))
23    }
24
25    /// Creates a new Duration from the specified number of hours
26    #[inline]
27    pub fn from_hours(hours: u64) -> Duration {
28        Duration(_sec_to_u64(hours * 3600))
29    }
30
31    /// Creates a new Duration from the specified number of minutes
32    #[inline]
33    pub fn from_mins(mins: u64) -> Duration {
34        Duration(_sec_to_u64(mins * 60))
35    }
36
37    /// Creates a new Duration from the specified number of seconds
38    #[inline]
39    pub fn from_secs(secs: u64) -> Duration {
40        Duration(_sec_to_u64(secs))
41    }
42
43    /// Creates a new Duration from the specified number of milliseconds
44    #[inline]
45    pub fn from_millis(millis: u64) -> Duration {
46        Duration(_millis_to_u64(millis))
47    }
48
49    /// Returns the number of days represented by this duration
50    #[inline]
51    pub fn as_days(&self) -> u64 {
52        self.as_secs() / 86400
53    }
54
55    /// Returns the number of minutes represented by this duration
56    #[inline]
57    pub fn as_hours(&self) -> u64 {
58        self.as_secs() / 3600
59    }
60
61    /// Returns the number of minutes represented by this duration
62    #[inline]
63    pub fn as_mins(&self) -> u64 {
64        self.as_secs() / 60
65    }
66
67    /// Returns the number of whole seconds represented by this duration
68    #[inline]
69    pub fn as_secs(&self) -> u64 {
70        self.0 >> 32
71    }
72
73    /// Returns the number of whole milliseconds represented by this duration
74    #[inline]
75    pub fn as_millis(&self) -> u64 {
76        ((self.0 as u128 * 125) >> 29) as u64
77    }
78
79    /// Returns the number of whole microseconds represented by this duration
80    #[inline]
81    pub fn as_micros(&self) -> u64 {
82        ((self.0 as u128 * 125_000) >> 29) as u64
83    }
84
85    /// Returns the number of whole nanoseconds represented by this duration
86    #[inline]
87    pub fn as_nanos(&self) -> u64 {
88        ((self.0 as u128 * 125_000_000) >> 29) as u64
89    }
90
91    /// Returns the nanosecond precision represented by this duration
92    #[inline]
93    pub fn subsec_nanos(&self) -> u32 {
94        ((self.0 as u32 as u64 * 125_000_000) >> 29) as u32
95    }
96
97    /// Return this duration as a number of "ticks".
98    ///
99    /// Note that length of a 'tick' is not guaranteed to represent
100    /// the same amount of time across different platforms, or from
101    /// one version of `coarsetime` to another.
102    #[inline]
103    pub fn as_ticks(&self) -> u64 {
104        self.as_u64()
105    }
106
107    /// Creates a new Duration from the specified number of "ticks".
108    ///
109    /// Note that length of a 'tick' is not guaranteed to represent
110    /// the same amount of time across different platforms, or from
111    /// one version of `coarsetime` to another.
112    #[inline]
113    pub fn from_ticks(ticks: u64) -> Duration {
114        Self::from_u64(ticks)
115    }
116
117    #[doc(hidden)]
118    #[inline]
119    pub fn as_u64(&self) -> u64 {
120        self.0
121    }
122
123    #[doc(hidden)]
124    #[inline]
125    pub fn from_u64(ts: u64) -> Duration {
126        Duration(ts)
127    }
128
129    /// Returns the duration as a floating point number, representing the number
130    /// of seconds
131    #[inline]
132    pub fn as_f64(&self) -> f64 {
133        (self.0 as f64) / ((1u64 << 32) as f64)
134    }
135
136    /// Returns the absolute difference between two `Duration`s
137    #[inline]
138    pub fn abs_diff(&self, other: Duration) -> Duration {
139        Duration(self.0.abs_diff(other.0))
140    }
141
142    /// Add two durations, saturating on overflow
143    #[inline]
144    pub fn saturating_add(self, rhs: Duration) -> Duration {
145        Duration(self.0.saturating_add(rhs.0))
146    }
147
148    /// Add two durations, returning `None` on overflow
149    #[inline]
150    pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
151        self.0.checked_add(rhs.0).map(Duration)
152    }
153
154    /// Subtract two durations, saturating on underflow/overflow
155    #[inline]
156    pub fn saturating_sub(self, rhs: Duration) -> Duration {
157        Duration(self.0.saturating_sub(rhs.0))
158    }
159
160    /// Subtract two durations, returning `None` on underflow/overflow
161    #[inline]
162    pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
163        self.0.checked_sub(rhs.0).map(Duration)
164    }
165
166    /// Multiply a duration by a scalar, saturating on overflow
167    #[inline]
168    pub fn saturating_mul(self, rhs: u32) -> Duration {
169        Duration(self.0.saturating_mul(rhs as u64))
170    }
171
172    /// Multiply a duration by a scalar, returning `None` on overflow
173    #[inline]
174    pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
175        self.0.checked_mul(rhs as u64).map(Duration)
176    }
177
178    /// Divide a duration by a scalar, returning `None` for division by zero
179    #[inline]
180    pub fn checked_div(self, rhs: u32) -> Option<Duration> {
181        self.0.checked_div(rhs as u64).map(Duration)
182    }
183}
184
185#[doc(hidden)]
186impl From<u64> for Duration {
187    #[doc(hidden)]
188    #[inline]
189    fn from(ts: u64) -> Duration {
190        Duration::from_u64(ts)
191    }
192}
193
194impl Add for Duration {
195    type Output = Duration;
196
197    #[inline]
198    fn add(self, rhs: Duration) -> Duration {
199        Duration(self.0 + rhs.0)
200    }
201}
202
203impl AddAssign for Duration {
204    #[inline]
205    fn add_assign(&mut self, rhs: Duration) {
206        *self = *self + rhs;
207    }
208}
209
210impl Sub for Duration {
211    type Output = Duration;
212
213    #[inline]
214    fn sub(self, rhs: Duration) -> Duration {
215        Duration(self.0 - rhs.0)
216    }
217}
218
219impl SubAssign for Duration {
220    #[inline]
221    fn sub_assign(&mut self, rhs: Duration) {
222        *self = *self - rhs;
223    }
224}
225
226impl Mul<u32> for Duration {
227    type Output = Duration;
228
229    #[inline]
230    fn mul(self, rhs: u32) -> Duration {
231        Duration(self.0 * rhs as u64)
232    }
233}
234
235impl MulAssign<u32> for Duration {
236    #[inline]
237    fn mul_assign(&mut self, rhs: u32) {
238        *self = *self * rhs;
239    }
240}
241
242impl Div<u32> for Duration {
243    type Output = Duration;
244
245    #[inline]
246    fn div(self, rhs: u32) -> Duration {
247        Duration(self.0 / rhs as u64)
248    }
249}
250
251impl DivAssign<u32> for Duration {
252    #[inline]
253    fn div_assign(&mut self, rhs: u32) {
254        *self = *self / rhs;
255    }
256}
257
258impl From<Duration> for time::Duration {
259    #[inline]
260    fn from(duration: Duration) -> time::Duration {
261        time::Duration::new(duration.as_secs(), duration.subsec_nanos())
262    }
263}
264
265impl From<time::Duration> for Duration {
266    #[inline]
267    fn from(duration_sys: time::Duration) -> Duration {
268        Duration::new(duration_sys.as_secs(), duration_sys.subsec_nanos())
269    }
270}