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