time_preview/
duration.rs

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
13use std::{fmt, i64};
14use std::error::Error;
15use std::ops::{Add, Sub, Mul, Div, Neg, FnOnce};
16use std::time::Duration as StdDuration;
17
18/// The number of nanoseconds in a microsecond.
19const NANOS_PER_MICRO: i32 = 1000;
20/// The number of nanoseconds in a millisecond.
21const NANOS_PER_MILLI: i32 = 1000_000;
22/// The number of nanoseconds in seconds.
23const NANOS_PER_SEC: i32 = 1_000_000_000;
24/// The number of microseconds per second.
25const MICROS_PER_SEC: i64 = 1000_000;
26/// The number of milliseconds per second.
27const MILLIS_PER_SEC: i64 = 1000;
28/// The number of seconds in a minute.
29const SECS_PER_MINUTE: i64 = 60;
30/// The number of seconds in an hour.
31const SECS_PER_HOUR: i64 = 3600;
32/// The number of (non-leap) seconds in days.
33const SECS_PER_DAY: i64 = 86400;
34/// The number of (non-leap) seconds in a week.
35const SECS_PER_WEEK: i64 = 604800;
36
37macro_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)]
45pub struct Duration {
46    secs: i64,
47    nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
48}
49
50/// The minimum possible `Duration`: `i64::MIN` milliseconds.
51pub 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.
57pub 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
62impl 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]
67    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]
76    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]
85    pub fn hours(hours: i64) -> Duration {
86        let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours ouf 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]
94    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]
103    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]
113    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]
121    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]
129    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.
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]
144    pub fn num_weeks(&self) -> i64 {
145        self.num_days() / 7
146    }
147
148    /// Returns the total number of whole days in the duration.
149    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]
155    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]
161    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.
166    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.
178    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,
187    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).
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).
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.
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.
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]
241    pub fn min_value() -> Duration { MIN }
242
243    /// The maximum possible `Duration`: `i64::MAX` milliseconds.
244    #[inline]
245    pub fn max_value() -> Duration { MAX }
246
247    /// A duration where the stored seconds and nanoseconds are equal to zero.
248    #[inline]
249    pub fn zero() -> Duration {
250        Duration { secs: 0, nanos: 0 }
251    }
252
253    /// Returns `true` if the duration equals `Duration::zero()`.
254    #[inline]
255    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.
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.
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")]
291    pub(crate) fn raw(&self) -> (i64, i32) {
292        (self.secs, self.nanos)
293    }
294}
295
296impl Neg for Duration {
297    type Output = Duration;
298
299    #[inline]
300    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
309impl Add for Duration {
310    type Output = Duration;
311
312    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
323impl Sub for Duration {
324    type Output = Duration;
325
326    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
337impl Mul<i32> for Duration {
338    type Output = Duration;
339
340    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
349impl Div<i32> for Duration {
350    type Output = Duration;
351
352    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
369impl fmt::Display for Duration {
370    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        try!(write!(f, "{}P", sign));
381
382        if hasdate {
383            try!(write!(f, "{}D", days));
384        }
385        if hastime {
386            if abs.nanos == 0 {
387                try!(write!(f, "T{}S", secs));
388            } else if abs.nanos % NANOS_PER_MILLI == 0 {
389                try!(write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI));
390            } else if abs.nanos % NANOS_PER_MICRO == 0 {
391                try!(write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO));
392            } else {
393                try!(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)]
407pub struct OutOfRangeError(());
408
409impl fmt::Display for OutOfRangeError {
410    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
411        write!(f, "{}", self.description())
412    }
413}
414
415impl Error for OutOfRangeError {
416    fn description(&self) -> &str {
417        "Source duration value is out of range for the target type"
418    }
419}
420
421// Copied from libnum
422#[inline]
423fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
424    (div_floor_64(this, other), mod_floor_64(this, other))
425}
426
427#[inline]
428fn div_floor_64(this: i64, other: i64) -> i64 {
429    match div_rem_64(this, other) {
430        (d, r) if (r > 0 && other < 0)
431               || (r < 0 && other > 0) => d - 1,
432        (d, _)                         => d,
433    }
434}
435
436#[inline]
437fn mod_floor_64(this: i64, other: i64) -> i64 {
438    match this % other {
439        r if (r > 0 && other < 0)
440          || (r < 0 && other > 0) => r + other,
441        r                         => r,
442    }
443}
444
445#[inline]
446fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
447    (this / other, this % other)
448}
449
450#[cfg(test)]
451mod tests {
452    use super::{Duration, MIN, MAX, OutOfRangeError};
453    use std::{i32, i64};
454    use std::time::Duration as StdDuration;
455
456    #[test]
457    fn test_duration() {
458        assert!(Duration::seconds(1) != Duration::zero());
459        assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
460        assert_eq!(Duration::seconds(86399) + Duration::seconds(4),
461                   Duration::days(1) + Duration::seconds(3));
462        assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
463        assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
464        assert_eq!(Duration::days(2) + Duration::seconds(86399) +
465                   Duration::nanoseconds(1234567890),
466                   Duration::days(3) + Duration::nanoseconds(234567890));
467        assert_eq!(-Duration::days(3), Duration::days(-3));
468        assert_eq!(-(Duration::days(3) + Duration::seconds(70)),
469                   Duration::days(-4) + Duration::seconds(86400-70));
470    }
471
472    #[test]
473    fn test_duration_num_days() {
474        assert_eq!(Duration::zero().num_days(), 0);
475        assert_eq!(Duration::days(1).num_days(), 1);
476        assert_eq!(Duration::days(-1).num_days(), -1);
477        assert_eq!(Duration::seconds(86399).num_days(), 0);
478        assert_eq!(Duration::seconds(86401).num_days(), 1);
479        assert_eq!(Duration::seconds(-86399).num_days(), 0);
480        assert_eq!(Duration::seconds(-86401).num_days(), -1);
481        assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
482        assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
483    }
484
485    #[test]
486    fn test_duration_num_seconds() {
487        assert_eq!(Duration::zero().num_seconds(), 0);
488        assert_eq!(Duration::seconds(1).num_seconds(), 1);
489        assert_eq!(Duration::seconds(-1).num_seconds(), -1);
490        assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
491        assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
492        assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
493        assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
494    }
495
496    #[test]
497    fn test_duration_num_milliseconds() {
498        assert_eq!(Duration::zero().num_milliseconds(), 0);
499        assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
500        assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
501        assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
502        assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
503        assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
504        assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
505        assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
506        assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
507        assert_eq!(MAX.num_milliseconds(), i64::MAX);
508        assert_eq!(MIN.num_milliseconds(), i64::MIN);
509    }
510
511    #[test]
512    fn test_duration_num_microseconds() {
513        assert_eq!(Duration::zero().num_microseconds(), Some(0));
514        assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
515        assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
516        assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
517        assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
518        assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
519        assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
520        assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
521        assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
522        assert_eq!(MAX.num_microseconds(), None);
523        assert_eq!(MIN.num_microseconds(), None);
524
525        // overflow checks
526        const MICROS_PER_DAY: i64 = 86400_000_000;
527        assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
528                   Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY));
529        assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
530                   Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY));
531        assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
532        assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
533    }
534
535    #[test]
536    fn test_duration_num_nanoseconds() {
537        assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
538        assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
539        assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
540        assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
541        assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
542        assert_eq!(MAX.num_nanoseconds(), None);
543        assert_eq!(MIN.num_nanoseconds(), None);
544
545        // overflow checks
546        const NANOS_PER_DAY: i64 = 86400_000_000_000;
547        assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
548                   Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY));
549        assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
550                   Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY));
551        assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
552        assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
553    }
554
555    #[test]
556    fn test_duration_checked_ops() {
557        assert_eq!(Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
558                   Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999)));
559        assert!(Duration::milliseconds(i64::MAX).checked_add(&Duration::microseconds(1000))
560                                                .is_none());
561
562        assert_eq!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
563                   Some(Duration::milliseconds(i64::MIN)));
564        assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1))
565                                                .is_none());
566    }
567
568    #[test]
569    fn test_duration_mul() {
570        assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
571        assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
572        assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
573        assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
574        assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(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(30) * 333_333_333,
578                   Duration::seconds(10) - Duration::nanoseconds(10));
579        assert_eq!((Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
580                   Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3));
581        assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
582        assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
583    }
584
585    #[test]
586    fn test_duration_div() {
587        assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
588        assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
589        assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
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::seconds(1) / 3, Duration::nanoseconds(333_333_333));
594        assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
595        assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
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(-4) / 3, Duration::nanoseconds(-1_333_333_333));
599        assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
600    }
601
602    #[test]
603    fn test_duration_fmt() {
604        assert_eq!(Duration::zero().to_string(), "PT0S");
605        assert_eq!(Duration::days(42).to_string(), "P42D");
606        assert_eq!(Duration::days(-42).to_string(), "-P42D");
607        assert_eq!(Duration::seconds(42).to_string(), "PT42S");
608        assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
609        assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
610        assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
611        assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
612                   "P7DT6.543S");
613        assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
614        assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
615
616        // the format specifier should have no effect on `Duration`
617        assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
618                   "P1DT2.345S");
619    }
620
621    #[test]
622    fn test_to_std() {
623        assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
624        assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0)));
625        assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123000000)));
626        assert_eq!(Duration::milliseconds(123765).to_std(), Ok(StdDuration::new(123, 765000000)));
627        assert_eq!(Duration::nanoseconds(777).to_std(), Ok(StdDuration::new(0, 777)));
628        assert_eq!(MAX.to_std(), Ok(StdDuration::new(9223372036854775, 807000000)));
629        assert_eq!(Duration::seconds(-1).to_std(),
630                   Err(OutOfRangeError(())));
631        assert_eq!(Duration::milliseconds(-1).to_std(),
632                   Err(OutOfRangeError(())));
633    }
634
635    #[test]
636    fn test_from_std() {
637        assert_eq!(Ok(Duration::seconds(1)),
638                   Duration::from_std(StdDuration::new(1, 0)));
639        assert_eq!(Ok(Duration::seconds(86401)),
640                   Duration::from_std(StdDuration::new(86401, 0)));
641        assert_eq!(Ok(Duration::milliseconds(123)),
642                   Duration::from_std(StdDuration::new(0, 123000000)));
643        assert_eq!(Ok(Duration::milliseconds(123765)),
644                   Duration::from_std(StdDuration::new(123, 765000000)));
645        assert_eq!(Ok(Duration::nanoseconds(777)),
646                   Duration::from_std(StdDuration::new(0, 777)));
647        assert_eq!(Ok(MAX),
648                   Duration::from_std(StdDuration::new(9223372036854775, 807000000)));
649        assert_eq!(Duration::from_std(StdDuration::new(9223372036854776, 0)),
650                   Err(OutOfRangeError(())));
651        assert_eq!(Duration::from_std(StdDuration::new(9223372036854775, 807000001)),
652                   Err(OutOfRangeError(())));
653    }
654}