1 use super::helpers::*;
2 use std::convert::From;
3 use std::ops::*;
4 use std::time;
5 
6 /// A duration type to represent an approximate span of time
7 #[derive(Copy, Clone, Debug, Hash, Ord, Eq, PartialOrd, PartialEq, Default)]
8 pub struct Duration(u64);
9 
10 impl Duration {
11     /// Creates a new `Duration` from the specified number of seconds and additional nanosecond
12     /// precision
13     #[inline]
new(sec: u64, nanos: u32) -> Duration14     pub fn new(sec: u64, nanos: u32) -> Duration {
15         Duration(_timespec_to_u64(sec, nanos))
16     }
17 
18     /// Creates a new Duration from the specified number of days
19     #[inline]
from_days(days: u64) -> Duration20     pub fn from_days(days: u64) -> Duration {
21         Duration(_sec_to_u64(days * 86400))
22     }
23 
24     /// Creates a new Duration from the specified number of hours
25     #[inline]
from_hours(hours: u64) -> Duration26     pub fn from_hours(hours: u64) -> Duration {
27         Duration(_sec_to_u64(hours * 3600))
28     }
29 
30     /// Creates a new Duration from the specified number of minutes
31     #[inline]
from_mins(mins: u64) -> Duration32     pub fn from_mins(mins: u64) -> Duration {
33         Duration(_sec_to_u64(mins * 60))
34     }
35 
36     /// Creates a new Duration from the specified number of seconds
37     #[inline]
from_secs(secs: u64) -> Duration38     pub fn from_secs(secs: u64) -> Duration {
39         Duration(_sec_to_u64(secs))
40     }
41 
42     /// Creates a new Duration from the specified number of milliseconds
43     #[inline]
from_millis(millis: u64) -> Duration44     pub fn from_millis(millis: u64) -> Duration {
45         Duration(_millis_to_u64(millis))
46     }
47 
48     /// Returns the number of days represented by this duration
49     #[inline]
as_days(&self) -> u6450     pub fn as_days(&self) -> u64 {
51         self.as_secs() / 86400
52     }
53 
54     /// Returns the number of minutes represented by this duration
55     #[inline]
as_hours(&self) -> u6456     pub fn as_hours(&self) -> u64 {
57         self.as_secs() / 3600
58     }
59 
60     /// Returns the number of minutes represented by this duration
61     #[inline]
as_mins(&self) -> u6462     pub fn as_mins(&self) -> u64 {
63         self.as_secs() / 60
64     }
65 
66     /// Returns the number of whole seconds represented by this duration
67     #[inline]
as_secs(&self) -> u6468     pub fn as_secs(&self) -> u64 {
69         self.0 >> 32
70     }
71 
72     /// Returns the number of whole milliseconds represented by this duration
73     #[inline]
as_millis(&self) -> u6474     pub fn as_millis(&self) -> u64 {
75         ((self.0 as u128 * 125) >> 29) as u64
76     }
77 
78     /// Returns the number of whole microseconds represented by this duration
79     #[inline]
as_micros(&self) -> u6480     pub fn as_micros(&self) -> u64 {
81         ((self.0 as u128 * 125_000) >> 29) as u64
82     }
83 
84     /// Returns the number of whole nanoseconds represented by this duration
85     #[inline]
as_nanos(&self) -> u6486     pub fn as_nanos(&self) -> u64 {
87         ((self.0 as u128 * 125_000_000) >> 29) as u64
88     }
89 
90     /// Returns the nanosecond precision represented by this duration
91     #[inline]
subsec_nanos(&self) -> u3292     pub fn subsec_nanos(&self) -> u32 {
93         ((self.0 as u32 as u64 * 125_000_000) >> 29) as u32
94     }
95 
96     #[doc(hidden)]
97     #[inline]
as_u64(&self) -> u6498     pub fn as_u64(&self) -> u64 {
99         self.0
100     }
101 
102     #[doc(hidden)]
103     #[inline]
from_u64(ts: u64) -> Duration104     pub fn from_u64(ts: u64) -> Duration {
105         Duration(ts)
106     }
107 
108     /// Returns the duration as a floating point number, representing the number of seconds
109     #[inline]
as_f64(&self) -> f64110     pub fn as_f64(&self) -> f64 {
111         (self.0 as f64) / ((1u64 << 32) as f64)
112     }
113 }
114 
115 #[doc(hidden)]
116 impl From<u64> for Duration {
117     #[doc(hidden)]
118     #[inline]
from(ts: u64) -> Duration119     fn from(ts: u64) -> Duration {
120         Duration::from_u64(ts)
121     }
122 }
123 
124 impl Add for Duration {
125     type Output = Duration;
126 
127     #[inline]
add(self, rhs: Duration) -> Duration128     fn add(self, rhs: Duration) -> Duration {
129         Duration(self.0 + rhs.0)
130     }
131 }
132 
133 impl AddAssign for Duration {
134     #[inline]
add_assign(&mut self, rhs: Duration)135     fn add_assign(&mut self, rhs: Duration) {
136         *self = *self + rhs;
137     }
138 }
139 
140 impl Sub for Duration {
141     type Output = Duration;
142 
143     #[inline]
sub(self, rhs: Duration) -> Duration144     fn sub(self, rhs: Duration) -> Duration {
145         Duration(self.0 - rhs.0)
146     }
147 }
148 
149 impl SubAssign for Duration {
150     #[inline]
sub_assign(&mut self, rhs: Duration)151     fn sub_assign(&mut self, rhs: Duration) {
152         *self = *self - rhs;
153     }
154 }
155 
156 impl Mul<u32> for Duration {
157     type Output = Duration;
158 
159     #[inline]
mul(self, rhs: u32) -> Duration160     fn mul(self, rhs: u32) -> Duration {
161         Duration(self.0 * rhs as u64)
162     }
163 }
164 
165 impl MulAssign<u32> for Duration {
166     #[inline]
mul_assign(&mut self, rhs: u32)167     fn mul_assign(&mut self, rhs: u32) {
168         *self = *self * rhs;
169     }
170 }
171 
172 impl Div<u32> for Duration {
173     type Output = Duration;
174 
175     #[inline]
div(self, rhs: u32) -> Duration176     fn div(self, rhs: u32) -> Duration {
177         Duration(self.0 / rhs as u64)
178     }
179 }
180 
181 impl DivAssign<u32> for Duration {
182     #[inline]
div_assign(&mut self, rhs: u32)183     fn div_assign(&mut self, rhs: u32) {
184         *self = *self / rhs;
185     }
186 }
187 
188 impl Into<time::Duration> for Duration {
189     #[inline]
into(self) -> time::Duration190     fn into(self) -> time::Duration {
191         time::Duration::new(self.as_secs(), self.subsec_nanos())
192     }
193 }
194 
195 impl From<time::Duration> for Duration {
196     #[inline]
from(duration_sys: time::Duration) -> Duration197     fn from(duration_sys: time::Duration) -> Duration {
198         Duration::new(duration_sys.as_secs(), duration_sys.subsec_nanos())
199     }
200 }
201