chrono/time/
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)]
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
290impl Neg for Duration {
291    type Output = Duration;
292
293    #[inline]
294    fn neg(self) -> Duration {
295        if self.nanos == 0 {
296            Duration { secs: -self.secs, nanos: 0 }
297        } else {
298            Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
299        }
300    }
301}
302
303impl Add for Duration {
304    type Output = Duration;
305
306    fn add(self, rhs: Duration) -> Duration {
307        let mut secs = self.secs + rhs.secs;
308        let mut nanos = self.nanos + rhs.nanos;
309        if nanos >= NANOS_PER_SEC {
310            nanos -= NANOS_PER_SEC;
311            secs += 1;
312        }
313        Duration { secs: secs, nanos: nanos }
314    }
315}
316
317impl Sub for Duration {
318    type Output = Duration;
319
320    fn sub(self, rhs: Duration) -> Duration {
321        let mut secs = self.secs - rhs.secs;
322        let mut nanos = self.nanos - rhs.nanos;
323        if nanos < 0 {
324            nanos += NANOS_PER_SEC;
325            secs -= 1;
326        }
327        Duration { secs: secs, nanos: nanos }
328    }
329}
330
331impl Mul<i32> for Duration {
332    type Output = Duration;
333
334    fn mul(self, rhs: i32) -> Duration {
335        // Multiply nanoseconds as i64, because it cannot overflow that way.
336        let total_nanos = self.nanos as i64 * rhs as i64;
337        let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
338        let secs = self.secs * rhs as i64 + extra_secs;
339        Duration { secs: secs, nanos: nanos as i32 }
340    }
341}
342
343impl Div<i32> for Duration {
344    type Output = Duration;
345
346    fn div(self, rhs: i32) -> Duration {
347        let mut secs = self.secs / rhs as i64;
348        let carry = self.secs - secs * rhs as i64;
349        let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
350        let mut nanos = self.nanos / rhs + extra_nanos as i32;
351        if nanos >= NANOS_PER_SEC {
352            nanos -= NANOS_PER_SEC;
353            secs += 1;
354        }
355        if nanos < 0 {
356            nanos += NANOS_PER_SEC;
357            secs -= 1;
358        }
359        Duration { secs: secs, nanos: nanos }
360    }
361}
362
363impl fmt::Display for Duration {
364    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
365        // technically speaking, negative duration is not valid ISO 8601,
366        // but we need to print it anyway.
367        let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
368
369        let days = abs.secs / SECS_PER_DAY;
370        let secs = abs.secs - days * SECS_PER_DAY;
371        let hasdate = days != 0;
372        let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
373
374        write!(f, "{}P", sign)?;
375
376        if hasdate {
377            write!(f, "{}D", days)?;
378        }
379        if hastime {
380            if abs.nanos == 0 {
381                write!(f, "T{}S", secs)?;
382            } else if abs.nanos % NANOS_PER_MILLI == 0 {
383                write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI)?;
384            } else if abs.nanos % NANOS_PER_MICRO == 0 {
385                write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO)?;
386            } else {
387                write!(f, "T{}.{:09}S", secs, abs.nanos)?;
388            }
389        }
390        Ok(())
391    }
392}
393
394/// Represents error when converting `Duration` to/from a standard library
395/// implementation
396///
397/// The `std::time::Duration` supports a range from zero to `u64::MAX`
398/// *seconds*, while this module supports signed range of up to
399/// `i64::MAX` of *milliseconds*.
400#[derive(Debug, Clone, Copy, PartialEq, Eq)]
401pub struct OutOfRangeError(());
402
403impl fmt::Display for OutOfRangeError {
404    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
405        write!(f, "{}", self.description())
406    }
407}
408
409impl Error for OutOfRangeError {
410    fn description(&self) -> &str {
411        "Source duration value is out of range for the target type"
412    }
413}
414
415// Copied from libnum
416#[inline]
417fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
418    (div_floor_64(this, other), mod_floor_64(this, other))
419}
420
421#[inline]
422fn div_floor_64(this: i64, other: i64) -> i64 {
423    match div_rem_64(this, other) {
424        (d, r) if (r > 0 && other < 0)
425               || (r < 0 && other > 0) => d - 1,
426        (d, _)                         => d,
427    }
428}
429
430#[inline]
431fn mod_floor_64(this: i64, other: i64) -> i64 {
432    match this % other {
433        r if (r > 0 && other < 0)
434          || (r < 0 && other > 0) => r + other,
435        r                         => r,
436    }
437}
438
439#[inline]
440fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
441    (this / other, this % other)
442}
443
444#[cfg(test)]
445mod tests {
446    use super::{Duration, MIN, MAX, OutOfRangeError};
447    use std::{i32, i64};
448    use std::time::Duration as StdDuration;
449
450    #[test]
451    fn test_duration() {
452        assert!(Duration::seconds(1) != Duration::zero());
453        assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
454        assert_eq!(Duration::seconds(86399) + Duration::seconds(4),
455                   Duration::days(1) + Duration::seconds(3));
456        assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
457        assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
458        assert_eq!(Duration::days(2) + Duration::seconds(86399) +
459                   Duration::nanoseconds(1234567890),
460                   Duration::days(3) + Duration::nanoseconds(234567890));
461        assert_eq!(-Duration::days(3), Duration::days(-3));
462        assert_eq!(-(Duration::days(3) + Duration::seconds(70)),
463                   Duration::days(-4) + Duration::seconds(86400-70));
464    }
465
466    #[test]
467    fn test_duration_num_days() {
468        assert_eq!(Duration::zero().num_days(), 0);
469        assert_eq!(Duration::days(1).num_days(), 1);
470        assert_eq!(Duration::days(-1).num_days(), -1);
471        assert_eq!(Duration::seconds(86399).num_days(), 0);
472        assert_eq!(Duration::seconds(86401).num_days(), 1);
473        assert_eq!(Duration::seconds(-86399).num_days(), 0);
474        assert_eq!(Duration::seconds(-86401).num_days(), -1);
475        assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
476        assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
477    }
478
479    #[test]
480    fn test_duration_num_seconds() {
481        assert_eq!(Duration::zero().num_seconds(), 0);
482        assert_eq!(Duration::seconds(1).num_seconds(), 1);
483        assert_eq!(Duration::seconds(-1).num_seconds(), -1);
484        assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
485        assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
486        assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
487        assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
488    }
489
490    #[test]
491    fn test_duration_num_milliseconds() {
492        assert_eq!(Duration::zero().num_milliseconds(), 0);
493        assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
494        assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
495        assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
496        assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
497        assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
498        assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
499        assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
500        assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
501        assert_eq!(MAX.num_milliseconds(), i64::MAX);
502        assert_eq!(MIN.num_milliseconds(), i64::MIN);
503    }
504
505    #[test]
506    fn test_duration_num_microseconds() {
507        assert_eq!(Duration::zero().num_microseconds(), Some(0));
508        assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
509        assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
510        assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
511        assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
512        assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
513        assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
514        assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
515        assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
516        assert_eq!(MAX.num_microseconds(), None);
517        assert_eq!(MIN.num_microseconds(), None);
518
519        // overflow checks
520        const MICROS_PER_DAY: i64 = 86400_000_000;
521        assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
522                   Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY));
523        assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
524                   Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY));
525        assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
526        assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
527    }
528
529    #[test]
530    fn test_duration_num_nanoseconds() {
531        assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
532        assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
533        assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
534        assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
535        assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
536        assert_eq!(MAX.num_nanoseconds(), None);
537        assert_eq!(MIN.num_nanoseconds(), None);
538
539        // overflow checks
540        const NANOS_PER_DAY: i64 = 86400_000_000_000;
541        assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
542                   Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY));
543        assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
544                   Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY));
545        assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
546        assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
547    }
548
549    #[test]
550    fn test_duration_checked_ops() {
551        assert_eq!(Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
552                   Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999)));
553        assert!(Duration::milliseconds(i64::MAX).checked_add(&Duration::microseconds(1000))
554                                                .is_none());
555
556        assert_eq!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
557                   Some(Duration::milliseconds(i64::MIN)));
558        assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1))
559                                                .is_none());
560    }
561
562    #[test]
563    fn test_duration_mul() {
564        assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
565        assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
566        assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
567        assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
568        assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
569        assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
570        assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
571        assert_eq!(Duration::nanoseconds(30) * 333_333_333,
572                   Duration::seconds(10) - Duration::nanoseconds(10));
573        assert_eq!((Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
574                   Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3));
575        assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
576        assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
577    }
578
579    #[test]
580    fn test_duration_div() {
581        assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
582        assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
583        assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
584        assert_eq!(Duration::nanoseconds(123_456_789) / -1, -Duration::nanoseconds(123_456_789));
585        assert_eq!(-Duration::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
586        assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
587        assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
588        assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
589        assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
590        assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
591        assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
592        assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
593        assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
594    }
595
596    #[test]
597    fn test_duration_fmt() {
598        assert_eq!(Duration::zero().to_string(), "PT0S");
599        assert_eq!(Duration::days(42).to_string(), "P42D");
600        assert_eq!(Duration::days(-42).to_string(), "-P42D");
601        assert_eq!(Duration::seconds(42).to_string(), "PT42S");
602        assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
603        assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
604        assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
605        assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
606                   "P7DT6.543S");
607        assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
608        assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
609
610        // the format specifier should have no effect on `Duration`
611        assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
612                   "P1DT2.345S");
613    }
614
615    #[test]
616    fn test_to_std() {
617        assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
618        assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0)));
619        assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123000000)));
620        assert_eq!(Duration::milliseconds(123765).to_std(), Ok(StdDuration::new(123, 765000000)));
621        assert_eq!(Duration::nanoseconds(777).to_std(), Ok(StdDuration::new(0, 777)));
622        assert_eq!(MAX.to_std(), Ok(StdDuration::new(9223372036854775, 807000000)));
623        assert_eq!(Duration::seconds(-1).to_std(),
624                   Err(OutOfRangeError(())));
625        assert_eq!(Duration::milliseconds(-1).to_std(),
626                   Err(OutOfRangeError(())));
627    }
628
629    #[test]
630    fn test_from_std() {
631        assert_eq!(Ok(Duration::seconds(1)),
632                   Duration::from_std(StdDuration::new(1, 0)));
633        assert_eq!(Ok(Duration::seconds(86401)),
634                   Duration::from_std(StdDuration::new(86401, 0)));
635        assert_eq!(Ok(Duration::milliseconds(123)),
636                   Duration::from_std(StdDuration::new(0, 123000000)));
637        assert_eq!(Ok(Duration::milliseconds(123765)),
638                   Duration::from_std(StdDuration::new(123, 765000000)));
639        assert_eq!(Ok(Duration::nanoseconds(777)),
640                   Duration::from_std(StdDuration::new(0, 777)));
641        assert_eq!(Ok(MAX),
642                   Duration::from_std(StdDuration::new(9223372036854775, 807000000)));
643        assert_eq!(Duration::from_std(StdDuration::new(9223372036854776, 0)),
644                   Err(OutOfRangeError(())));
645        assert_eq!(Duration::from_std(StdDuration::new(9223372036854775, 807000001)),
646                   Err(OutOfRangeError(())));
647    }
648}