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
143#[doc(hidden)]
144impl From<u64> for Duration {
145    #[doc(hidden)]
146    #[inline]
147    fn from(ts: u64) -> Duration {
148        Duration::from_u64(ts)
149    }
150}
151
152impl Add for Duration {
153    type Output = Duration;
154
155    #[inline]
156    fn add(self, rhs: Duration) -> Duration {
157        Duration(self.0 + rhs.0)
158    }
159}
160
161impl AddAssign for Duration {
162    #[inline]
163    fn add_assign(&mut self, rhs: Duration) {
164        *self = *self + rhs;
165    }
166}
167
168impl Sub for Duration {
169    type Output = Duration;
170
171    #[inline]
172    fn sub(self, rhs: Duration) -> Duration {
173        Duration(self.0 - rhs.0)
174    }
175}
176
177impl SubAssign for Duration {
178    #[inline]
179    fn sub_assign(&mut self, rhs: Duration) {
180        *self = *self - rhs;
181    }
182}
183
184impl Mul<u32> for Duration {
185    type Output = Duration;
186
187    #[inline]
188    fn mul(self, rhs: u32) -> Duration {
189        Duration(self.0 * rhs as u64)
190    }
191}
192
193impl MulAssign<u32> for Duration {
194    #[inline]
195    fn mul_assign(&mut self, rhs: u32) {
196        *self = *self * rhs;
197    }
198}
199
200impl Div<u32> for Duration {
201    type Output = Duration;
202
203    #[inline]
204    fn div(self, rhs: u32) -> Duration {
205        Duration(self.0 / rhs as u64)
206    }
207}
208
209impl DivAssign<u32> for Duration {
210    #[inline]
211    fn div_assign(&mut self, rhs: u32) {
212        *self = *self / rhs;
213    }
214}
215
216impl From<Duration> for time::Duration {
217    #[inline]
218    fn from(duration: Duration) -> time::Duration {
219        time::Duration::new(duration.as_secs(), duration.subsec_nanos())
220    }
221}
222
223impl From<time::Duration> for Duration {
224    #[inline]
225    fn from(duration_sys: time::Duration) -> Duration {
226        Duration::new(duration_sys.as_secs(), duration_sys.subsec_nanos())
227    }
228}