1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 //! Temporal quantification
12 
13 use std::{fmt, i64};
14 use std::error::Error;
15 use std::ops::{Add, Sub, Mul, Div, Neg, FnOnce};
16 use std::time::Duration as StdDuration;
17 
18 /// The number of nanoseconds in a microsecond.
19 const NANOS_PER_MICRO: i32 = 1000;
20 /// The number of nanoseconds in a millisecond.
21 const NANOS_PER_MILLI: i32 = 1000_000;
22 /// The number of nanoseconds in seconds.
23 const NANOS_PER_SEC: i32 = 1_000_000_000;
24 /// The number of microseconds per second.
25 const MICROS_PER_SEC: i64 = 1000_000;
26 /// The number of milliseconds per second.
27 const MILLIS_PER_SEC: i64 = 1000;
28 /// The number of seconds in a minute.
29 const SECS_PER_MINUTE: i64 = 60;
30 /// The number of seconds in an hour.
31 const SECS_PER_HOUR: i64 = 3600;
32 /// The number of (non-leap) seconds in days.
33 const SECS_PER_DAY: i64 = 86400;
34 /// The number of (non-leap) seconds in a week.
35 const SECS_PER_WEEK: i64 = 604800;
36 
37 macro_rules! try_opt {
38     ($e:expr) => (match $e { Some(v) => v, None => return None })
39 }
40 
41 
42 /// ISO 8601 time duration with nanosecond precision.
43 /// This also allows for the negative duration; see individual methods for details.
44 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
45 pub struct Duration {
46     secs: i64,
47     nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
48 }
49 
50 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
51 pub const MIN: Duration = Duration {
52     secs: i64::MIN / MILLIS_PER_SEC - 1,
53     nanos: NANOS_PER_SEC + (i64::MIN % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
54 };
55 
56 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
57 pub const MAX: Duration = Duration {
58     secs: i64::MAX / MILLIS_PER_SEC,
59     nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
60 };
61 
62 impl Duration {
63     /// Makes a new `Duration` with given number of weeks.
64     /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60)` with overflow checks.
65     /// Panics when the duration is out of bounds.
66     #[inline]
weeks(weeks: i64) -> Duration67     pub fn weeks(weeks: i64) -> Duration {
68         let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
69         Duration::seconds(secs)
70     }
71 
72     /// Makes a new `Duration` with given number of days.
73     /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
74     /// Panics when the duration is out of bounds.
75     #[inline]
days(days: i64) -> Duration76     pub fn days(days: i64) -> Duration {
77         let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
78         Duration::seconds(secs)
79     }
80 
81     /// Makes a new `Duration` with given number of hours.
82     /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
83     /// Panics when the duration is out of bounds.
84     #[inline]
hours(hours: i64) -> Duration85     pub fn hours(hours: i64) -> Duration {
86         let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours out of bounds");
87         Duration::seconds(secs)
88     }
89 
90     /// Makes a new `Duration` with given number of minutes.
91     /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
92     /// Panics when the duration is out of bounds.
93     #[inline]
minutes(minutes: i64) -> Duration94     pub fn minutes(minutes: i64) -> Duration {
95         let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
96         Duration::seconds(secs)
97     }
98 
99     /// Makes a new `Duration` with given number of seconds.
100     /// Panics when the duration is more than `i64::MAX` milliseconds
101     /// or less than `i64::MIN` milliseconds.
102     #[inline]
seconds(seconds: i64) -> Duration103     pub fn seconds(seconds: i64) -> Duration {
104         let d = Duration { secs: seconds, nanos: 0 };
105         if d < MIN || d > MAX {
106             panic!("Duration::seconds out of bounds");
107         }
108         d
109     }
110 
111     /// Makes a new `Duration` with given number of milliseconds.
112     #[inline]
milliseconds(milliseconds: i64) -> Duration113     pub fn milliseconds(milliseconds: i64) -> Duration {
114         let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
115         let nanos = millis as i32 * NANOS_PER_MILLI;
116         Duration { secs: secs, nanos: nanos }
117     }
118 
119     /// Makes a new `Duration` with given number of microseconds.
120     #[inline]
microseconds(microseconds: i64) -> Duration121     pub fn microseconds(microseconds: i64) -> Duration {
122         let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
123         let nanos = micros as i32 * NANOS_PER_MICRO;
124         Duration { secs: secs, nanos: nanos }
125     }
126 
127     /// Makes a new `Duration` with given number of nanoseconds.
128     #[inline]
nanoseconds(nanos: i64) -> Duration129     pub fn nanoseconds(nanos: i64) -> Duration {
130         let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
131         Duration { secs: secs, nanos: nanos as i32 }
132     }
133 
134     /// Runs a closure, returning the duration of time it took to run the
135     /// closure.
span<F>(f: F) -> Duration where F: FnOnce()136     pub fn span<F>(f: F) -> Duration where F: FnOnce() {
137         let before = super::precise_time_ns();
138         f();
139         Duration::nanoseconds((super::precise_time_ns() - before) as i64)
140     }
141 
142     /// Returns the total number of whole weeks in the duration.
143     #[inline]
num_weeks(&self) -> i64144     pub fn num_weeks(&self) -> i64 {
145         self.num_days() / 7
146     }
147 
148     /// Returns the total number of whole days in the duration.
num_days(&self) -> i64149     pub fn num_days(&self) -> i64 {
150         self.num_seconds() / SECS_PER_DAY
151     }
152 
153     /// Returns the total number of whole hours in the duration.
154     #[inline]
num_hours(&self) -> i64155     pub fn num_hours(&self) -> i64 {
156         self.num_seconds() / SECS_PER_HOUR
157     }
158 
159     /// Returns the total number of whole minutes in the duration.
160     #[inline]
num_minutes(&self) -> i64161     pub fn num_minutes(&self) -> i64 {
162         self.num_seconds() / SECS_PER_MINUTE
163     }
164 
165     /// Returns the total number of whole seconds in the duration.
num_seconds(&self) -> i64166     pub fn num_seconds(&self) -> i64 {
167         // If secs is negative, nanos should be subtracted from the duration.
168         if self.secs < 0 && self.nanos > 0 {
169             self.secs + 1
170         } else {
171             self.secs
172         }
173     }
174 
175     /// Returns the number of nanoseconds such that
176     /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of
177     /// nanoseconds in the duration.
nanos_mod_sec(&self) -> i32178     fn nanos_mod_sec(&self) -> i32 {
179         if self.secs < 0 && self.nanos > 0 {
180             self.nanos - NANOS_PER_SEC
181         } else {
182             self.nanos
183         }
184     }
185 
186     /// Returns the total number of whole milliseconds in the duration,
num_milliseconds(&self) -> i64187     pub fn num_milliseconds(&self) -> i64 {
188         // A proper Duration will not overflow, because MIN and MAX are defined
189         // such that the range is exactly i64 milliseconds.
190         let secs_part = self.num_seconds() * MILLIS_PER_SEC;
191         let nanos_part = self.nanos_mod_sec() / NANOS_PER_MILLI;
192         secs_part + nanos_part as i64
193     }
194 
195     /// Returns the total number of whole microseconds in the duration,
196     /// or `None` on overflow (exceeding 2<sup>63</sup> microseconds in either direction).
num_microseconds(&self) -> Option<i64>197     pub fn num_microseconds(&self) -> Option<i64> {
198         let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
199         let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO;
200         secs_part.checked_add(nanos_part as i64)
201     }
202 
203     /// Returns the total number of whole nanoseconds in the duration,
204     /// or `None` on overflow (exceeding 2<sup>63</sup> nanoseconds in either direction).
num_nanoseconds(&self) -> Option<i64>205     pub fn num_nanoseconds(&self) -> Option<i64> {
206         let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
207         let nanos_part = self.nanos_mod_sec();
208         secs_part.checked_add(nanos_part as i64)
209     }
210 
211     /// Add two durations, returning `None` if overflow occurred.
checked_add(&self, rhs: &Duration) -> Option<Duration>212     pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
213         let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
214         let mut nanos = self.nanos + rhs.nanos;
215         if nanos >= NANOS_PER_SEC {
216             nanos -= NANOS_PER_SEC;
217             secs = try_opt!(secs.checked_add(1));
218         }
219         let d = Duration { secs: secs, nanos: nanos };
220         // Even if d is within the bounds of i64 seconds,
221         // it might still overflow i64 milliseconds.
222         if d < MIN || d > MAX { None } else { Some(d) }
223     }
224 
225     /// Subtract two durations, returning `None` if overflow occurred.
checked_sub(&self, rhs: &Duration) -> Option<Duration>226     pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
227         let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
228         let mut nanos = self.nanos - rhs.nanos;
229         if nanos < 0 {
230             nanos += NANOS_PER_SEC;
231             secs = try_opt!(secs.checked_sub(1));
232         }
233         let d = Duration { secs: secs, nanos: nanos };
234         // Even if d is within the bounds of i64 seconds,
235         // it might still overflow i64 milliseconds.
236         if d < MIN || d > MAX { None } else { Some(d) }
237     }
238 
239     /// The minimum possible `Duration`: `i64::MIN` milliseconds.
240     #[inline]
min_value() -> Duration241     pub fn min_value() -> Duration { MIN }
242 
243     /// The maximum possible `Duration`: `i64::MAX` milliseconds.
244     #[inline]
max_value() -> Duration245     pub fn max_value() -> Duration { MAX }
246 
247     /// A duration where the stored seconds and nanoseconds are equal to zero.
248     #[inline]
zero() -> Duration249     pub fn zero() -> Duration {
250         Duration { secs: 0, nanos: 0 }
251     }
252 
253     /// Returns `true` if the duration equals `Duration::zero()`.
254     #[inline]
is_zero(&self) -> bool255     pub fn is_zero(&self) -> bool {
256         self.secs == 0 && self.nanos == 0
257     }
258 
259     /// Creates a `time::Duration` object from `std::time::Duration`
260     ///
261     /// This function errors when original duration is larger than the maximum
262     /// value supported for this type.
from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError>263     pub fn from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError> {
264         // We need to check secs as u64 before coercing to i64
265         if duration.as_secs() > MAX.secs as u64 {
266             return Err(OutOfRangeError(()));
267         }
268         let d = Duration {
269             secs: duration.as_secs() as i64,
270             nanos: duration.subsec_nanos() as i32,
271         };
272         if d > MAX {
273             return Err(OutOfRangeError(()));
274         }
275         Ok(d)
276     }
277 
278     /// Creates a `std::time::Duration` object from `time::Duration`
279     ///
280     /// This function errors when duration is less than zero. As standard
281     /// library implementation is limited to non-negative values.
to_std(&self) -> Result<StdDuration, OutOfRangeError>282     pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
283         if self.secs < 0 {
284             return Err(OutOfRangeError(()));
285         }
286         Ok(StdDuration::new(self.secs as u64, self.nanos as u32))
287     }
288 
289     /// Returns the raw value of duration.
290     #[cfg(target_env = "sgx")]
raw(&self) -> (i64, i32)291     pub(crate) fn raw(&self) -> (i64, i32) {
292         (self.secs, self.nanos)
293     }
294 }
295 
296 impl Neg for Duration {
297     type Output = Duration;
298 
299     #[inline]
neg(self) -> Duration300     fn neg(self) -> Duration {
301         if self.nanos == 0 {
302             Duration { secs: -self.secs, nanos: 0 }
303         } else {
304             Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
305         }
306     }
307 }
308 
309 impl Add for Duration {
310     type Output = Duration;
311 
add(self, rhs: Duration) -> Duration312     fn add(self, rhs: Duration) -> Duration {
313         let mut secs = self.secs + rhs.secs;
314         let mut nanos = self.nanos + rhs.nanos;
315         if nanos >= NANOS_PER_SEC {
316             nanos -= NANOS_PER_SEC;
317             secs += 1;
318         }
319         Duration { secs: secs, nanos: nanos }
320     }
321 }
322 
323 impl Sub for Duration {
324     type Output = Duration;
325 
sub(self, rhs: Duration) -> Duration326     fn sub(self, rhs: Duration) -> Duration {
327         let mut secs = self.secs - rhs.secs;
328         let mut nanos = self.nanos - rhs.nanos;
329         if nanos < 0 {
330             nanos += NANOS_PER_SEC;
331             secs -= 1;
332         }
333         Duration { secs: secs, nanos: nanos }
334     }
335 }
336 
337 impl Mul<i32> for Duration {
338     type Output = Duration;
339 
mul(self, rhs: i32) -> Duration340     fn mul(self, rhs: i32) -> Duration {
341         // Multiply nanoseconds as i64, because it cannot overflow that way.
342         let total_nanos = self.nanos as i64 * rhs as i64;
343         let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
344         let secs = self.secs * rhs as i64 + extra_secs;
345         Duration { secs: secs, nanos: nanos as i32 }
346     }
347 }
348 
349 impl Div<i32> for Duration {
350     type Output = Duration;
351 
div(self, rhs: i32) -> Duration352     fn div(self, rhs: i32) -> Duration {
353         let mut secs = self.secs / rhs as i64;
354         let carry = self.secs - secs * rhs as i64;
355         let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
356         let mut nanos = self.nanos / rhs + extra_nanos as i32;
357         if nanos >= NANOS_PER_SEC {
358             nanos -= NANOS_PER_SEC;
359             secs += 1;
360         }
361         if nanos < 0 {
362             nanos += NANOS_PER_SEC;
363             secs -= 1;
364         }
365         Duration { secs: secs, nanos: nanos }
366     }
367 }
368 
369 impl fmt::Display for Duration {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result370     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
371         // technically speaking, negative duration is not valid ISO 8601,
372         // but we need to print it anyway.
373         let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
374 
375         let days = abs.secs / SECS_PER_DAY;
376         let secs = abs.secs - days * SECS_PER_DAY;
377         let hasdate = days != 0;
378         let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
379 
380         write!(f, "{}P", sign)?;
381 
382         if hasdate {
383             write!(f, "{}D", days)?;
384         }
385         if hastime {
386             if abs.nanos == 0 {
387                 write!(f, "T{}S", secs)?;
388             } else if abs.nanos % NANOS_PER_MILLI == 0 {
389                 write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI)?;
390             } else if abs.nanos % NANOS_PER_MICRO == 0 {
391                 write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO)?;
392             } else {
393                 write!(f, "T{}.{:09}S", secs, abs.nanos)?;
394             }
395         }
396         Ok(())
397     }
398 }
399 
400 /// Represents error when converting `Duration` to/from a standard library
401 /// implementation
402 ///
403 /// The `std::time::Duration` supports a range from zero to `u64::MAX`
404 /// *seconds*, while this module supports signed range of up to
405 /// `i64::MAX` of *milliseconds*.
406 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
407 pub struct OutOfRangeError(());
408 
409 impl fmt::Display for OutOfRangeError {
410     #[allow(deprecated)]
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result411     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
412         write!(f, "{}", self.description())
413     }
414 }
415 
416 impl Error for OutOfRangeError {
description(&self) -> &str417     fn description(&self) -> &str {
418         "Source duration value is out of range for the target type"
419     }
420 }
421 
422 // Copied from libnum
423 #[inline]
div_mod_floor_64(this: i64, other: i64) -> (i64, i64)424 fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
425     (div_floor_64(this, other), mod_floor_64(this, other))
426 }
427 
428 #[inline]
div_floor_64(this: i64, other: i64) -> i64429 fn div_floor_64(this: i64, other: i64) -> i64 {
430     match div_rem_64(this, other) {
431         (d, r) if (r > 0 && other < 0)
432                || (r < 0 && other > 0) => d - 1,
433         (d, _)                         => d,
434     }
435 }
436 
437 #[inline]
mod_floor_64(this: i64, other: i64) -> i64438 fn mod_floor_64(this: i64, other: i64) -> i64 {
439     match this % other {
440         r if (r > 0 && other < 0)
441           || (r < 0 && other > 0) => r + other,
442         r                         => r,
443     }
444 }
445 
446 #[inline]
div_rem_64(this: i64, other: i64) -> (i64, i64)447 fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
448     (this / other, this % other)
449 }
450 
451 #[cfg(test)]
452 mod tests {
453     use super::{Duration, MIN, MAX, OutOfRangeError};
454     use std::{i32, i64};
455     use std::time::Duration as StdDuration;
456 
457     #[test]
test_duration()458     fn test_duration() {
459         assert!(Duration::seconds(1) != Duration::zero());
460         assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
461         assert_eq!(Duration::seconds(86399) + Duration::seconds(4),
462                    Duration::days(1) + Duration::seconds(3));
463         assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
464         assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
465         assert_eq!(Duration::days(2) + Duration::seconds(86399) +
466                    Duration::nanoseconds(1234567890),
467                    Duration::days(3) + Duration::nanoseconds(234567890));
468         assert_eq!(-Duration::days(3), Duration::days(-3));
469         assert_eq!(-(Duration::days(3) + Duration::seconds(70)),
470                    Duration::days(-4) + Duration::seconds(86400-70));
471     }
472 
473     #[test]
test_duration_num_days()474     fn test_duration_num_days() {
475         assert_eq!(Duration::zero().num_days(), 0);
476         assert_eq!(Duration::days(1).num_days(), 1);
477         assert_eq!(Duration::days(-1).num_days(), -1);
478         assert_eq!(Duration::seconds(86399).num_days(), 0);
479         assert_eq!(Duration::seconds(86401).num_days(), 1);
480         assert_eq!(Duration::seconds(-86399).num_days(), 0);
481         assert_eq!(Duration::seconds(-86401).num_days(), -1);
482         assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
483         assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
484     }
485 
486     #[test]
test_duration_num_seconds()487     fn test_duration_num_seconds() {
488         assert_eq!(Duration::zero().num_seconds(), 0);
489         assert_eq!(Duration::seconds(1).num_seconds(), 1);
490         assert_eq!(Duration::seconds(-1).num_seconds(), -1);
491         assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
492         assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
493         assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
494         assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
495     }
496 
497     #[test]
test_duration_num_milliseconds()498     fn test_duration_num_milliseconds() {
499         assert_eq!(Duration::zero().num_milliseconds(), 0);
500         assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
501         assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
502         assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
503         assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
504         assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
505         assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
506         assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
507         assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
508         assert_eq!(MAX.num_milliseconds(), i64::MAX);
509         assert_eq!(MIN.num_milliseconds(), i64::MIN);
510     }
511 
512     #[test]
test_duration_num_microseconds()513     fn test_duration_num_microseconds() {
514         assert_eq!(Duration::zero().num_microseconds(), Some(0));
515         assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
516         assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
517         assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
518         assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
519         assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
520         assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
521         assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
522         assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
523         assert_eq!(MAX.num_microseconds(), None);
524         assert_eq!(MIN.num_microseconds(), None);
525 
526         // overflow checks
527         const MICROS_PER_DAY: i64 = 86400_000_000;
528         assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
529                    Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY));
530         assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
531                    Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY));
532         assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
533         assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
534     }
535 
536     #[test]
test_duration_num_nanoseconds()537     fn test_duration_num_nanoseconds() {
538         assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
539         assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
540         assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
541         assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
542         assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
543         assert_eq!(MAX.num_nanoseconds(), None);
544         assert_eq!(MIN.num_nanoseconds(), None);
545 
546         // overflow checks
547         const NANOS_PER_DAY: i64 = 86400_000_000_000;
548         assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
549                    Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY));
550         assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
551                    Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY));
552         assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
553         assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
554     }
555 
556     #[test]
test_duration_checked_ops()557     fn test_duration_checked_ops() {
558         assert_eq!(Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
559                    Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999)));
560         assert!(Duration::milliseconds(i64::MAX).checked_add(&Duration::microseconds(1000))
561                                                 .is_none());
562 
563         assert_eq!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
564                    Some(Duration::milliseconds(i64::MIN)));
565         assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1))
566                                                 .is_none());
567     }
568 
569     #[test]
test_duration_mul()570     fn test_duration_mul() {
571         assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
572         assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
573         assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
574         assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
575         assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
576         assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
577         assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
578         assert_eq!(Duration::nanoseconds(30) * 333_333_333,
579                    Duration::seconds(10) - Duration::nanoseconds(10));
580         assert_eq!((Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
581                    Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3));
582         assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
583         assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
584     }
585 
586     #[test]
test_duration_div()587     fn test_duration_div() {
588         assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
589         assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
590         assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
591         assert_eq!(Duration::nanoseconds(123_456_789) / -1, -Duration::nanoseconds(123_456_789));
592         assert_eq!(-Duration::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
593         assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
594         assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
595         assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
596         assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
597         assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
598         assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
599         assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
600         assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
601     }
602 
603     #[test]
test_duration_fmt()604     fn test_duration_fmt() {
605         assert_eq!(Duration::zero().to_string(), "PT0S");
606         assert_eq!(Duration::days(42).to_string(), "P42D");
607         assert_eq!(Duration::days(-42).to_string(), "-P42D");
608         assert_eq!(Duration::seconds(42).to_string(), "PT42S");
609         assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
610         assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
611         assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
612         assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
613                    "P7DT6.543S");
614         assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
615         assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
616 
617         // the format specifier should have no effect on `Duration`
618         assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
619                    "P1DT2.345S");
620     }
621 
622     #[test]
test_to_std()623     fn test_to_std() {
624         assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
625         assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0)));
626         assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123000000)));
627         assert_eq!(Duration::milliseconds(123765).to_std(), Ok(StdDuration::new(123, 765000000)));
628         assert_eq!(Duration::nanoseconds(777).to_std(), Ok(StdDuration::new(0, 777)));
629         assert_eq!(MAX.to_std(), Ok(StdDuration::new(9223372036854775, 807000000)));
630         assert_eq!(Duration::seconds(-1).to_std(),
631                    Err(OutOfRangeError(())));
632         assert_eq!(Duration::milliseconds(-1).to_std(),
633                    Err(OutOfRangeError(())));
634     }
635 
636     #[test]
test_from_std()637     fn test_from_std() {
638         assert_eq!(Ok(Duration::seconds(1)),
639                    Duration::from_std(StdDuration::new(1, 0)));
640         assert_eq!(Ok(Duration::seconds(86401)),
641                    Duration::from_std(StdDuration::new(86401, 0)));
642         assert_eq!(Ok(Duration::milliseconds(123)),
643                    Duration::from_std(StdDuration::new(0, 123000000)));
644         assert_eq!(Ok(Duration::milliseconds(123765)),
645                    Duration::from_std(StdDuration::new(123, 765000000)));
646         assert_eq!(Ok(Duration::nanoseconds(777)),
647                    Duration::from_std(StdDuration::new(0, 777)));
648         assert_eq!(Ok(MAX),
649                    Duration::from_std(StdDuration::new(9223372036854775, 807000000)));
650         assert_eq!(Duration::from_std(StdDuration::new(9223372036854776, 0)),
651                    Err(OutOfRangeError(())));
652         assert_eq!(Duration::from_std(StdDuration::new(9223372036854775, 807000001)),
653                    Err(OutOfRangeError(())));
654     }
655 }
656