]> git.lizzy.rs Git - rust.git/blob - src/libstd/time/duration.rs
debuginfo: Make debuginfo source location assignment more stable (Pt. 1)
[rust.git] / src / libstd / 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
13 #![unstable]
14
15 use {fmt, i64};
16 use ops::{Add, Sub, Mul, Div, Neg, FnOnce};
17 use option::Option;
18 use option::Option::{Some, None};
19 use num::Int;
20 use result::Result::Ok;
21
22 /// The number of nanoseconds in a microsecond.
23 const NANOS_PER_MICRO: i32 = 1000;
24 /// The number of nanoseconds in a millisecond.
25 const NANOS_PER_MILLI: i32 = 1000_000;
26 /// The number of nanoseconds in seconds.
27 const NANOS_PER_SEC: i32 = 1_000_000_000;
28 /// The number of microseconds per second.
29 const MICROS_PER_SEC: i64 = 1000_000;
30 /// The number of milliseconds per second.
31 const MILLIS_PER_SEC: i64 = 1000;
32 /// The number of seconds in a minute.
33 const SECS_PER_MINUTE: i64 = 60;
34 /// The number of seconds in an hour.
35 const SECS_PER_HOUR: i64 = 3600;
36 /// The number of (non-leap) seconds in days.
37 const SECS_PER_DAY: i64 = 86400;
38 /// The number of (non-leap) seconds in a week.
39 const SECS_PER_WEEK: i64 = 604800;
40
41 macro_rules! try_opt {
42     ($e:expr) => (match $e { Some(v) => v, None => return None })
43 }
44
45
46 /// ISO 8601 time duration with nanosecond precision.
47 /// This also allows for the negative duration; see individual methods for details.
48 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Show)]
49 pub struct Duration {
50     secs: i64,
51     nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
52 }
53
54 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
55 pub const MIN: Duration = Duration {
56     secs: i64::MIN / MILLIS_PER_SEC - 1,
57     nanos: NANOS_PER_SEC + (i64::MIN % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
58 };
59
60 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
61 pub const MAX: Duration = Duration {
62     secs: i64::MAX / MILLIS_PER_SEC,
63     nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
64 };
65
66 impl Duration {
67     /// Makes a new `Duration` with given number of weeks.
68     /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60), with overflow checks.
69     /// Panics when the duration is out of bounds.
70     #[inline]
71     pub fn weeks(weeks: i64) -> Duration {
72         let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
73         Duration::seconds(secs)
74     }
75
76     /// Makes a new `Duration` with given number of days.
77     /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
78     /// Panics when the duration is out of bounds.
79     #[inline]
80     pub fn days(days: i64) -> Duration {
81         let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
82         Duration::seconds(secs)
83     }
84
85     /// Makes a new `Duration` with given number of hours.
86     /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
87     /// Panics when the duration is out of bounds.
88     #[inline]
89     pub fn hours(hours: i64) -> Duration {
90         let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours ouf of bounds");
91         Duration::seconds(secs)
92     }
93
94     /// Makes a new `Duration` with given number of minutes.
95     /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
96     /// Panics when the duration is out of bounds.
97     #[inline]
98     pub fn minutes(minutes: i64) -> Duration {
99         let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
100         Duration::seconds(secs)
101     }
102
103     /// Makes a new `Duration` with given number of seconds.
104     /// Panics when the duration is more than `i64::MAX` milliseconds
105     /// or less than `i64::MIN` milliseconds.
106     #[inline]
107     pub fn seconds(seconds: i64) -> Duration {
108         let d = Duration { secs: seconds, nanos: 0 };
109         if d < MIN || d > MAX {
110             panic!("Duration::seconds out of bounds");
111         }
112         d
113     }
114
115     /// Makes a new `Duration` with given number of milliseconds.
116     #[inline]
117     pub fn milliseconds(milliseconds: i64) -> Duration {
118         let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
119         let nanos = millis as i32 * NANOS_PER_MILLI;
120         Duration { secs: secs, nanos: nanos }
121     }
122
123     /// Makes a new `Duration` with given number of microseconds.
124     #[inline]
125     pub fn microseconds(microseconds: i64) -> Duration {
126         let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
127         let nanos = micros as i32 * NANOS_PER_MICRO;
128         Duration { secs: secs, nanos: nanos }
129     }
130
131     /// Makes a new `Duration` with given number of nanoseconds.
132     #[inline]
133     pub fn nanoseconds(nanos: i64) -> Duration {
134         let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
135         Duration { secs: secs, nanos: nanos as i32 }
136     }
137
138     /// Runs a closure, returning the duration of time it took to run the
139     /// closure.
140     pub fn span<F>(f: F) -> Duration where F: FnOnce() {
141         let before = super::precise_time_ns();
142         f();
143         Duration::nanoseconds((super::precise_time_ns() - before) as i64)
144     }
145
146     /// Returns the total number of whole weeks in the duration.
147     #[inline]
148     pub fn num_weeks(&self) -> i64 {
149         self.num_days() / 7
150     }
151
152     /// Returns the total number of whole days in the duration.
153     pub fn num_days(&self) -> i64 {
154         self.num_seconds() / SECS_PER_DAY
155     }
156
157     /// Returns the total number of whole hours in the duration.
158     #[inline]
159     pub fn num_hours(&self) -> i64 {
160         self.num_seconds() / SECS_PER_HOUR
161     }
162
163     /// Returns the total number of whole minutes in the duration.
164     #[inline]
165     pub fn num_minutes(&self) -> i64 {
166         self.num_seconds() / SECS_PER_MINUTE
167     }
168
169     /// Returns the total number of whole seconds in the duration.
170     pub fn num_seconds(&self) -> i64 {
171         // If secs is negative, nanos should be subtracted from the duration.
172         if self.secs < 0 && self.nanos > 0 {
173             self.secs + 1
174         } else {
175             self.secs
176         }
177     }
178
179     /// Returns the number of nanoseconds such that
180     /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of
181     /// nanoseconds in the duration.
182     fn nanos_mod_sec(&self) -> i32 {
183         if self.secs < 0 && self.nanos > 0 {
184             self.nanos - NANOS_PER_SEC
185         } else {
186             self.nanos
187         }
188     }
189
190     /// Returns the total number of whole milliseconds in the duration,
191     pub fn num_milliseconds(&self) -> i64 {
192         // A proper Duration will not overflow, because MIN and MAX are defined
193         // such that the range is exactly i64 milliseconds.
194         let secs_part = self.num_seconds() * MILLIS_PER_SEC;
195         let nanos_part = self.nanos_mod_sec() / NANOS_PER_MILLI;
196         secs_part + nanos_part as i64
197     }
198
199     /// Returns the total number of whole microseconds in the duration,
200     /// or `None` on overflow (exceeding 2^63 microseconds in either direction).
201     pub fn num_microseconds(&self) -> Option<i64> {
202         let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
203         let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO;
204         secs_part.checked_add(nanos_part as i64)
205     }
206
207     /// Returns the total number of whole nanoseconds in the duration,
208     /// or `None` on overflow (exceeding 2^63 nanoseconds in either direction).
209     pub fn num_nanoseconds(&self) -> Option<i64> {
210         let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
211         let nanos_part = self.nanos_mod_sec();
212         secs_part.checked_add(nanos_part as i64)
213     }
214
215     /// Add two durations, returning `None` if overflow occured.
216     pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
217         let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
218         let mut nanos = self.nanos + rhs.nanos;
219         if nanos >= NANOS_PER_SEC {
220             nanos -= NANOS_PER_SEC;
221             secs = try_opt!(secs.checked_add(1));
222         }
223         let d = Duration { secs: secs, nanos: nanos };
224         // Even if d is within the bounds of i64 seconds,
225         // it might still overflow i64 milliseconds.
226         if d < MIN || d > MAX { None } else { Some(d) }
227     }
228
229     /// Subtract two durations, returning `None` if overflow occured.
230     pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
231         let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
232         let mut nanos = self.nanos - rhs.nanos;
233         if nanos < 0 {
234             nanos += NANOS_PER_SEC;
235             secs = try_opt!(secs.checked_sub(1));
236         }
237         let d = Duration { secs: secs, nanos: nanos };
238         // Even if d is within the bounds of i64 seconds,
239         // it might still overflow i64 milliseconds.
240         if d < MIN || d > MAX { None } else { Some(d) }
241     }
242
243     /// The minimum possible `Duration`: `i64::MIN` milliseconds.
244     #[inline]
245     pub fn min_value() -> Duration { MIN }
246
247     /// The maximum possible `Duration`: `i64::MAX` milliseconds.
248     #[inline]
249     pub fn max_value() -> Duration { MAX }
250
251     /// A duration where the stored seconds and nanoseconds are equal to zero.
252     #[inline]
253     pub fn zero() -> Duration {
254         Duration { secs: 0, nanos: 0 }
255     }
256
257     /// Returns `true` if the duration equals `Duration::zero()`.
258     #[inline]
259     pub fn is_zero(&self) -> bool {
260         self.secs == 0 && self.nanos == 0
261     }
262 }
263
264 impl Neg for Duration {
265     type Output = Duration;
266
267     #[inline]
268     fn neg(self) -> Duration {
269         if self.nanos == 0 {
270             Duration { secs: -self.secs, nanos: 0 }
271         } else {
272             Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
273         }
274     }
275 }
276
277 impl Add for Duration {
278     type Output = Duration;
279
280     fn add(self, rhs: Duration) -> Duration {
281         let mut secs = self.secs + rhs.secs;
282         let mut nanos = self.nanos + rhs.nanos;
283         if nanos >= NANOS_PER_SEC {
284             nanos -= NANOS_PER_SEC;
285             secs += 1;
286         }
287         Duration { secs: secs, nanos: nanos }
288     }
289 }
290
291 impl Sub for Duration {
292     type Output = Duration;
293
294     fn sub(self, rhs: Duration) -> Duration {
295         let mut secs = self.secs - rhs.secs;
296         let mut nanos = self.nanos - rhs.nanos;
297         if nanos < 0 {
298             nanos += NANOS_PER_SEC;
299             secs -= 1;
300         }
301         Duration { secs: secs, nanos: nanos }
302     }
303 }
304
305 impl Mul<i32> for Duration {
306     type Output = Duration;
307
308     fn mul(self, rhs: i32) -> Duration {
309         // Multiply nanoseconds as i64, because it cannot overflow that way.
310         let total_nanos = self.nanos as i64 * rhs as i64;
311         let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
312         let secs = self.secs * rhs as i64 + extra_secs;
313         Duration { secs: secs, nanos: nanos as i32 }
314     }
315 }
316
317 impl Div<i32> for Duration {
318     type Output = Duration;
319
320     fn div(self, rhs: i32) -> Duration {
321         let mut secs = self.secs / rhs as i64;
322         let carry = self.secs - secs * rhs as i64;
323         let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
324         let mut nanos = self.nanos / rhs + extra_nanos as i32;
325         if nanos >= NANOS_PER_SEC {
326             nanos -= NANOS_PER_SEC;
327             secs += 1;
328         }
329         if nanos < 0 {
330             nanos += NANOS_PER_SEC;
331             secs -= 1;
332         }
333         Duration { secs: secs, nanos: nanos }
334     }
335 }
336
337 impl fmt::String for Duration {
338     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
339         // technically speaking, negative duration is not valid ISO 8601,
340         // but we need to print it anyway.
341         let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
342
343         let days = abs.secs / SECS_PER_DAY;
344         let secs = abs.secs - days * SECS_PER_DAY;
345         let hasdate = days != 0;
346         let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
347
348         try!(write!(f, "{}P", sign));
349
350         if hasdate {
351             try!(write!(f, "{}D", days));
352         }
353         if hastime {
354             if abs.nanos == 0 {
355                 try!(write!(f, "T{}S", secs));
356             } else if abs.nanos % NANOS_PER_MILLI == 0 {
357                 try!(write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI));
358             } else if abs.nanos % NANOS_PER_MICRO == 0 {
359                 try!(write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO));
360             } else {
361                 try!(write!(f, "T{}.{:09}S", secs, abs.nanos));
362             }
363         }
364         Ok(())
365     }
366 }
367
368 // Copied from libnum
369 #[inline]
370 fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
371     (div_floor_64(this, other), mod_floor_64(this, other))
372 }
373
374 #[inline]
375 fn div_floor_64(this: i64, other: i64) -> i64 {
376     match div_rem_64(this, other) {
377         (d, r) if (r > 0 && other < 0)
378                || (r < 0 && other > 0) => d - 1,
379         (d, _)                         => d,
380     }
381 }
382
383 #[inline]
384 fn mod_floor_64(this: i64, other: i64) -> i64 {
385     match this % other {
386         r if (r > 0 && other < 0)
387           || (r < 0 && other > 0) => r + other,
388         r                         => r,
389     }
390 }
391
392 #[inline]
393 fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
394     (this / other, this % other)
395 }
396
397 #[cfg(test)]
398 mod tests {
399     use super::{Duration, MIN, MAX};
400     use {i32, i64};
401     use option::Option::{Some, None};
402     use string::ToString;
403
404     #[test]
405     fn test_duration() {
406         assert!(Duration::seconds(1) != Duration::zero());
407         assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
408         assert_eq!(Duration::seconds(86399) + Duration::seconds(4),
409                    Duration::days(1) + Duration::seconds(3));
410         assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
411         assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
412         assert_eq!(Duration::days(2) + Duration::seconds(86399) +
413                    Duration::nanoseconds(1234567890),
414                    Duration::days(3) + Duration::nanoseconds(234567890));
415         assert_eq!(-Duration::days(3), Duration::days(-3));
416         assert_eq!(-(Duration::days(3) + Duration::seconds(70)),
417                    Duration::days(-4) + Duration::seconds(86400-70));
418     }
419
420     #[test]
421     fn test_duration_num_days() {
422         assert_eq!(Duration::zero().num_days(), 0);
423         assert_eq!(Duration::days(1).num_days(), 1);
424         assert_eq!(Duration::days(-1).num_days(), -1);
425         assert_eq!(Duration::seconds(86399).num_days(), 0);
426         assert_eq!(Duration::seconds(86401).num_days(), 1);
427         assert_eq!(Duration::seconds(-86399).num_days(), 0);
428         assert_eq!(Duration::seconds(-86401).num_days(), -1);
429         assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
430         assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
431     }
432
433     #[test]
434     fn test_duration_num_seconds() {
435         assert_eq!(Duration::zero().num_seconds(), 0);
436         assert_eq!(Duration::seconds(1).num_seconds(), 1);
437         assert_eq!(Duration::seconds(-1).num_seconds(), -1);
438         assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
439         assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
440         assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
441         assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
442     }
443
444     #[test]
445     fn test_duration_num_milliseconds() {
446         assert_eq!(Duration::zero().num_milliseconds(), 0);
447         assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
448         assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
449         assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
450         assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
451         assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
452         assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
453         assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
454         assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
455         assert_eq!(MAX.num_milliseconds(), i64::MAX);
456         assert_eq!(MIN.num_milliseconds(), i64::MIN);
457     }
458
459     #[test]
460     fn test_duration_num_microseconds() {
461         assert_eq!(Duration::zero().num_microseconds(), Some(0));
462         assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
463         assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
464         assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
465         assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
466         assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
467         assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
468         assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
469         assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
470         assert_eq!(MAX.num_microseconds(), None);
471         assert_eq!(MIN.num_microseconds(), None);
472
473         // overflow checks
474         const MICROS_PER_DAY: i64 = 86400_000_000;
475         assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
476                    Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY));
477         assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
478                    Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY));
479         assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
480         assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
481     }
482
483     #[test]
484     fn test_duration_num_nanoseconds() {
485         assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
486         assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
487         assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
488         assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
489         assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
490         assert_eq!(MAX.num_nanoseconds(), None);
491         assert_eq!(MIN.num_nanoseconds(), None);
492
493         // overflow checks
494         const NANOS_PER_DAY: i64 = 86400_000_000_000;
495         assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
496                    Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY));
497         assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
498                    Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY));
499         assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
500         assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
501     }
502
503     #[test]
504     fn test_duration_checked_ops() {
505         assert_eq!(Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
506                    Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999)));
507         assert!(Duration::milliseconds(i64::MAX).checked_add(&Duration::microseconds(1000))
508                                                 .is_none());
509
510         assert_eq!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
511                    Some(Duration::milliseconds(i64::MIN)));
512         assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1))
513                                                 .is_none());
514     }
515
516     #[test]
517     fn test_duration_mul() {
518         assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
519         assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
520         assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
521         assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
522         assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
523         assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
524         assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
525         assert_eq!(Duration::nanoseconds(30) * 333_333_333,
526                    Duration::seconds(10) - Duration::nanoseconds(10));
527         assert_eq!((Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
528                    Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3));
529         assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
530         assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
531     }
532
533     #[test]
534     fn test_duration_div() {
535         assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
536         assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
537         assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
538         assert_eq!(Duration::nanoseconds(123_456_789) / -1, -Duration::nanoseconds(123_456_789));
539         assert_eq!(-Duration::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
540         assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
541         assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
542         assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
543         assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
544         assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
545         assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
546         assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
547         assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
548     }
549
550     #[test]
551     fn test_duration_fmt() {
552         assert_eq!(Duration::zero().to_string(), "PT0S");
553         assert_eq!(Duration::days(42).to_string(), "P42D");
554         assert_eq!(Duration::days(-42).to_string(), "-P42D");
555         assert_eq!(Duration::seconds(42).to_string(), "PT42S");
556         assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
557         assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
558         assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
559         assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
560                    "P7DT6.543S");
561         assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
562         assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
563
564         // the format specifier should have no effect on `Duration`
565         assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
566                    "P1DT2.345S");
567     }
568 }