@@ -45,19 +45,22 @@ macro_rules! try_opt {
45
45
46
46
/// ISO 8601 time duration with nanosecond precision.
47
47
/// This also allows for the negative duration; see individual methods for details.
48
+ #[ unstable( feature = "std_misc" ) ]
48
49
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
49
50
pub struct Duration {
50
51
secs : i64 ,
51
52
nanos : i32 , // Always 0 <= nanos < NANOS_PER_SEC
52
53
}
53
54
54
55
/// The minimum possible `Duration`: `i64::MIN` milliseconds.
56
+ #[ unstable( feature = "std_misc" ) ]
55
57
pub const MIN : Duration = Duration {
56
58
secs : i64:: MIN / MILLIS_PER_SEC - 1 ,
57
59
nanos : NANOS_PER_SEC + ( i64:: MIN % MILLIS_PER_SEC ) as i32 * NANOS_PER_MILLI
58
60
} ;
59
61
60
62
/// The maximum possible `Duration`: `i64::MAX` milliseconds.
63
+ #[ unstable( feature = "std_misc" ) ]
61
64
pub const MAX : Duration = Duration {
62
65
secs : i64:: MAX / MILLIS_PER_SEC ,
63
66
nanos : ( i64:: MAX % MILLIS_PER_SEC ) as i32 * NANOS_PER_MILLI
@@ -68,6 +71,7 @@ impl Duration {
68
71
/// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60), with overflow checks.
69
72
/// Panics when the duration is out of bounds.
70
73
#[ inline]
74
+ #[ unstable( feature = "std_misc" ) ]
71
75
pub fn weeks ( weeks : i64 ) -> Duration {
72
76
let secs = weeks. checked_mul ( SECS_PER_WEEK ) . expect ( "Duration::weeks out of bounds" ) ;
73
77
Duration :: seconds ( secs)
@@ -77,6 +81,7 @@ impl Duration {
77
81
/// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
78
82
/// Panics when the duration is out of bounds.
79
83
#[ inline]
84
+ #[ unstable( feature = "std_misc" ) ]
80
85
pub fn days ( days : i64 ) -> Duration {
81
86
let secs = days. checked_mul ( SECS_PER_DAY ) . expect ( "Duration::days out of bounds" ) ;
82
87
Duration :: seconds ( secs)
@@ -86,6 +91,7 @@ impl Duration {
86
91
/// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
87
92
/// Panics when the duration is out of bounds.
88
93
#[ inline]
94
+ #[ unstable( feature = "std_misc" ) ]
89
95
pub fn hours ( hours : i64 ) -> Duration {
90
96
let secs = hours. checked_mul ( SECS_PER_HOUR ) . expect ( "Duration::hours ouf of bounds" ) ;
91
97
Duration :: seconds ( secs)
@@ -95,6 +101,7 @@ impl Duration {
95
101
/// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
96
102
/// Panics when the duration is out of bounds.
97
103
#[ inline]
104
+ #[ unstable( feature = "std_misc" ) ]
98
105
pub fn minutes ( minutes : i64 ) -> Duration {
99
106
let secs = minutes. checked_mul ( SECS_PER_MINUTE ) . expect ( "Duration::minutes out of bounds" ) ;
100
107
Duration :: seconds ( secs)
@@ -104,6 +111,7 @@ impl Duration {
104
111
/// Panics when the duration is more than `i64::MAX` milliseconds
105
112
/// or less than `i64::MIN` milliseconds.
106
113
#[ inline]
114
+ #[ unstable( feature = "std_misc" ) ]
107
115
pub fn seconds ( seconds : i64 ) -> Duration {
108
116
let d = Duration { secs : seconds, nanos : 0 } ;
109
117
if d < MIN || d > MAX {
@@ -114,6 +122,7 @@ impl Duration {
114
122
115
123
/// Makes a new `Duration` with given number of milliseconds.
116
124
#[ inline]
125
+ #[ unstable( feature = "std_misc" ) ]
117
126
pub fn milliseconds ( milliseconds : i64 ) -> Duration {
118
127
let ( secs, millis) = div_mod_floor_64 ( milliseconds, MILLIS_PER_SEC ) ;
119
128
let nanos = millis as i32 * NANOS_PER_MILLI ;
@@ -122,6 +131,7 @@ impl Duration {
122
131
123
132
/// Makes a new `Duration` with given number of microseconds.
124
133
#[ inline]
134
+ #[ unstable( feature = "std_misc" ) ]
125
135
pub fn microseconds ( microseconds : i64 ) -> Duration {
126
136
let ( secs, micros) = div_mod_floor_64 ( microseconds, MICROS_PER_SEC ) ;
127
137
let nanos = micros as i32 * NANOS_PER_MICRO ;
@@ -130,13 +140,15 @@ impl Duration {
130
140
131
141
/// Makes a new `Duration` with given number of nanoseconds.
132
142
#[ inline]
143
+ #[ unstable( feature = "std_misc" ) ]
133
144
pub fn nanoseconds ( nanos : i64 ) -> Duration {
134
145
let ( secs, nanos) = div_mod_floor_64 ( nanos, NANOS_PER_SEC as i64 ) ;
135
146
Duration { secs : secs, nanos : nanos as i32 }
136
147
}
137
148
138
149
/// Runs a closure, returning the duration of time it took to run the
139
150
/// closure.
151
+ #[ unstable( feature = "std_misc" ) ]
140
152
pub fn span < F > ( f : F ) -> Duration where F : FnOnce ( ) {
141
153
let before = super :: precise_time_ns ( ) ;
142
154
f ( ) ;
@@ -145,28 +157,33 @@ impl Duration {
145
157
146
158
/// Returns the total number of whole weeks in the duration.
147
159
#[ inline]
160
+ #[ unstable( feature = "std_misc" ) ]
148
161
pub fn num_weeks ( & self ) -> i64 {
149
162
self . num_days ( ) / 7
150
163
}
151
164
152
165
/// Returns the total number of whole days in the duration.
166
+ #[ unstable( feature = "std_misc" ) ]
153
167
pub fn num_days ( & self ) -> i64 {
154
168
self . num_seconds ( ) / SECS_PER_DAY
155
169
}
156
170
157
171
/// Returns the total number of whole hours in the duration.
158
172
#[ inline]
173
+ #[ unstable( feature = "std_misc" ) ]
159
174
pub fn num_hours ( & self ) -> i64 {
160
175
self . num_seconds ( ) / SECS_PER_HOUR
161
176
}
162
177
163
178
/// Returns the total number of whole minutes in the duration.
164
179
#[ inline]
180
+ #[ unstable( feature = "std_misc" ) ]
165
181
pub fn num_minutes ( & self ) -> i64 {
166
182
self . num_seconds ( ) / SECS_PER_MINUTE
167
183
}
168
184
169
185
/// Returns the total number of whole seconds in the duration.
186
+ #[ unstable( feature = "std_misc" ) ]
170
187
pub fn num_seconds ( & self ) -> i64 {
171
188
// If secs is negative, nanos should be subtracted from the duration.
172
189
if self . secs < 0 && self . nanos > 0 {
@@ -188,6 +205,7 @@ impl Duration {
188
205
}
189
206
190
207
/// Returns the total number of whole milliseconds in the duration,
208
+ #[ unstable( feature = "std_misc" ) ]
191
209
pub fn num_milliseconds ( & self ) -> i64 {
192
210
// A proper Duration will not overflow, because MIN and MAX are defined
193
211
// such that the range is exactly i64 milliseconds.
@@ -198,6 +216,7 @@ impl Duration {
198
216
199
217
/// Returns the total number of whole microseconds in the duration,
200
218
/// or `None` on overflow (exceeding 2^63 microseconds in either direction).
219
+ #[ unstable( feature = "std_misc" ) ]
201
220
pub fn num_microseconds ( & self ) -> Option < i64 > {
202
221
let secs_part = try_opt ! ( self . num_seconds( ) . checked_mul( MICROS_PER_SEC ) ) ;
203
222
let nanos_part = self . nanos_mod_sec ( ) / NANOS_PER_MICRO ;
@@ -206,13 +225,15 @@ impl Duration {
206
225
207
226
/// Returns the total number of whole nanoseconds in the duration,
208
227
/// or `None` on overflow (exceeding 2^63 nanoseconds in either direction).
228
+ #[ unstable( feature = "std_misc" ) ]
209
229
pub fn num_nanoseconds ( & self ) -> Option < i64 > {
210
230
let secs_part = try_opt ! ( self . num_seconds( ) . checked_mul( NANOS_PER_SEC as i64 ) ) ;
211
231
let nanos_part = self . nanos_mod_sec ( ) ;
212
232
secs_part. checked_add ( nanos_part as i64 )
213
233
}
214
234
215
235
/// Add two durations, returning `None` if overflow occured.
236
+ #[ unstable( feature = "std_misc" ) ]
216
237
pub fn checked_add ( & self , rhs : & Duration ) -> Option < Duration > {
217
238
let mut secs = try_opt ! ( self . secs. checked_add( rhs. secs) ) ;
218
239
let mut nanos = self . nanos + rhs. nanos ;
@@ -227,6 +248,7 @@ impl Duration {
227
248
}
228
249
229
250
/// Subtract two durations, returning `None` if overflow occured.
251
+ #[ unstable( feature = "std_misc" ) ]
230
252
pub fn checked_sub ( & self , rhs : & Duration ) -> Option < Duration > {
231
253
let mut secs = try_opt ! ( self . secs. checked_sub( rhs. secs) ) ;
232
254
let mut nanos = self . nanos - rhs. nanos ;
@@ -242,25 +264,30 @@ impl Duration {
242
264
243
265
/// The minimum possible `Duration`: `i64::MIN` milliseconds.
244
266
#[ inline]
267
+ #[ unstable( feature = "std_misc" ) ]
245
268
pub fn min_value ( ) -> Duration { MIN }
246
269
247
270
/// The maximum possible `Duration`: `i64::MAX` milliseconds.
248
271
#[ inline]
272
+ #[ unstable( feature = "std_misc" ) ]
249
273
pub fn max_value ( ) -> Duration { MAX }
250
274
251
275
/// A duration where the stored seconds and nanoseconds are equal to zero.
252
276
#[ inline]
277
+ #[ unstable( feature = "std_misc" ) ]
253
278
pub fn zero ( ) -> Duration {
254
279
Duration { secs : 0 , nanos : 0 }
255
280
}
256
281
257
282
/// Returns `true` if the duration equals `Duration::zero()`.
258
283
#[ inline]
284
+ #[ unstable( feature = "std_misc" ) ]
259
285
pub fn is_zero ( & self ) -> bool {
260
286
self . secs == 0 && self . nanos == 0
261
287
}
262
288
}
263
289
290
+ #[ unstable( feature = "std_misc" ) ]
264
291
impl Neg for Duration {
265
292
type Output = Duration ;
266
293
@@ -274,6 +301,7 @@ impl Neg for Duration {
274
301
}
275
302
}
276
303
304
+ #[ unstable( feature = "std_misc" ) ]
277
305
impl Add for Duration {
278
306
type Output = Duration ;
279
307
@@ -288,6 +316,7 @@ impl Add for Duration {
288
316
}
289
317
}
290
318
319
+ #[ unstable( feature = "std_misc" ) ]
291
320
impl Sub for Duration {
292
321
type Output = Duration ;
293
322
@@ -302,6 +331,7 @@ impl Sub for Duration {
302
331
}
303
332
}
304
333
334
+ #[ unstable( feature = "std_misc" ) ]
305
335
impl Mul < i32 > for Duration {
306
336
type Output = Duration ;
307
337
@@ -314,6 +344,7 @@ impl Mul<i32> for Duration {
314
344
}
315
345
}
316
346
347
+ #[ unstable( feature = "std_misc" ) ]
317
348
impl Div < i32 > for Duration {
318
349
type Output = Duration ;
319
350
0 commit comments