]> git.lizzy.rs Git - rust.git/blob - src/libcore/time.rs
Avoid 128-bit arithmetic where possible
[rust.git] / src / libcore / time.rs
1 // Copyright 2012-2017 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 #![stable(feature = "duration_core", since = "1.25.0")]
11
12 //! Temporal quantification.
13 //!
14 //! Example:
15 //!
16 //! ```
17 //! use std::time::Duration;
18 //!
19 //! let five_seconds = Duration::new(5, 0);
20 //! // both declarations are equivalent
21 //! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
22 //! ```
23
24 use iter::Sum;
25 use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
26
27 const NANOS_PER_SEC: u32 = 1_000_000_000;
28 const NANOS_PER_MILLI: u32 = 1_000_000;
29 const NANOS_PER_MICRO: u32 = 1_000;
30 const MILLIS_PER_SEC: u64 = 1_000;
31 const MICROS_PER_SEC: u64 = 1_000_000;
32
33 /// A `Duration` type to represent a span of time, typically used for system
34 /// timeouts.
35 ///
36 /// Each `Duration` is composed of a whole number of seconds and a fractional part
37 /// represented in nanoseconds.  If the underlying system does not support
38 /// nanosecond-level precision, APIs binding a system timeout will typically round up
39 /// the number of nanoseconds.
40 ///
41 /// `Duration`s implement many common traits, including [`Add`], [`Sub`], and other
42 /// [`ops`] traits.
43 ///
44 /// [`Add`]: ../../std/ops/trait.Add.html
45 /// [`Sub`]: ../../std/ops/trait.Sub.html
46 /// [`ops`]: ../../std/ops/index.html
47 ///
48 /// # Examples
49 ///
50 /// ```
51 /// use std::time::Duration;
52 ///
53 /// let five_seconds = Duration::new(5, 0);
54 /// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
55 ///
56 /// assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
57 /// assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
58 ///
59 /// let ten_millis = Duration::from_millis(10);
60 /// ```
61 #[stable(feature = "duration", since = "1.3.0")]
62 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Default)]
63 pub struct Duration {
64     secs: u64,
65     nanos: u32, // Always 0 <= nanos < NANOS_PER_SEC
66 }
67
68 impl Duration {
69     /// Creates a new `Duration` from the specified number of whole seconds and
70     /// additional nanoseconds.
71     ///
72     /// If the number of nanoseconds is greater than 1 billion (the number of
73     /// nanoseconds in a second), then it will carry over into the seconds provided.
74     ///
75     /// # Panics
76     ///
77     /// This constructor will panic if the carry from the nanoseconds overflows
78     /// the seconds counter.
79     ///
80     /// # Examples
81     ///
82     /// ```
83     /// use std::time::Duration;
84     ///
85     /// let five_seconds = Duration::new(5, 0);
86     /// ```
87     #[stable(feature = "duration", since = "1.3.0")]
88     #[inline]
89     pub fn new(secs: u64, nanos: u32) -> Duration {
90         let secs = secs.checked_add((nanos / NANOS_PER_SEC) as u64)
91             .expect("overflow in Duration::new");
92         let nanos = nanos % NANOS_PER_SEC;
93         Duration { secs: secs, nanos: nanos }
94     }
95
96     /// Creates a new `Duration` from the specified number of whole seconds.
97     ///
98     /// # Examples
99     ///
100     /// ```
101     /// use std::time::Duration;
102     ///
103     /// let duration = Duration::from_secs(5);
104     ///
105     /// assert_eq!(5, duration.as_secs());
106     /// assert_eq!(0, duration.subsec_nanos());
107     /// ```
108     #[stable(feature = "duration", since = "1.3.0")]
109     #[inline]
110     pub const fn from_secs(secs: u64) -> Duration {
111         Duration { secs: secs, nanos: 0 }
112     }
113
114     /// Creates a new `Duration` from the specified number of milliseconds.
115     ///
116     /// # Examples
117     ///
118     /// ```
119     /// use std::time::Duration;
120     ///
121     /// let duration = Duration::from_millis(2569);
122     ///
123     /// assert_eq!(2, duration.as_secs());
124     /// assert_eq!(569_000_000, duration.subsec_nanos());
125     /// ```
126     #[stable(feature = "duration", since = "1.3.0")]
127     #[inline]
128     pub const fn from_millis(millis: u64) -> Duration {
129         Duration {
130             secs: millis / MILLIS_PER_SEC,
131             nanos: ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI,
132         }
133     }
134
135     /// Creates a new `Duration` from the specified number of microseconds.
136     ///
137     /// # Examples
138     ///
139     /// ```
140     /// use std::time::Duration;
141     ///
142     /// let duration = Duration::from_micros(1_000_002);
143     ///
144     /// assert_eq!(1, duration.as_secs());
145     /// assert_eq!(2000, duration.subsec_nanos());
146     /// ```
147     #[stable(feature = "duration_from_micros", since = "1.27.0")]
148     #[inline]
149     pub const fn from_micros(micros: u64) -> Duration {
150         Duration {
151             secs: micros / MICROS_PER_SEC,
152             nanos: ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO,
153         }
154     }
155
156     /// Creates a new `Duration` from the specified number of nanoseconds.
157     ///
158     /// # Examples
159     ///
160     /// ```
161     /// use std::time::Duration;
162     ///
163     /// let duration = Duration::from_nanos(1_000_000_123);
164     ///
165     /// assert_eq!(1, duration.as_secs());
166     /// assert_eq!(123, duration.subsec_nanos());
167     /// ```
168     #[stable(feature = "duration_extras", since = "1.27.0")]
169     #[inline]
170     pub const fn from_nanos(nanos: u64) -> Duration {
171         Duration {
172             secs: nanos / (NANOS_PER_SEC as u64),
173             nanos: (nanos % (NANOS_PER_SEC as u64)) as u32,
174         }
175     }
176
177     /// Returns the number of _whole_ seconds contained by this `Duration`.
178     ///
179     /// The returned value does not include the fractional (nanosecond) part of the
180     /// duration, which can be obtained using [`subsec_nanos`].
181     ///
182     /// # Examples
183     ///
184     /// ```
185     /// use std::time::Duration;
186     ///
187     /// let duration = Duration::new(5, 730023852);
188     /// assert_eq!(duration.as_secs(), 5);
189     /// ```
190     ///
191     /// To determine the total number of seconds represented by the `Duration`,
192     /// use `as_secs` in combination with [`subsec_nanos`]:
193     ///
194     /// ```
195     /// use std::time::Duration;
196     ///
197     /// let duration = Duration::new(5, 730023852);
198     ///
199     /// assert_eq!(5.730023852,
200     ///            duration.as_secs() as f64
201     ///            + duration.subsec_nanos() as f64 * 1e-9);
202     /// ```
203     ///
204     /// [`subsec_nanos`]: #method.subsec_nanos
205     #[stable(feature = "duration", since = "1.3.0")]
206     #[inline]
207     pub fn as_secs(&self) -> u64 { self.secs }
208
209     /// Returns the fractional part of this `Duration`, in milliseconds.
210     ///
211     /// This method does **not** return the length of the duration when
212     /// represented by milliseconds. The returned number always represents a
213     /// fractional portion of a second (i.e. it is less than one thousand).
214     ///
215     /// # Examples
216     ///
217     /// ```
218     /// use std::time::Duration;
219     ///
220     /// let duration = Duration::from_millis(5432);
221     /// assert_eq!(duration.as_secs(), 5);
222     /// assert_eq!(duration.subsec_millis(), 432);
223     /// ```
224     #[stable(feature = "duration_extras", since = "1.27.0")]
225     #[inline]
226     pub fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
227
228     /// Returns the fractional part of this `Duration`, in microseconds.
229     ///
230     /// This method does **not** return the length of the duration when
231     /// represented by microseconds. The returned number always represents a
232     /// fractional portion of a second (i.e. it is less than one million).
233     ///
234     /// # Examples
235     ///
236     /// ```
237     /// use std::time::Duration;
238     ///
239     /// let duration = Duration::from_micros(1_234_567);
240     /// assert_eq!(duration.as_secs(), 1);
241     /// assert_eq!(duration.subsec_micros(), 234_567);
242     /// ```
243     #[stable(feature = "duration_extras", since = "1.27.0")]
244     #[inline]
245     pub fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
246
247     /// Returns the fractional part of this `Duration`, in nanoseconds.
248     ///
249     /// This method does **not** return the length of the duration when
250     /// represented by nanoseconds. The returned number always represents a
251     /// fractional portion of a second (i.e. it is less than one billion).
252     ///
253     /// # Examples
254     ///
255     /// ```
256     /// use std::time::Duration;
257     ///
258     /// let duration = Duration::from_millis(5010);
259     /// assert_eq!(duration.as_secs(), 5);
260     /// assert_eq!(duration.subsec_nanos(), 10_000_000);
261     /// ```
262     #[stable(feature = "duration", since = "1.3.0")]
263     #[inline]
264     pub fn subsec_nanos(&self) -> u32 { self.nanos }
265
266     /// Returns the total number of milliseconds contained by this `Duration`.
267     ///
268     /// # Examples
269     ///
270     /// ```
271     /// # #![feature(duration_as_u128)]
272     /// use std::time::Duration;
273     ///
274     /// let duration = Duration::new(5, 730023852);
275     /// assert_eq!(duration.as_millis(), 5730);
276     /// ```
277     #[unstable(feature = "duration_as_u128", issue = "50202")]
278     #[inline]
279     pub fn as_millis(&self) -> u128 {
280         self.secs as u128 * MILLIS_PER_SEC as u128 + (self.nanos / NANOS_PER_MILLI) as u128
281     }
282
283     /// Returns the total number of microseconds contained by this `Duration`.
284     ///
285     /// # Examples
286     ///
287     /// ```
288     /// # #![feature(duration_as_u128)]
289     /// use std::time::Duration;
290     ///
291     /// let duration = Duration::new(5, 730023852);
292     /// assert_eq!(duration.as_micros(), 5730023);
293     /// ```
294     #[unstable(feature = "duration_as_u128", issue = "50202")]
295     #[inline]
296     pub fn as_micros(&self) -> u128 {
297         self.secs as u128 * MICROS_PER_SEC as u128 + (self.nanos / NANOS_PER_MICRO) as u128
298     }
299
300     /// Returns the total number of nanoseconds contained by this `Duration`.
301     ///
302     /// # Examples
303     ///
304     /// ```
305     /// # #![feature(duration_as_u128)]
306     /// use std::time::Duration;
307     ///
308     /// let duration = Duration::new(5, 730023852);
309     /// assert_eq!(duration.as_nanos(), 5730023852);
310     /// ```
311     #[unstable(feature = "duration_as_u128", issue = "50202")]
312     #[inline]
313     pub fn as_nanos(&self) -> u128 {
314         self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128
315     }
316
317     /// Checked `Duration` addition. Computes `self + other`, returning [`None`]
318     /// if overflow occurred.
319     ///
320     /// [`None`]: ../../std/option/enum.Option.html#variant.None
321     ///
322     /// # Examples
323     ///
324     /// Basic usage:
325     ///
326     /// ```
327     /// use std::time::Duration;
328     ///
329     /// assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
330     /// assert_eq!(Duration::new(1, 0).checked_add(Duration::new(std::u64::MAX, 0)), None);
331     /// ```
332     #[stable(feature = "duration_checked_ops", since = "1.16.0")]
333     #[inline]
334     pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
335         if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
336             let mut nanos = self.nanos + rhs.nanos;
337             if nanos >= NANOS_PER_SEC {
338                 nanos -= NANOS_PER_SEC;
339                 if let Some(new_secs) = secs.checked_add(1) {
340                     secs = new_secs;
341                 } else {
342                     return None;
343                 }
344             }
345             debug_assert!(nanos < NANOS_PER_SEC);
346             Some(Duration {
347                 secs,
348                 nanos,
349             })
350         } else {
351             None
352         }
353     }
354
355     /// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
356     /// if the result would be negative or if overflow occurred.
357     ///
358     /// [`None`]: ../../std/option/enum.Option.html#variant.None
359     ///
360     /// # Examples
361     ///
362     /// Basic usage:
363     ///
364     /// ```
365     /// use std::time::Duration;
366     ///
367     /// assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
368     /// assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);
369     /// ```
370     #[stable(feature = "duration_checked_ops", since = "1.16.0")]
371     #[inline]
372     pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
373         if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
374             let nanos = if self.nanos >= rhs.nanos {
375                 self.nanos - rhs.nanos
376             } else {
377                 if let Some(sub_secs) = secs.checked_sub(1) {
378                     secs = sub_secs;
379                     self.nanos + NANOS_PER_SEC - rhs.nanos
380                 } else {
381                     return None;
382                 }
383             };
384             debug_assert!(nanos < NANOS_PER_SEC);
385             Some(Duration { secs: secs, nanos: nanos })
386         } else {
387             None
388         }
389     }
390
391     /// Checked `Duration` multiplication. Computes `self * other`, returning
392     /// [`None`] if overflow occurred.
393     ///
394     /// [`None`]: ../../std/option/enum.Option.html#variant.None
395     ///
396     /// # Examples
397     ///
398     /// Basic usage:
399     ///
400     /// ```
401     /// use std::time::Duration;
402     ///
403     /// assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
404     /// assert_eq!(Duration::new(std::u64::MAX - 1, 0).checked_mul(2), None);
405     /// ```
406     #[stable(feature = "duration_checked_ops", since = "1.16.0")]
407     #[inline]
408     pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
409         // Multiply nanoseconds as u64, because it cannot overflow that way.
410         let total_nanos = self.nanos as u64 * rhs as u64;
411         let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
412         let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
413         if let Some(secs) = self.secs
414             .checked_mul(rhs as u64)
415             .and_then(|s| s.checked_add(extra_secs)) {
416             debug_assert!(nanos < NANOS_PER_SEC);
417             Some(Duration {
418                 secs,
419                 nanos,
420             })
421         } else {
422             None
423         }
424     }
425
426     /// Checked `Duration` division. Computes `self / other`, returning [`None`]
427     /// if `other == 0`.
428     ///
429     /// [`None`]: ../../std/option/enum.Option.html#variant.None
430     ///
431     /// # Examples
432     ///
433     /// Basic usage:
434     ///
435     /// ```
436     /// use std::time::Duration;
437     ///
438     /// assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
439     /// assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
440     /// assert_eq!(Duration::new(2, 0).checked_div(0), None);
441     /// ```
442     #[stable(feature = "duration_checked_ops", since = "1.16.0")]
443     #[inline]
444     pub fn checked_div(self, rhs: u32) -> Option<Duration> {
445         if rhs != 0 {
446             let secs = self.secs / (rhs as u64);
447             let carry = self.secs - secs * (rhs as u64);
448             let extra_nanos = carry * (NANOS_PER_SEC as u64) / (rhs as u64);
449             let nanos = self.nanos / rhs + (extra_nanos as u32);
450             debug_assert!(nanos < NANOS_PER_SEC);
451             Some(Duration { secs: secs, nanos: nanos })
452         } else {
453             None
454         }
455     }
456 }
457
458 #[stable(feature = "duration", since = "1.3.0")]
459 impl Add for Duration {
460     type Output = Duration;
461
462     fn add(self, rhs: Duration) -> Duration {
463         self.checked_add(rhs).expect("overflow when adding durations")
464     }
465 }
466
467 #[stable(feature = "time_augmented_assignment", since = "1.9.0")]
468 impl AddAssign for Duration {
469     fn add_assign(&mut self, rhs: Duration) {
470         *self = *self + rhs;
471     }
472 }
473
474 #[stable(feature = "duration", since = "1.3.0")]
475 impl Sub for Duration {
476     type Output = Duration;
477
478     fn sub(self, rhs: Duration) -> Duration {
479         self.checked_sub(rhs).expect("overflow when subtracting durations")
480     }
481 }
482
483 #[stable(feature = "time_augmented_assignment", since = "1.9.0")]
484 impl SubAssign for Duration {
485     fn sub_assign(&mut self, rhs: Duration) {
486         *self = *self - rhs;
487     }
488 }
489
490 #[stable(feature = "duration", since = "1.3.0")]
491 impl Mul<u32> for Duration {
492     type Output = Duration;
493
494     fn mul(self, rhs: u32) -> Duration {
495         self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
496     }
497 }
498
499 #[stable(feature = "time_augmented_assignment", since = "1.9.0")]
500 impl MulAssign<u32> for Duration {
501     fn mul_assign(&mut self, rhs: u32) {
502         *self = *self * rhs;
503     }
504 }
505
506 #[stable(feature = "duration", since = "1.3.0")]
507 impl Div<u32> for Duration {
508     type Output = Duration;
509
510     fn div(self, rhs: u32) -> Duration {
511         self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
512     }
513 }
514
515 #[stable(feature = "time_augmented_assignment", since = "1.9.0")]
516 impl DivAssign<u32> for Duration {
517     fn div_assign(&mut self, rhs: u32) {
518         *self = *self / rhs;
519     }
520 }
521
522 #[stable(feature = "duration_sum", since = "1.16.0")]
523 impl Sum for Duration {
524     fn sum<I: Iterator<Item=Duration>>(iter: I) -> Duration {
525         iter.fold(Duration::new(0, 0), |a, b| a + b)
526     }
527 }
528
529 #[stable(feature = "duration_sum", since = "1.16.0")]
530 impl<'a> Sum<&'a Duration> for Duration {
531     fn sum<I: Iterator<Item=&'a Duration>>(iter: I) -> Duration {
532         iter.fold(Duration::new(0, 0), |a, b| a + *b)
533     }
534 }
535
536 #[cfg(test)]
537 mod tests {
538     use super::Duration;
539
540     #[test]
541     fn creation() {
542         assert!(Duration::from_secs(1) != Duration::from_secs(0));
543         assert_eq!(Duration::from_secs(1) + Duration::from_secs(2),
544                    Duration::from_secs(3));
545         assert_eq!(Duration::from_millis(10) + Duration::from_secs(4),
546                    Duration::new(4, 10 * 1_000_000));
547         assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
548     }
549
550     #[test]
551     fn secs() {
552         assert_eq!(Duration::new(0, 0).as_secs(), 0);
553         assert_eq!(Duration::from_secs(1).as_secs(), 1);
554         assert_eq!(Duration::from_millis(999).as_secs(), 0);
555         assert_eq!(Duration::from_millis(1001).as_secs(), 1);
556     }
557
558     #[test]
559     fn nanos() {
560         assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
561         assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
562         assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
563         assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
564         assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
565         assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
566     }
567
568     #[test]
569     fn add() {
570         assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
571                    Duration::new(0, 1));
572         assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
573                    Duration::new(1, 1));
574     }
575
576     #[test]
577     fn checked_add() {
578         assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)),
579                    Some(Duration::new(0, 1)));
580         assert_eq!(Duration::new(0, 500_000_000).checked_add(Duration::new(0, 500_000_001)),
581                    Some(Duration::new(1, 1)));
582         assert_eq!(Duration::new(1, 0).checked_add(Duration::new(::u64::MAX, 0)), None);
583     }
584
585     #[test]
586     fn sub() {
587         assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
588                    Duration::new(0, 1));
589         assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
590                    Duration::new(0, 1));
591         assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
592                    Duration::new(0, 999_999_999));
593     }
594
595     #[test]
596     fn checked_sub() {
597         let zero = Duration::new(0, 0);
598         let one_nano = Duration::new(0, 1);
599         let one_sec = Duration::new(1, 0);
600         assert_eq!(one_nano.checked_sub(zero), Some(Duration::new(0, 1)));
601         assert_eq!(one_sec.checked_sub(one_nano),
602                    Some(Duration::new(0, 999_999_999)));
603         assert_eq!(zero.checked_sub(one_nano), None);
604         assert_eq!(zero.checked_sub(one_sec), None);
605     }
606
607     #[test] #[should_panic]
608     fn sub_bad1() {
609         Duration::new(0, 0) - Duration::new(0, 1);
610     }
611
612     #[test] #[should_panic]
613     fn sub_bad2() {
614         Duration::new(0, 0) - Duration::new(1, 0);
615     }
616
617     #[test]
618     fn mul() {
619         assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
620         assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
621         assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
622         assert_eq!(Duration::new(0, 500_000_001) * 4000,
623                    Duration::new(2000, 4000));
624     }
625
626     #[test]
627     fn checked_mul() {
628         assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
629         assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
630         assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
631         assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000),
632                    Some(Duration::new(2000, 4000)));
633         assert_eq!(Duration::new(::u64::MAX - 1, 0).checked_mul(2), None);
634     }
635
636     #[test]
637     fn div() {
638         assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
639         assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
640         assert_eq!(Duration::new(99, 999_999_000) / 100,
641                    Duration::new(0, 999_999_990));
642     }
643
644     #[test]
645     fn checked_div() {
646         assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
647         assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
648         assert_eq!(Duration::new(2, 0).checked_div(0), None);
649     }
650 }