chrono/
duration.rs

1// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Temporal quantification
12
13use core::ops::{Add, Div, Mul, Neg, Sub};
14use core::time::Duration as StdDuration;
15use core::{fmt, i64};
16#[cfg(feature = "std")]
17use std::error::Error;
18
19#[cfg(feature = "rkyv")]
20use rkyv::{Archive, Deserialize, Serialize};
21
22/// The number of nanoseconds in a microsecond.
23const NANOS_PER_MICRO: i32 = 1000;
24/// The number of nanoseconds in a millisecond.
25const NANOS_PER_MILLI: i32 = 1_000_000;
26/// The number of nanoseconds in seconds.
27const NANOS_PER_SEC: i32 = 1_000_000_000;
28/// The number of microseconds per second.
29const MICROS_PER_SEC: i64 = 1_000_000;
30/// The number of milliseconds per second.
31const MILLIS_PER_SEC: i64 = 1000;
32/// The number of seconds in a minute.
33const SECS_PER_MINUTE: i64 = 60;
34/// The number of seconds in an hour.
35const SECS_PER_HOUR: i64 = 3600;
36/// The number of (non-leap) seconds in days.
37const SECS_PER_DAY: i64 = 86_400;
38/// The number of (non-leap) seconds in a week.
39const SECS_PER_WEEK: i64 = 604_800;
40
41macro_rules! try_opt {
42    ($e:expr) => {
43        match $e {
44            Some(v) => v,
45            None => return None,
46        }
47    };
48}
49
50/// ISO 8601 time duration with nanosecond precision.
51///
52/// This also allows for the negative duration; see individual methods for details.
53#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
54#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
55pub struct Duration {
56    secs: i64,
57    nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
58}
59
60/// The minimum possible `Duration`: `i64::MIN` milliseconds.
61pub(crate) const MIN: Duration = Duration {
62    secs: i64::MIN / MILLIS_PER_SEC - 1,
63    nanos: NANOS_PER_SEC + (i64::MIN % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI,
64};
65
66/// The maximum possible `Duration`: `i64::MAX` milliseconds.
67pub(crate) const MAX: Duration = Duration {
68    secs: i64::MAX / MILLIS_PER_SEC,
69    nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI,
70};
71
72impl Duration {
73    /// Makes a new `Duration` with given number of weeks.
74    /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60)` with overflow checks.
75    /// Panics when the duration is out of bounds.
76    #[inline]
77    #[must_use]
78    pub fn weeks(weeks: i64) -> Duration {
79        let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
80        Duration::seconds(secs)
81    }
82
83    /// Makes a new `Duration` with given number of days.
84    /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
85    /// Panics when the duration is out of bounds.
86    #[inline]
87    #[must_use]
88    pub fn days(days: i64) -> Duration {
89        let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
90        Duration::seconds(secs)
91    }
92
93    /// Makes a new `Duration` with given number of hours.
94    /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
95    /// Panics when the duration is out of bounds.
96    #[inline]
97    #[must_use]
98    pub fn hours(hours: i64) -> Duration {
99        let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours ouf of bounds");
100        Duration::seconds(secs)
101    }
102
103    /// Makes a new `Duration` with given number of minutes.
104    /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
105    /// Panics when the duration is out of bounds.
106    #[inline]
107    #[must_use]
108    pub fn minutes(minutes: i64) -> Duration {
109        let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
110        Duration::seconds(secs)
111    }
112
113    /// Makes a new `Duration` with given number of seconds.
114    /// Panics when the duration is more than `i64::MAX` milliseconds
115    /// or less than `i64::MIN` milliseconds.
116    #[inline]
117    #[must_use]
118    pub fn seconds(seconds: i64) -> Duration {
119        let d = Duration { secs: seconds, nanos: 0 };
120        if d < MIN || d > MAX {
121            panic!("Duration::seconds out of bounds");
122        }
123        d
124    }
125
126    /// Makes a new `Duration` with given number of milliseconds.
127    #[inline]
128    pub const fn milliseconds(milliseconds: i64) -> Duration {
129        let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
130        let nanos = millis as i32 * NANOS_PER_MILLI;
131        Duration { secs, nanos }
132    }
133
134    /// Makes a new `Duration` with given number of microseconds.
135    #[inline]
136    pub const fn microseconds(microseconds: i64) -> Duration {
137        let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
138        let nanos = micros as i32 * NANOS_PER_MICRO;
139        Duration { secs, nanos }
140    }
141
142    /// Makes a new `Duration` with given number of nanoseconds.
143    #[inline]
144    pub const fn nanoseconds(nanos: i64) -> Duration {
145        let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
146        Duration { secs, nanos: nanos as i32 }
147    }
148
149    /// Returns the total number of whole weeks in the duration.
150    #[inline]
151    pub const fn num_weeks(&self) -> i64 {
152        self.num_days() / 7
153    }
154
155    /// Returns the total number of whole days in the duration.
156    pub const fn num_days(&self) -> i64 {
157        self.num_seconds() / SECS_PER_DAY
158    }
159
160    /// Returns the total number of whole hours in the duration.
161    #[inline]
162    pub const fn num_hours(&self) -> i64 {
163        self.num_seconds() / SECS_PER_HOUR
164    }
165
166    /// Returns the total number of whole minutes in the duration.
167    #[inline]
168    pub const fn num_minutes(&self) -> i64 {
169        self.num_seconds() / SECS_PER_MINUTE
170    }
171
172    /// Returns the total number of whole seconds in the duration.
173    pub const fn num_seconds(&self) -> i64 {
174        // If secs is negative, nanos should be subtracted from the duration.
175        if self.secs < 0 && self.nanos > 0 {
176            self.secs + 1
177        } else {
178            self.secs
179        }
180    }
181
182    /// Returns the number of nanoseconds such that
183    /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of
184    /// nanoseconds in the duration.
185    const fn nanos_mod_sec(&self) -> i32 {
186        if self.secs < 0 && self.nanos > 0 {
187            self.nanos - NANOS_PER_SEC
188        } else {
189            self.nanos
190        }
191    }
192
193    /// Returns the total number of whole milliseconds in the duration,
194    pub const fn num_milliseconds(&self) -> i64 {
195        // A proper Duration will not overflow, because MIN and MAX are defined
196        // such that the range is exactly i64 milliseconds.
197        let secs_part = self.num_seconds() * MILLIS_PER_SEC;
198        let nanos_part = self.nanos_mod_sec() / NANOS_PER_MILLI;
199        secs_part + nanos_part as i64
200    }
201
202    /// Returns the total number of whole microseconds in the duration,
203    /// or `None` on overflow (exceeding 2^63 microseconds in either direction).
204    pub const fn num_microseconds(&self) -> Option<i64> {
205        let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
206        let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO;
207        secs_part.checked_add(nanos_part as i64)
208    }
209
210    /// Returns the total number of whole nanoseconds in the duration,
211    /// or `None` on overflow (exceeding 2^63 nanoseconds in either direction).
212    pub const fn num_nanoseconds(&self) -> Option<i64> {
213        let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
214        let nanos_part = self.nanos_mod_sec();
215        secs_part.checked_add(nanos_part as i64)
216    }
217
218    /// Add two durations, returning `None` if overflow occurred.
219    #[must_use]
220    pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
221        let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
222        let mut nanos = self.nanos + rhs.nanos;
223        if nanos >= NANOS_PER_SEC {
224            nanos -= NANOS_PER_SEC;
225            secs = try_opt!(secs.checked_add(1));
226        }
227        let d = Duration { secs, nanos };
228        // Even if d is within the bounds of i64 seconds,
229        // it might still overflow i64 milliseconds.
230        if d < MIN || d > MAX {
231            None
232        } else {
233            Some(d)
234        }
235    }
236
237    /// Subtract two durations, returning `None` if overflow occurred.
238    #[must_use]
239    pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
240        let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
241        let mut nanos = self.nanos - rhs.nanos;
242        if nanos < 0 {
243            nanos += NANOS_PER_SEC;
244            secs = try_opt!(secs.checked_sub(1));
245        }
246        let d = Duration { secs, nanos };
247        // Even if d is within the bounds of i64 seconds,
248        // it might still overflow i64 milliseconds.
249        if d < MIN || d > MAX {
250            None
251        } else {
252            Some(d)
253        }
254    }
255
256    /// Returns the duration as an absolute (non-negative) value.
257    #[inline]
258    pub const fn abs(&self) -> Duration {
259        if self.secs < 0 && self.nanos != 0 {
260            Duration { secs: (self.secs + 1).abs(), nanos: NANOS_PER_SEC - self.nanos }
261        } else {
262            Duration { secs: self.secs.abs(), nanos: self.nanos }
263        }
264    }
265
266    /// The minimum possible `Duration`: `i64::MIN` milliseconds.
267    #[inline]
268    pub const fn min_value() -> Duration {
269        MIN
270    }
271
272    /// The maximum possible `Duration`: `i64::MAX` milliseconds.
273    #[inline]
274    pub const fn max_value() -> Duration {
275        MAX
276    }
277
278    /// A duration where the stored seconds and nanoseconds are equal to zero.
279    #[inline]
280    pub const fn zero() -> Duration {
281        Duration { secs: 0, nanos: 0 }
282    }
283
284    /// Returns `true` if the duration equals `Duration::zero()`.
285    #[inline]
286    pub const fn is_zero(&self) -> bool {
287        self.secs == 0 && self.nanos == 0
288    }
289
290    /// Creates a `time::Duration` object from `std::time::Duration`
291    ///
292    /// This function errors when original duration is larger than the maximum
293    /// value supported for this type.
294    pub fn from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError> {
295        // We need to check secs as u64 before coercing to i64
296        if duration.as_secs() > MAX.secs as u64 {
297            return Err(OutOfRangeError(()));
298        }
299        let d = Duration { secs: duration.as_secs() as i64, nanos: duration.subsec_nanos() as i32 };
300        if d > MAX {
301            return Err(OutOfRangeError(()));
302        }
303        Ok(d)
304    }
305
306    /// Creates a `std::time::Duration` object from `time::Duration`
307    ///
308    /// This function errors when duration is less than zero. As standard
309    /// library implementation is limited to non-negative values.
310    pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
311        if self.secs < 0 {
312            return Err(OutOfRangeError(()));
313        }
314        Ok(StdDuration::new(self.secs as u64, self.nanos as u32))
315    }
316}
317
318impl Neg for Duration {
319    type Output = Duration;
320
321    #[inline]
322    fn neg(self) -> Duration {
323        if self.nanos == 0 {
324            Duration { secs: -self.secs, nanos: 0 }
325        } else {
326            Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
327        }
328    }
329}
330
331impl Add for Duration {
332    type Output = Duration;
333
334    fn add(self, rhs: Duration) -> Duration {
335        let mut secs = self.secs + rhs.secs;
336        let mut nanos = self.nanos + rhs.nanos;
337        if nanos >= NANOS_PER_SEC {
338            nanos -= NANOS_PER_SEC;
339            secs += 1;
340        }
341        Duration { secs, nanos }
342    }
343}
344
345impl Sub for Duration {
346    type Output = Duration;
347
348    fn sub(self, rhs: Duration) -> Duration {
349        let mut secs = self.secs - rhs.secs;
350        let mut nanos = self.nanos - rhs.nanos;
351        if nanos < 0 {
352            nanos += NANOS_PER_SEC;
353            secs -= 1;
354        }
355        Duration { secs, nanos }
356    }
357}
358
359impl Mul<i32> for Duration {
360    type Output = Duration;
361
362    fn mul(self, rhs: i32) -> Duration {
363        // Multiply nanoseconds as i64, because it cannot overflow that way.
364        let total_nanos = self.nanos as i64 * rhs as i64;
365        let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
366        let secs = self.secs * rhs as i64 + extra_secs;
367        Duration { secs, nanos: nanos as i32 }
368    }
369}
370
371impl Div<i32> for Duration {
372    type Output = Duration;
373
374    fn div(self, rhs: i32) -> Duration {
375        let mut secs = self.secs / rhs as i64;
376        let carry = self.secs - secs * rhs as i64;
377        let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
378        let mut nanos = self.nanos / rhs + extra_nanos as i32;
379        if nanos >= NANOS_PER_SEC {
380            nanos -= NANOS_PER_SEC;
381            secs += 1;
382        }
383        if nanos < 0 {
384            nanos += NANOS_PER_SEC;
385            secs -= 1;
386        }
387        Duration { secs, nanos }
388    }
389}
390
391impl<'a> core::iter::Sum<&'a Duration> for Duration {
392    fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
393        iter.fold(Duration::zero(), |acc, x| acc + *x)
394    }
395}
396
397impl core::iter::Sum<Duration> for Duration {
398    fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
399        iter.fold(Duration::zero(), |acc, x| acc + x)
400    }
401}
402
403impl fmt::Display for Duration {
404    /// Format a duration using the [ISO 8601] format
405    ///
406    /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601#Durations
407    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
408        // technically speaking, negative duration is not valid ISO 8601,
409        // but we need to print it anyway.
410        let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
411
412        let days = abs.secs / SECS_PER_DAY;
413        let secs = abs.secs - days * SECS_PER_DAY;
414        let hasdate = days != 0;
415        let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
416
417        write!(f, "{}P", sign)?;
418
419        if hasdate {
420            write!(f, "{}D", days)?;
421        }
422        if hastime {
423            if abs.nanos == 0 {
424                write!(f, "T{}S", secs)?;
425            } else if abs.nanos % NANOS_PER_MILLI == 0 {
426                write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI)?;
427            } else if abs.nanos % NANOS_PER_MICRO == 0 {
428                write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO)?;
429            } else {
430                write!(f, "T{}.{:09}S", secs, abs.nanos)?;
431            }
432        }
433        Ok(())
434    }
435}
436
437/// Represents error when converting `Duration` to/from a standard library
438/// implementation
439///
440/// The `std::time::Duration` supports a range from zero to `u64::MAX`
441/// *seconds*, while this module supports signed range of up to
442/// `i64::MAX` of *milliseconds*.
443#[derive(Debug, Clone, Copy, PartialEq, Eq)]
444pub struct OutOfRangeError(());
445
446impl fmt::Display for OutOfRangeError {
447    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
448        write!(f, "Source duration value is out of range for the target type")
449    }
450}
451
452#[cfg(feature = "std")]
453impl Error for OutOfRangeError {
454    #[allow(deprecated)]
455    fn description(&self) -> &str {
456        "out of range error"
457    }
458}
459
460#[inline]
461const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
462    (this.div_euclid(other), this.rem_euclid(other))
463}
464
465#[cfg(feature = "arbitrary")]
466impl arbitrary::Arbitrary<'_> for Duration {
467    fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Duration> {
468        const MIN_SECS: i64 = i64::MIN / MILLIS_PER_SEC - 1;
469        const MAX_SECS: i64 = i64::MAX / MILLIS_PER_SEC;
470
471        let secs: i64 = u.int_in_range(MIN_SECS..=MAX_SECS)?;
472        let nanos: i32 = u.int_in_range(0..=(NANOS_PER_SEC - 1))?;
473        let duration = Duration { secs, nanos };
474
475        if duration < MIN || duration > MAX {
476            Err(arbitrary::Error::IncorrectFormat)
477        } else {
478            Ok(duration)
479        }
480    }
481}
482
483#[cfg(test)]
484mod tests {
485    use super::OutOfRangeError;
486    use super::{Duration, MAX, MIN};
487    use core::time::Duration as StdDuration;
488
489    #[test]
490    fn test_duration() {
491        assert!(Duration::seconds(1) != Duration::zero());
492        assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
493        assert_eq!(
494            Duration::seconds(86_399) + Duration::seconds(4),
495            Duration::days(1) + Duration::seconds(3)
496        );
497        assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863_000));
498        assert_eq!(Duration::days(10) - Duration::seconds(1_000_000), Duration::seconds(-136_000));
499        assert_eq!(
500            Duration::days(2) + Duration::seconds(86_399) + Duration::nanoseconds(1_234_567_890),
501            Duration::days(3) + Duration::nanoseconds(234_567_890)
502        );
503        assert_eq!(-Duration::days(3), Duration::days(-3));
504        assert_eq!(
505            -(Duration::days(3) + Duration::seconds(70)),
506            Duration::days(-4) + Duration::seconds(86_400 - 70)
507        );
508    }
509
510    #[test]
511    fn test_duration_num_days() {
512        assert_eq!(Duration::zero().num_days(), 0);
513        assert_eq!(Duration::days(1).num_days(), 1);
514        assert_eq!(Duration::days(-1).num_days(), -1);
515        assert_eq!(Duration::seconds(86_399).num_days(), 0);
516        assert_eq!(Duration::seconds(86_401).num_days(), 1);
517        assert_eq!(Duration::seconds(-86_399).num_days(), 0);
518        assert_eq!(Duration::seconds(-86_401).num_days(), -1);
519        assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
520        assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
521    }
522
523    #[test]
524    fn test_duration_num_seconds() {
525        assert_eq!(Duration::zero().num_seconds(), 0);
526        assert_eq!(Duration::seconds(1).num_seconds(), 1);
527        assert_eq!(Duration::seconds(-1).num_seconds(), -1);
528        assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
529        assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
530        assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
531        assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
532    }
533
534    #[test]
535    fn test_duration_num_milliseconds() {
536        assert_eq!(Duration::zero().num_milliseconds(), 0);
537        assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
538        assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
539        assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
540        assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
541        assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
542        assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
543        assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
544        assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
545        assert_eq!(MAX.num_milliseconds(), i64::MAX);
546        assert_eq!(MIN.num_milliseconds(), i64::MIN);
547    }
548
549    #[test]
550    fn test_duration_num_microseconds() {
551        assert_eq!(Duration::zero().num_microseconds(), Some(0));
552        assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
553        assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
554        assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
555        assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
556        assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
557        assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
558        assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
559        assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
560        assert_eq!(MAX.num_microseconds(), None);
561        assert_eq!(MIN.num_microseconds(), None);
562
563        // overflow checks
564        const MICROS_PER_DAY: i64 = 86_400_000_000;
565        assert_eq!(
566            Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
567            Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY)
568        );
569        assert_eq!(
570            Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
571            Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY)
572        );
573        assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
574        assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
575    }
576
577    #[test]
578    fn test_duration_num_nanoseconds() {
579        assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
580        assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
581        assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
582        assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
583        assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
584        assert_eq!(MAX.num_nanoseconds(), None);
585        assert_eq!(MIN.num_nanoseconds(), None);
586
587        // overflow checks
588        const NANOS_PER_DAY: i64 = 86_400_000_000_000;
589        assert_eq!(
590            Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
591            Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY)
592        );
593        assert_eq!(
594            Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
595            Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY)
596        );
597        assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
598        assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
599    }
600
601    #[test]
602    fn test_duration_checked_ops() {
603        assert_eq!(
604            Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
605            Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999))
606        );
607        assert!(Duration::milliseconds(i64::MAX)
608            .checked_add(&Duration::microseconds(1000))
609            .is_none());
610
611        assert_eq!(
612            Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
613            Some(Duration::milliseconds(i64::MIN))
614        );
615        assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1)).is_none());
616    }
617
618    #[test]
619    fn test_duration_abs() {
620        assert_eq!(Duration::milliseconds(1300).abs(), Duration::milliseconds(1300));
621        assert_eq!(Duration::milliseconds(1000).abs(), Duration::milliseconds(1000));
622        assert_eq!(Duration::milliseconds(300).abs(), Duration::milliseconds(300));
623        assert_eq!(Duration::milliseconds(0).abs(), Duration::milliseconds(0));
624        assert_eq!(Duration::milliseconds(-300).abs(), Duration::milliseconds(300));
625        assert_eq!(Duration::milliseconds(-700).abs(), Duration::milliseconds(700));
626        assert_eq!(Duration::milliseconds(-1000).abs(), Duration::milliseconds(1000));
627        assert_eq!(Duration::milliseconds(-1300).abs(), Duration::milliseconds(1300));
628        assert_eq!(Duration::milliseconds(-1700).abs(), Duration::milliseconds(1700));
629    }
630
631    #[test]
632    #[allow(clippy::erasing_op)]
633    fn test_duration_mul() {
634        assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
635        assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
636        assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
637        assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
638        assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
639        assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
640        assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
641        assert_eq!(
642            Duration::nanoseconds(30) * 333_333_333,
643            Duration::seconds(10) - Duration::nanoseconds(10)
644        );
645        assert_eq!(
646            (Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
647            Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3)
648        );
649        assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
650        assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
651    }
652
653    #[test]
654    fn test_duration_div() {
655        assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
656        assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
657        assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
658        assert_eq!(Duration::nanoseconds(123_456_789) / -1, -Duration::nanoseconds(123_456_789));
659        assert_eq!(-Duration::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
660        assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
661        assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
662        assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
663        assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
664        assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
665        assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
666        assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
667        assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
668    }
669
670    #[test]
671    fn test_duration_sum() {
672        let duration_list_1 = [Duration::zero(), Duration::seconds(1)];
673        let sum_1: Duration = duration_list_1.iter().sum();
674        assert_eq!(sum_1, Duration::seconds(1));
675
676        let duration_list_2 =
677            [Duration::zero(), Duration::seconds(1), Duration::seconds(6), Duration::seconds(10)];
678        let sum_2: Duration = duration_list_2.iter().sum();
679        assert_eq!(sum_2, Duration::seconds(17));
680
681        let duration_arr =
682            [Duration::zero(), Duration::seconds(1), Duration::seconds(6), Duration::seconds(10)];
683        let sum_3: Duration = duration_arr.into_iter().sum();
684        assert_eq!(sum_3, Duration::seconds(17));
685    }
686
687    #[test]
688    fn test_duration_fmt() {
689        assert_eq!(Duration::zero().to_string(), "PT0S");
690        assert_eq!(Duration::days(42).to_string(), "P42D");
691        assert_eq!(Duration::days(-42).to_string(), "-P42D");
692        assert_eq!(Duration::seconds(42).to_string(), "PT42S");
693        assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
694        assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
695        assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
696        assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(), "P7DT6.543S");
697        assert_eq!(Duration::seconds(-86_401).to_string(), "-P1DT1S");
698        assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
699
700        // the format specifier should have no effect on `Duration`
701        assert_eq!(
702            format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
703            "P1DT2.345S"
704        );
705    }
706
707    #[test]
708    fn test_to_std() {
709        assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
710        assert_eq!(Duration::seconds(86_401).to_std(), Ok(StdDuration::new(86_401, 0)));
711        assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123_000_000)));
712        assert_eq!(
713            Duration::milliseconds(123_765).to_std(),
714            Ok(StdDuration::new(123, 765_000_000))
715        );
716        assert_eq!(Duration::nanoseconds(777).to_std(), Ok(StdDuration::new(0, 777)));
717        assert_eq!(MAX.to_std(), Ok(StdDuration::new(9_223_372_036_854_775, 807_000_000)));
718        assert_eq!(Duration::seconds(-1).to_std(), Err(OutOfRangeError(())));
719        assert_eq!(Duration::milliseconds(-1).to_std(), Err(OutOfRangeError(())));
720    }
721
722    #[test]
723    fn test_from_std() {
724        assert_eq!(Ok(Duration::seconds(1)), Duration::from_std(StdDuration::new(1, 0)));
725        assert_eq!(Ok(Duration::seconds(86_401)), Duration::from_std(StdDuration::new(86_401, 0)));
726        assert_eq!(
727            Ok(Duration::milliseconds(123)),
728            Duration::from_std(StdDuration::new(0, 123_000_000))
729        );
730        assert_eq!(
731            Ok(Duration::milliseconds(123_765)),
732            Duration::from_std(StdDuration::new(123, 765_000_000))
733        );
734        assert_eq!(Ok(Duration::nanoseconds(777)), Duration::from_std(StdDuration::new(0, 777)));
735        assert_eq!(
736            Ok(MAX),
737            Duration::from_std(StdDuration::new(9_223_372_036_854_775, 807_000_000))
738        );
739        assert_eq!(
740            Duration::from_std(StdDuration::new(9_223_372_036_854_776, 0)),
741            Err(OutOfRangeError(()))
742        );
743        assert_eq!(
744            Duration::from_std(StdDuration::new(9_223_372_036_854_775, 807_000_001)),
745            Err(OutOfRangeError(()))
746        );
747    }
748}