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