Skip to main content

ai_chrono/
time_delta.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::error::Error;
14use core::fmt;
15use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
16use core::time::Duration;
17
18use crate::{expect, try_opt};
19
20#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
21use rkyv::{Archive, Deserialize, Serialize};
22
23/// The number of nanoseconds in a microsecond.
24const NANOS_PER_MICRO: i32 = 1000;
25/// The number of nanoseconds in a millisecond.
26const NANOS_PER_MILLI: i32 = 1_000_000;
27/// The number of nanoseconds in seconds.
28pub(crate) const NANOS_PER_SEC: i32 = 1_000_000_000;
29/// The number of microseconds per second.
30const MICROS_PER_SEC: i64 = 1_000_000;
31/// The number of milliseconds per second.
32const MILLIS_PER_SEC: i64 = 1000;
33/// The number of seconds in a minute.
34const SECS_PER_MINUTE: i64 = 60;
35/// The number of seconds in an hour.
36const SECS_PER_HOUR: i64 = 3600;
37/// The number of (non-leap) seconds in days.
38const SECS_PER_DAY: i64 = 86_400;
39/// The number of (non-leap) seconds in a week.
40const SECS_PER_WEEK: i64 = 604_800;
41
42/// Time duration with nanosecond precision.
43///
44/// This also allows for negative durations; see individual methods for details.
45///
46/// A `TimeDelta` is represented internally as a complement of seconds and
47/// nanoseconds. The range is restricted to that of `i64` milliseconds, with the
48/// minimum value notably being set to `-i64::MAX` rather than allowing the full
49/// range of `i64::MIN`. This is to allow easy flipping of sign, so that for
50/// instance `abs()` can be called without any checks.
51#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
52#[cfg_attr(
53    any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"),
54    derive(Archive, Deserialize, Serialize),
55    archive(compare(PartialEq, PartialOrd)),
56    archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash))
57)]
58#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
59#[cfg_attr(feature = "defmt", derive(defmt::Format))]
60pub struct TimeDelta {
61    secs: i64,
62    nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
63}
64
65/// The minimum possible `TimeDelta`: `-i64::MAX` milliseconds.
66pub(crate) const MIN: TimeDelta = TimeDelta {
67    secs: -i64::MAX / MILLIS_PER_SEC - 1,
68    nanos: NANOS_PER_SEC + (-i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI,
69};
70
71/// The maximum possible `TimeDelta`: `i64::MAX` milliseconds.
72pub(crate) const MAX: TimeDelta = TimeDelta {
73    secs: i64::MAX / MILLIS_PER_SEC,
74    nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI,
75};
76
77impl TimeDelta {
78    /// Makes a new `TimeDelta` with given number of seconds and nanoseconds.
79    ///
80    /// # Errors
81    ///
82    /// Returns `None` when the duration is out of bounds, or if `nanos` ≥ 1,000,000,000.
83    pub const fn new(secs: i64, nanos: u32) -> Option<TimeDelta> {
84        if secs < MIN.secs
85            || secs > MAX.secs
86            || nanos >= 1_000_000_000
87            || (secs == MAX.secs && nanos > MAX.nanos as u32)
88            || (secs == MIN.secs && nanos < MIN.nanos as u32)
89        {
90            return None;
91        }
92        Some(TimeDelta { secs, nanos: nanos as i32 })
93    }
94
95    /// Makes a new `TimeDelta` with the given number of weeks.
96    ///
97    /// Equivalent to `TimeDelta::seconds(weeks * 7 * 24 * 60 * 60)` with
98    /// overflow checks.
99    ///
100    /// # Panics
101    ///
102    /// Panics when the duration is out of bounds.
103    #[inline]
104    #[must_use]
105    #[track_caller]
106    pub const fn weeks(weeks: i64) -> TimeDelta {
107        expect(TimeDelta::try_weeks(weeks), "TimeDelta::weeks out of bounds")
108    }
109
110    /// Makes a new `TimeDelta` with the given number of weeks.
111    ///
112    /// Equivalent to `TimeDelta::try_seconds(weeks * 7 * 24 * 60 * 60)` with
113    /// overflow checks.
114    ///
115    /// # Errors
116    ///
117    /// Returns `None` when the `TimeDelta` would be out of bounds.
118    #[inline]
119    pub const fn try_weeks(weeks: i64) -> Option<TimeDelta> {
120        TimeDelta::try_seconds(try_opt!(weeks.checked_mul(SECS_PER_WEEK)))
121    }
122
123    /// Makes a new `TimeDelta` with the given number of days.
124    ///
125    /// Equivalent to `TimeDelta::seconds(days * 24 * 60 * 60)` with overflow
126    /// checks.
127    ///
128    /// # Panics
129    ///
130    /// Panics when the `TimeDelta` would be out of bounds.
131    #[inline]
132    #[must_use]
133    #[track_caller]
134    pub const fn days(days: i64) -> TimeDelta {
135        expect(TimeDelta::try_days(days), "TimeDelta::days out of bounds")
136    }
137
138    /// Makes a new `TimeDelta` with the given number of days.
139    ///
140    /// Equivalent to `TimeDelta::try_seconds(days * 24 * 60 * 60)` with overflow
141    /// checks.
142    ///
143    /// # Errors
144    ///
145    /// Returns `None` when the `TimeDelta` would be out of bounds.
146    #[inline]
147    pub const fn try_days(days: i64) -> Option<TimeDelta> {
148        TimeDelta::try_seconds(try_opt!(days.checked_mul(SECS_PER_DAY)))
149    }
150
151    /// Makes a new `TimeDelta` with the given number of hours.
152    ///
153    /// Equivalent to `TimeDelta::seconds(hours * 60 * 60)` with overflow checks.
154    ///
155    /// # Panics
156    ///
157    /// Panics when the `TimeDelta` would be out of bounds.
158    #[inline]
159    #[must_use]
160    #[track_caller]
161    pub const fn hours(hours: i64) -> TimeDelta {
162        expect(TimeDelta::try_hours(hours), "TimeDelta::hours out of bounds")
163    }
164
165    /// Makes a new `TimeDelta` with the given number of hours.
166    ///
167    /// Equivalent to `TimeDelta::try_seconds(hours * 60 * 60)` with overflow checks.
168    ///
169    /// # Errors
170    ///
171    /// Returns `None` when the `TimeDelta` would be out of bounds.
172    #[inline]
173    pub const fn try_hours(hours: i64) -> Option<TimeDelta> {
174        TimeDelta::try_seconds(try_opt!(hours.checked_mul(SECS_PER_HOUR)))
175    }
176
177    /// Makes a new `TimeDelta` with the given number of minutes.
178    ///
179    /// Equivalent to `TimeDelta::seconds(minutes * 60)` with overflow checks.
180    ///
181    /// # Panics
182    ///
183    /// Panics when the `TimeDelta` would be out of bounds.
184    #[inline]
185    #[must_use]
186    #[track_caller]
187    pub const fn minutes(minutes: i64) -> TimeDelta {
188        expect(TimeDelta::try_minutes(minutes), "TimeDelta::minutes out of bounds")
189    }
190
191    /// Makes a new `TimeDelta` with the given number of minutes.
192    ///
193    /// Equivalent to `TimeDelta::try_seconds(minutes * 60)` with overflow checks.
194    ///
195    /// # Errors
196    ///
197    /// Returns `None` when the `TimeDelta` would be out of bounds.
198    #[inline]
199    pub const fn try_minutes(minutes: i64) -> Option<TimeDelta> {
200        TimeDelta::try_seconds(try_opt!(minutes.checked_mul(SECS_PER_MINUTE)))
201    }
202
203    /// Makes a new `TimeDelta` with the given number of seconds.
204    ///
205    /// # Panics
206    ///
207    /// Panics when `seconds` is more than `i64::MAX / 1_000` or less than `-i64::MAX / 1_000`
208    /// (in this context, this is the same as `i64::MIN / 1_000` due to rounding).
209    #[inline]
210    #[must_use]
211    #[track_caller]
212    pub const fn seconds(seconds: i64) -> TimeDelta {
213        expect(TimeDelta::try_seconds(seconds), "TimeDelta::seconds out of bounds")
214    }
215
216    /// Makes a new `TimeDelta` with the given number of seconds.
217    ///
218    /// # Errors
219    ///
220    /// Returns `None` when `seconds` is more than `i64::MAX / 1_000` or less than
221    /// `-i64::MAX / 1_000` (in this context, this is the same as `i64::MIN / 1_000` due to
222    /// rounding).
223    #[inline]
224    pub const fn try_seconds(seconds: i64) -> Option<TimeDelta> {
225        TimeDelta::new(seconds, 0)
226    }
227
228    /// Makes a new `TimeDelta` with the given number of milliseconds.
229    ///
230    /// # Panics
231    ///
232    /// Panics when the `TimeDelta` would be out of bounds, i.e. when `milliseconds` is more than
233    /// `i64::MAX` or less than `-i64::MAX`. Notably, this is not the same as `i64::MIN`.
234    #[inline]
235    #[track_caller]
236    pub const fn milliseconds(milliseconds: i64) -> TimeDelta {
237        expect(TimeDelta::try_milliseconds(milliseconds), "TimeDelta::milliseconds out of bounds")
238    }
239
240    /// Makes a new `TimeDelta` with the given number of milliseconds.
241    ///
242    /// # Errors
243    ///
244    /// Returns `None` the `TimeDelta` would be out of bounds, i.e. when `milliseconds` is more
245    /// than `i64::MAX` or less than `-i64::MAX`. Notably, this is not the same as `i64::MIN`.
246    #[inline]
247    pub const fn try_milliseconds(milliseconds: i64) -> Option<TimeDelta> {
248        // We don't need to compare against MAX, as this function accepts an
249        // i64, and MAX is aligned to i64::MAX milliseconds.
250        if milliseconds < -i64::MAX {
251            return None;
252        }
253        let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
254        let d = TimeDelta { secs, nanos: millis as i32 * NANOS_PER_MILLI };
255        Some(d)
256    }
257
258    /// Makes a new `TimeDelta` with the given number of microseconds.
259    ///
260    /// The number of microseconds acceptable by this constructor is less than
261    /// the total number that can actually be stored in a `TimeDelta`, so it is
262    /// not possible to specify a value that would be out of bounds. This
263    /// function is therefore infallible.
264    #[inline]
265    pub const fn microseconds(microseconds: i64) -> TimeDelta {
266        let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
267        let nanos = micros as i32 * NANOS_PER_MICRO;
268        TimeDelta { secs, nanos }
269    }
270
271    /// Makes a new `TimeDelta` with the given number of nanoseconds.
272    ///
273    /// The number of nanoseconds acceptable by this constructor is less than
274    /// the total number that can actually be stored in a `TimeDelta`, so it is
275    /// not possible to specify a value that would be out of bounds. This
276    /// function is therefore infallible.
277    #[inline]
278    pub const fn nanoseconds(nanos: i64) -> TimeDelta {
279        let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
280        TimeDelta { secs, nanos: nanos as i32 }
281    }
282
283    /// Returns the total number of whole weeks in the `TimeDelta`.
284    #[inline]
285    pub const fn num_weeks(&self) -> i64 {
286        self.num_days() / 7
287    }
288
289    /// Returns the total number of whole days in the `TimeDelta`.
290    #[inline]
291    pub const fn num_days(&self) -> i64 {
292        self.num_seconds() / SECS_PER_DAY
293    }
294
295    /// Returns the total number of whole hours in the `TimeDelta`.
296    #[inline]
297    pub const fn num_hours(&self) -> i64 {
298        self.num_seconds() / SECS_PER_HOUR
299    }
300
301    /// Returns the total number of whole minutes in the `TimeDelta`.
302    #[inline]
303    pub const fn num_minutes(&self) -> i64 {
304        self.num_seconds() / SECS_PER_MINUTE
305    }
306
307    /// Returns the total number of whole seconds in the `TimeDelta`.
308    pub const fn num_seconds(&self) -> i64 {
309        // If secs is negative, nanos should be subtracted from the duration.
310        if self.secs < 0 && self.nanos > 0 { self.secs + 1 } else { self.secs }
311    }
312
313    /// Returns the fractional number of seconds in the `TimeDelta`.
314    pub fn as_seconds_f64(self) -> f64 {
315        self.secs as f64 + self.nanos as f64 / NANOS_PER_SEC as f64
316    }
317
318    /// Returns the fractional number of seconds in the `TimeDelta`.
319    pub fn as_seconds_f32(self) -> f32 {
320        self.secs as f32 + self.nanos as f32 / NANOS_PER_SEC as f32
321    }
322
323    /// Returns the total number of whole milliseconds in the `TimeDelta`.
324    pub const fn num_milliseconds(&self) -> i64 {
325        // A proper TimeDelta will not overflow, because MIN and MAX are defined such
326        // that the range is within the bounds of an i64, from -i64::MAX through to
327        // +i64::MAX inclusive. Notably, i64::MIN is excluded from this range.
328        let secs_part = self.num_seconds() * MILLIS_PER_SEC;
329        let nanos_part = self.subsec_nanos() / NANOS_PER_MILLI;
330        secs_part + nanos_part as i64
331    }
332
333    /// Returns the number of milliseconds in the fractional part of the duration.
334    ///
335    /// This is the number of milliseconds such that
336    /// `subsec_millis() + num_seconds() * 1_000` is the truncated number of
337    /// milliseconds in the duration.
338    pub const fn subsec_millis(&self) -> i32 {
339        self.subsec_nanos() / NANOS_PER_MILLI
340    }
341
342    /// Returns the total number of whole microseconds in the `TimeDelta`,
343    /// or `None` on overflow (exceeding 2^63 microseconds in either direction).
344    pub const fn num_microseconds(&self) -> Option<i64> {
345        let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
346        let nanos_part = self.subsec_nanos() / NANOS_PER_MICRO;
347        secs_part.checked_add(nanos_part as i64)
348    }
349
350    /// Returns the number of microseconds in the fractional part of the duration.
351    ///
352    /// This is the number of microseconds such that
353    /// `subsec_micros() + num_seconds() * 1_000_000` is the truncated number of
354    /// microseconds in the duration.
355    pub const fn subsec_micros(&self) -> i32 {
356        self.subsec_nanos() / NANOS_PER_MICRO
357    }
358
359    /// Returns the total number of whole nanoseconds in the `TimeDelta`,
360    /// or `None` on overflow (exceeding 2^63 nanoseconds in either direction).
361    pub const fn num_nanoseconds(&self) -> Option<i64> {
362        let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
363        let nanos_part = self.subsec_nanos();
364        secs_part.checked_add(nanos_part as i64)
365    }
366
367    /// Returns the number of nanoseconds in the fractional part of the duration.
368    ///
369    /// This is the number of nanoseconds such that
370    /// `subsec_nanos() + num_seconds() * 1_000_000_000` is the total number of
371    /// nanoseconds in the `TimeDelta`.
372    pub const fn subsec_nanos(&self) -> i32 {
373        if self.secs < 0 && self.nanos > 0 { self.nanos - NANOS_PER_SEC } else { self.nanos }
374    }
375
376    /// Add two `TimeDelta`s, returning `None` if overflow occurred.
377    #[must_use]
378    pub const fn checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta> {
379        // No overflow checks here because we stay comfortably within the range of an `i64`.
380        // Range checks happen in `TimeDelta::new`.
381        let mut secs = self.secs + rhs.secs;
382        let mut nanos = self.nanos + rhs.nanos;
383        if nanos >= NANOS_PER_SEC {
384            nanos -= NANOS_PER_SEC;
385            secs += 1;
386        }
387        TimeDelta::new(secs, nanos as u32)
388    }
389
390    /// Subtract two `TimeDelta`s, returning `None` if overflow occurred.
391    #[must_use]
392    pub const fn checked_sub(&self, rhs: &TimeDelta) -> Option<TimeDelta> {
393        // No overflow checks here because we stay comfortably within the range of an `i64`.
394        // Range checks happen in `TimeDelta::new`.
395        let mut secs = self.secs - rhs.secs;
396        let mut nanos = self.nanos - rhs.nanos;
397        if nanos < 0 {
398            nanos += NANOS_PER_SEC;
399            secs -= 1;
400        }
401        TimeDelta::new(secs, nanos as u32)
402    }
403
404    /// Multiply a `TimeDelta` with a i32, returning `None` if overflow occurred.
405    #[must_use]
406    pub const fn checked_mul(&self, rhs: i32) -> Option<TimeDelta> {
407        // Multiply nanoseconds as i64, because it cannot overflow that way.
408        let total_nanos = self.nanos as i64 * rhs as i64;
409        let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
410        // Multiply seconds as i128 to prevent overflow
411        let secs: i128 = self.secs as i128 * rhs as i128 + extra_secs as i128;
412        if secs <= i64::MIN as i128 || secs >= i64::MAX as i128 {
413            return None;
414        };
415        Some(TimeDelta { secs: secs as i64, nanos: nanos as i32 })
416    }
417
418    /// Divide a `TimeDelta` with a i32, returning `None` if dividing by 0.
419    #[must_use]
420    pub const fn checked_div(&self, rhs: i32) -> Option<TimeDelta> {
421        if rhs == 0 {
422            return None;
423        }
424        let secs = self.secs / rhs as i64;
425        let carry = self.secs % rhs as i64;
426        let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
427        let nanos = self.nanos / rhs + extra_nanos as i32;
428
429        let (secs, nanos) = match nanos {
430            i32::MIN..=-1 => (secs - 1, nanos + NANOS_PER_SEC),
431            NANOS_PER_SEC..=i32::MAX => (secs + 1, nanos - NANOS_PER_SEC),
432            _ => (secs, nanos),
433        };
434
435        Some(TimeDelta { secs, nanos })
436    }
437
438    /// Returns the `TimeDelta` as an absolute (non-negative) value.
439    #[inline]
440    pub const fn abs(&self) -> TimeDelta {
441        if self.secs < 0 && self.nanos != 0 {
442            TimeDelta { secs: (self.secs + 1).abs(), nanos: NANOS_PER_SEC - self.nanos }
443        } else {
444            TimeDelta { secs: self.secs.abs(), nanos: self.nanos }
445        }
446    }
447
448    /// The minimum possible `TimeDelta`: `-i64::MAX` milliseconds.
449    #[deprecated(since = "0.4.39", note = "Use `TimeDelta::MIN` instead")]
450    #[inline]
451    pub const fn min_value() -> TimeDelta {
452        MIN
453    }
454
455    /// The maximum possible `TimeDelta`: `i64::MAX` milliseconds.
456    #[deprecated(since = "0.4.39", note = "Use `TimeDelta::MAX` instead")]
457    #[inline]
458    pub const fn max_value() -> TimeDelta {
459        MAX
460    }
461
462    /// A `TimeDelta` where the stored seconds and nanoseconds are equal to zero.
463    #[inline]
464    pub const fn zero() -> TimeDelta {
465        TimeDelta { secs: 0, nanos: 0 }
466    }
467
468    /// Returns `true` if the `TimeDelta` equals `TimeDelta::zero()`.
469    #[inline]
470    pub const fn is_zero(&self) -> bool {
471        self.secs == 0 && self.nanos == 0
472    }
473
474    /// Creates a `TimeDelta` object from `std::time::Duration`
475    ///
476    /// This function errors when original duration is larger than the maximum
477    /// value supported for this type.
478    pub const fn from_std(duration: Duration) -> Result<TimeDelta, OutOfRangeError> {
479        // We need to check secs as u64 before coercing to i64
480        if duration.as_secs() > MAX.secs as u64 {
481            return Err(OutOfRangeError(()));
482        }
483        match TimeDelta::new(duration.as_secs() as i64, duration.subsec_nanos()) {
484            Some(d) => Ok(d),
485            None => Err(OutOfRangeError(())),
486        }
487    }
488
489    /// Creates a `std::time::Duration` object from a `TimeDelta`.
490    ///
491    /// This function errors when duration is less than zero. As standard
492    /// library implementation is limited to non-negative values.
493    pub const fn to_std(&self) -> Result<Duration, OutOfRangeError> {
494        if self.secs < 0 {
495            return Err(OutOfRangeError(()));
496        }
497        Ok(Duration::new(self.secs as u64, self.nanos as u32))
498    }
499
500    /// This duplicates `Neg::neg` because trait methods can't be const yet.
501    pub(crate) const fn neg(self) -> TimeDelta {
502        let (secs_diff, nanos) = match self.nanos {
503            0 => (0, 0),
504            nanos => (1, NANOS_PER_SEC - nanos),
505        };
506        TimeDelta { secs: -self.secs - secs_diff, nanos }
507    }
508
509    /// The minimum possible `TimeDelta`: `-i64::MAX` milliseconds.
510    pub const MIN: Self = MIN;
511
512    /// The maximum possible `TimeDelta`: `i64::MAX` milliseconds.
513    pub const MAX: Self = MAX;
514}
515
516impl Neg for TimeDelta {
517    type Output = TimeDelta;
518
519    #[inline]
520    #[track_caller]
521    fn neg(self) -> TimeDelta {
522        let (secs_diff, nanos) = match self.nanos {
523            0 => (0, 0),
524            nanos => (1, NANOS_PER_SEC - nanos),
525        };
526        TimeDelta { secs: -self.secs - secs_diff, nanos }
527    }
528}
529
530impl Add for TimeDelta {
531    type Output = TimeDelta;
532
533    #[track_caller]
534    fn add(self, rhs: TimeDelta) -> TimeDelta {
535        self.checked_add(&rhs).expect("`TimeDelta + TimeDelta` overflowed")
536    }
537}
538
539impl Sub for TimeDelta {
540    type Output = TimeDelta;
541
542    #[track_caller]
543    fn sub(self, rhs: TimeDelta) -> TimeDelta {
544        self.checked_sub(&rhs).expect("`TimeDelta - TimeDelta` overflowed")
545    }
546}
547
548impl AddAssign for TimeDelta {
549    #[track_caller]
550    fn add_assign(&mut self, rhs: TimeDelta) {
551        let new = self.checked_add(&rhs).expect("`TimeDelta + TimeDelta` overflowed");
552        *self = new;
553    }
554}
555
556impl SubAssign for TimeDelta {
557    #[track_caller]
558    fn sub_assign(&mut self, rhs: TimeDelta) {
559        let new = self.checked_sub(&rhs).expect("`TimeDelta - TimeDelta` overflowed");
560        *self = new;
561    }
562}
563
564impl Mul<i32> for TimeDelta {
565    type Output = TimeDelta;
566
567    #[track_caller]
568    fn mul(self, rhs: i32) -> TimeDelta {
569        self.checked_mul(rhs).expect("`TimeDelta * i32` overflowed")
570    }
571}
572
573impl Div<i32> for TimeDelta {
574    type Output = TimeDelta;
575
576    #[track_caller]
577    fn div(self, rhs: i32) -> TimeDelta {
578        self.checked_div(rhs).expect("`i32` is zero")
579    }
580}
581
582impl<'a> core::iter::Sum<&'a TimeDelta> for TimeDelta {
583    fn sum<I: Iterator<Item = &'a TimeDelta>>(iter: I) -> TimeDelta {
584        iter.fold(TimeDelta::zero(), |acc, x| acc + *x)
585    }
586}
587
588impl core::iter::Sum<TimeDelta> for TimeDelta {
589    fn sum<I: Iterator<Item = TimeDelta>>(iter: I) -> TimeDelta {
590        iter.fold(TimeDelta::zero(), |acc, x| acc + x)
591    }
592}
593
594impl fmt::Display for TimeDelta {
595    /// Format a `TimeDelta` using the [ISO 8601] format
596    ///
597    /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601#Durations
598    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
599        // technically speaking, negative duration is not valid ISO 8601,
600        // but we need to print it anyway.
601        let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
602
603        write!(f, "{sign}P")?;
604        // Plenty of ways to encode an empty string. `P0D` is short and not too strange.
605        if abs.secs == 0 && abs.nanos == 0 {
606            return f.write_str("0D");
607        }
608
609        f.write_fmt(format_args!("T{}", abs.secs))?;
610
611        if abs.nanos > 0 {
612            // Count the number of significant digits, while removing all trailing zero's.
613            let mut figures = 9usize;
614            let mut fraction_digits = abs.nanos;
615            loop {
616                let div = fraction_digits / 10;
617                let last_digit = fraction_digits % 10;
618                if last_digit != 0 {
619                    break;
620                }
621                fraction_digits = div;
622                figures -= 1;
623            }
624            f.write_fmt(format_args!(".{fraction_digits:0figures$}"))?;
625        }
626        f.write_str("S")?;
627        Ok(())
628    }
629}
630
631/// Represents error when converting `TimeDelta` to/from a standard library
632/// implementation
633///
634/// The `std::time::Duration` supports a range from zero to `u64::MAX`
635/// *seconds*, while this module supports signed range of up to
636/// `i64::MAX` of *milliseconds*.
637#[derive(Debug, Clone, Copy, PartialEq, Eq)]
638#[cfg_attr(feature = "defmt", derive(defmt::Format))]
639pub struct OutOfRangeError(());
640
641impl fmt::Display for OutOfRangeError {
642    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
643        write!(f, "Source duration value is out of range for the target type")
644    }
645}
646
647impl Error for OutOfRangeError {
648    #[allow(deprecated)]
649    fn description(&self) -> &str {
650        "out of range error"
651    }
652}
653
654#[inline]
655const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
656    (this.div_euclid(other), this.rem_euclid(other))
657}
658
659#[cfg(all(feature = "arbitrary", feature = "std"))]
660impl arbitrary::Arbitrary<'_> for TimeDelta {
661    fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<TimeDelta> {
662        const MIN_SECS: i64 = -i64::MAX / MILLIS_PER_SEC - 1;
663        const MAX_SECS: i64 = i64::MAX / MILLIS_PER_SEC;
664
665        let secs: i64 = u.int_in_range(MIN_SECS..=MAX_SECS)?;
666        let nanos: i32 = u.int_in_range(0..=(NANOS_PER_SEC - 1))?;
667        let duration = TimeDelta { secs, nanos };
668
669        if duration < MIN || duration > MAX {
670            Err(arbitrary::Error::IncorrectFormat)
671        } else {
672            Ok(duration)
673        }
674    }
675}
676
677#[cfg(feature = "serde")]
678mod serde {
679    use super::TimeDelta;
680    use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error};
681
682    impl Serialize for TimeDelta {
683        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
684            <(i64, i32) as Serialize>::serialize(&(self.secs, self.nanos), serializer)
685        }
686    }
687
688    impl<'de> Deserialize<'de> for TimeDelta {
689        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
690            let (secs, nanos) = <(i64, i32) as Deserialize>::deserialize(deserializer)?;
691            TimeDelta::new(secs, nanos as u32).ok_or(Error::custom("TimeDelta out of bounds"))
692        }
693    }
694
695    #[cfg(test)]
696    mod tests {
697        use super::{super::MAX, TimeDelta};
698
699        #[test]
700        fn test_serde() {
701            let duration = TimeDelta::new(123, 456).unwrap();
702            assert_eq!(
703                serde_json::from_value::<TimeDelta>(serde_json::to_value(duration).unwrap())
704                    .unwrap(),
705                duration
706            );
707        }
708
709        #[test]
710        #[should_panic(expected = "TimeDelta out of bounds")]
711        fn test_serde_oob_panic() {
712            let _ =
713                serde_json::from_value::<TimeDelta>(serde_json::json!([MAX.secs + 1, 0])).unwrap();
714        }
715    }
716}
717
718#[cfg(test)]
719mod tests {
720    use super::OutOfRangeError;
721    use super::{MAX, MIN, TimeDelta};
722    use crate::expect;
723    use core::time::Duration;
724
725    #[test]
726    fn test_duration() {
727        let days = |d| TimeDelta::try_days(d).unwrap();
728        let seconds = |s| TimeDelta::try_seconds(s).unwrap();
729
730        assert!(seconds(1) != TimeDelta::zero());
731        assert_eq!(seconds(1) + seconds(2), seconds(3));
732        assert_eq!(seconds(86_399) + seconds(4), days(1) + seconds(3));
733        assert_eq!(days(10) - seconds(1000), seconds(863_000));
734        assert_eq!(days(10) - seconds(1_000_000), seconds(-136_000));
735        assert_eq!(
736            days(2) + seconds(86_399) + TimeDelta::nanoseconds(1_234_567_890),
737            days(3) + TimeDelta::nanoseconds(234_567_890)
738        );
739        assert_eq!(-days(3), days(-3));
740        assert_eq!(-(days(3) + seconds(70)), days(-4) + seconds(86_400 - 70));
741
742        let mut d = TimeDelta::default();
743        d += TimeDelta::try_minutes(1).unwrap();
744        d -= seconds(30);
745        assert_eq!(d, seconds(30));
746    }
747
748    #[test]
749    fn test_duration_num_days() {
750        assert_eq!(TimeDelta::zero().num_days(), 0);
751        assert_eq!(TimeDelta::try_days(1).unwrap().num_days(), 1);
752        assert_eq!(TimeDelta::try_days(-1).unwrap().num_days(), -1);
753        assert_eq!(TimeDelta::try_seconds(86_399).unwrap().num_days(), 0);
754        assert_eq!(TimeDelta::try_seconds(86_401).unwrap().num_days(), 1);
755        assert_eq!(TimeDelta::try_seconds(-86_399).unwrap().num_days(), 0);
756        assert_eq!(TimeDelta::try_seconds(-86_401).unwrap().num_days(), -1);
757        assert_eq!(TimeDelta::try_days(i32::MAX as i64).unwrap().num_days(), i32::MAX as i64);
758        assert_eq!(TimeDelta::try_days(i32::MIN as i64).unwrap().num_days(), i32::MIN as i64);
759    }
760
761    #[test]
762    fn test_duration_num_seconds() {
763        assert_eq!(TimeDelta::zero().num_seconds(), 0);
764        assert_eq!(TimeDelta::try_seconds(1).unwrap().num_seconds(), 1);
765        assert_eq!(TimeDelta::try_seconds(-1).unwrap().num_seconds(), -1);
766        assert_eq!(TimeDelta::try_milliseconds(999).unwrap().num_seconds(), 0);
767        assert_eq!(TimeDelta::try_milliseconds(1001).unwrap().num_seconds(), 1);
768        assert_eq!(TimeDelta::try_milliseconds(-999).unwrap().num_seconds(), 0);
769        assert_eq!(TimeDelta::try_milliseconds(-1001).unwrap().num_seconds(), -1);
770    }
771
772    #[test]
773    fn test_duration_seconds_max_allowed() {
774        let duration = TimeDelta::try_seconds(i64::MAX / 1_000).unwrap();
775        assert_eq!(duration.num_seconds(), i64::MAX / 1_000);
776        assert_eq!(
777            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
778            i64::MAX as i128 / 1_000 * 1_000_000_000
779        );
780    }
781
782    #[test]
783    fn test_duration_seconds_max_overflow() {
784        assert!(TimeDelta::try_seconds(i64::MAX / 1_000 + 1).is_none());
785    }
786
787    #[test]
788    #[should_panic(expected = "TimeDelta::seconds out of bounds")]
789    fn test_duration_seconds_max_overflow_panic() {
790        let _ = TimeDelta::seconds(i64::MAX / 1_000 + 1);
791    }
792
793    #[test]
794    fn test_duration_seconds_min_allowed() {
795        let duration = TimeDelta::try_seconds(i64::MIN / 1_000).unwrap(); // Same as -i64::MAX / 1_000 due to rounding
796        assert_eq!(duration.num_seconds(), i64::MIN / 1_000); // Same as -i64::MAX / 1_000 due to rounding
797        assert_eq!(
798            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
799            -i64::MAX as i128 / 1_000 * 1_000_000_000
800        );
801    }
802
803    #[test]
804    fn test_duration_seconds_min_underflow() {
805        assert!(TimeDelta::try_seconds(-i64::MAX / 1_000 - 1).is_none());
806    }
807
808    #[test]
809    #[should_panic(expected = "TimeDelta::seconds out of bounds")]
810    fn test_duration_seconds_min_underflow_panic() {
811        let _ = TimeDelta::seconds(-i64::MAX / 1_000 - 1);
812    }
813
814    #[test]
815    fn test_duration_as_seconds_f64() {
816        assert_eq!(TimeDelta::seconds(1).as_seconds_f64(), 1.0);
817        assert_eq!(TimeDelta::seconds(-1).as_seconds_f64(), -1.0);
818        assert_eq!(TimeDelta::seconds(100).as_seconds_f64(), 100.0);
819        assert_eq!(TimeDelta::seconds(-100).as_seconds_f64(), -100.0);
820
821        assert_eq!(TimeDelta::milliseconds(500).as_seconds_f64(), 0.5);
822        assert_eq!(TimeDelta::milliseconds(-500).as_seconds_f64(), -0.5);
823        assert_eq!(TimeDelta::milliseconds(1_500).as_seconds_f64(), 1.5);
824        assert_eq!(TimeDelta::milliseconds(-1_500).as_seconds_f64(), -1.5);
825    }
826
827    #[test]
828    fn test_duration_as_seconds_f32() {
829        assert_eq!(TimeDelta::seconds(1).as_seconds_f32(), 1.0);
830        assert_eq!(TimeDelta::seconds(-1).as_seconds_f32(), -1.0);
831        assert_eq!(TimeDelta::seconds(100).as_seconds_f32(), 100.0);
832        assert_eq!(TimeDelta::seconds(-100).as_seconds_f32(), -100.0);
833
834        assert_eq!(TimeDelta::milliseconds(500).as_seconds_f32(), 0.5);
835        assert_eq!(TimeDelta::milliseconds(-500).as_seconds_f32(), -0.5);
836        assert_eq!(TimeDelta::milliseconds(1_500).as_seconds_f32(), 1.5);
837        assert_eq!(TimeDelta::milliseconds(-1_500).as_seconds_f32(), -1.5);
838    }
839
840    #[test]
841    fn test_duration_subsec_nanos() {
842        assert_eq!(TimeDelta::zero().subsec_nanos(), 0);
843        assert_eq!(TimeDelta::nanoseconds(1).subsec_nanos(), 1);
844        assert_eq!(TimeDelta::nanoseconds(-1).subsec_nanos(), -1);
845        assert_eq!(TimeDelta::seconds(1).subsec_nanos(), 0);
846        assert_eq!(TimeDelta::nanoseconds(1_000_000_001).subsec_nanos(), 1);
847    }
848
849    #[test]
850    fn test_duration_subsec_micros() {
851        assert_eq!(TimeDelta::zero().subsec_micros(), 0);
852        assert_eq!(TimeDelta::microseconds(1).subsec_micros(), 1);
853        assert_eq!(TimeDelta::microseconds(-1).subsec_micros(), -1);
854        assert_eq!(TimeDelta::seconds(1).subsec_micros(), 0);
855        assert_eq!(TimeDelta::microseconds(1_000_001).subsec_micros(), 1);
856        assert_eq!(TimeDelta::nanoseconds(1_000_001_999).subsec_micros(), 1);
857    }
858
859    #[test]
860    fn test_duration_subsec_millis() {
861        assert_eq!(TimeDelta::zero().subsec_millis(), 0);
862        assert_eq!(TimeDelta::milliseconds(1).subsec_millis(), 1);
863        assert_eq!(TimeDelta::milliseconds(-1).subsec_millis(), -1);
864        assert_eq!(TimeDelta::seconds(1).subsec_millis(), 0);
865        assert_eq!(TimeDelta::milliseconds(1_001).subsec_millis(), 1);
866        assert_eq!(TimeDelta::microseconds(1_001_999).subsec_millis(), 1);
867    }
868
869    #[test]
870    fn test_duration_num_milliseconds() {
871        assert_eq!(TimeDelta::zero().num_milliseconds(), 0);
872        assert_eq!(TimeDelta::try_milliseconds(1).unwrap().num_milliseconds(), 1);
873        assert_eq!(TimeDelta::try_milliseconds(-1).unwrap().num_milliseconds(), -1);
874        assert_eq!(TimeDelta::microseconds(999).num_milliseconds(), 0);
875        assert_eq!(TimeDelta::microseconds(1001).num_milliseconds(), 1);
876        assert_eq!(TimeDelta::microseconds(-999).num_milliseconds(), 0);
877        assert_eq!(TimeDelta::microseconds(-1001).num_milliseconds(), -1);
878    }
879
880    #[test]
881    fn test_duration_milliseconds_max_allowed() {
882        // The maximum number of milliseconds acceptable through the constructor is
883        // equal to the number that can be stored in a TimeDelta.
884        let duration = TimeDelta::try_milliseconds(i64::MAX).unwrap();
885        assert_eq!(duration.num_milliseconds(), i64::MAX);
886        assert_eq!(
887            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
888            i64::MAX as i128 * 1_000_000
889        );
890    }
891
892    #[test]
893    fn test_duration_milliseconds_max_overflow() {
894        // Here we ensure that trying to add one millisecond to the maximum storable
895        // value will fail.
896        assert!(
897            TimeDelta::try_milliseconds(i64::MAX)
898                .unwrap()
899                .checked_add(&TimeDelta::try_milliseconds(1).unwrap())
900                .is_none()
901        );
902    }
903
904    #[test]
905    fn test_duration_milliseconds_min_allowed() {
906        // The minimum number of milliseconds acceptable through the constructor is
907        // not equal to the number that can be stored in a TimeDelta - there is a
908        // difference of one (i64::MIN vs -i64::MAX).
909        let duration = TimeDelta::try_milliseconds(-i64::MAX).unwrap();
910        assert_eq!(duration.num_milliseconds(), -i64::MAX);
911        assert_eq!(
912            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
913            -i64::MAX as i128 * 1_000_000
914        );
915    }
916
917    #[test]
918    fn test_duration_milliseconds_min_underflow() {
919        // Here we ensure that trying to subtract one millisecond from the minimum
920        // storable value will fail.
921        assert!(
922            TimeDelta::try_milliseconds(-i64::MAX)
923                .unwrap()
924                .checked_sub(&TimeDelta::try_milliseconds(1).unwrap())
925                .is_none()
926        );
927    }
928
929    #[test]
930    #[should_panic(expected = "TimeDelta::milliseconds out of bounds")]
931    fn test_duration_milliseconds_min_underflow_panic() {
932        // Here we ensure that trying to create a value one millisecond below the
933        // minimum storable value will fail. This test is necessary because the
934        // storable range is -i64::MAX, but the constructor type of i64 will allow
935        // i64::MIN, which is one value below.
936        let _ = TimeDelta::milliseconds(i64::MIN); // Same as -i64::MAX - 1
937    }
938
939    #[test]
940    fn test_duration_num_microseconds() {
941        assert_eq!(TimeDelta::zero().num_microseconds(), Some(0));
942        assert_eq!(TimeDelta::microseconds(1).num_microseconds(), Some(1));
943        assert_eq!(TimeDelta::microseconds(-1).num_microseconds(), Some(-1));
944        assert_eq!(TimeDelta::nanoseconds(999).num_microseconds(), Some(0));
945        assert_eq!(TimeDelta::nanoseconds(1001).num_microseconds(), Some(1));
946        assert_eq!(TimeDelta::nanoseconds(-999).num_microseconds(), Some(0));
947        assert_eq!(TimeDelta::nanoseconds(-1001).num_microseconds(), Some(-1));
948
949        // overflow checks
950        const MICROS_PER_DAY: i64 = 86_400_000_000;
951        assert_eq!(
952            TimeDelta::try_days(i64::MAX / MICROS_PER_DAY).unwrap().num_microseconds(),
953            Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY)
954        );
955        assert_eq!(
956            TimeDelta::try_days(-i64::MAX / MICROS_PER_DAY).unwrap().num_microseconds(),
957            Some(-i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY)
958        );
959        assert_eq!(
960            TimeDelta::try_days(i64::MAX / MICROS_PER_DAY + 1).unwrap().num_microseconds(),
961            None
962        );
963        assert_eq!(
964            TimeDelta::try_days(-i64::MAX / MICROS_PER_DAY - 1).unwrap().num_microseconds(),
965            None
966        );
967    }
968    #[test]
969    fn test_duration_microseconds_max_allowed() {
970        // The number of microseconds acceptable through the constructor is far
971        // fewer than the number that can actually be stored in a TimeDelta, so this
972        // is not a particular insightful test.
973        let duration = TimeDelta::microseconds(i64::MAX);
974        assert_eq!(duration.num_microseconds(), Some(i64::MAX));
975        assert_eq!(
976            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
977            i64::MAX as i128 * 1_000
978        );
979        // Here we create a TimeDelta with the maximum possible number of
980        // microseconds by creating a TimeDelta with the maximum number of
981        // milliseconds and then checking that the number of microseconds matches
982        // the storage limit.
983        let duration = TimeDelta::try_milliseconds(i64::MAX).unwrap();
984        assert!(duration.num_microseconds().is_none());
985        assert_eq!(
986            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
987            i64::MAX as i128 * 1_000_000
988        );
989    }
990    #[test]
991    fn test_duration_microseconds_max_overflow() {
992        // This test establishes that a TimeDelta can store more microseconds than
993        // are representable through the return of duration.num_microseconds().
994        let duration = TimeDelta::microseconds(i64::MAX) + TimeDelta::microseconds(1);
995        assert!(duration.num_microseconds().is_none());
996        assert_eq!(
997            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
998            (i64::MAX as i128 + 1) * 1_000
999        );
1000        // Here we ensure that trying to add one microsecond to the maximum storable
1001        // value will fail.
1002        assert!(
1003            TimeDelta::try_milliseconds(i64::MAX)
1004                .unwrap()
1005                .checked_add(&TimeDelta::microseconds(1))
1006                .is_none()
1007        );
1008    }
1009    #[test]
1010    fn test_duration_microseconds_min_allowed() {
1011        // The number of microseconds acceptable through the constructor is far
1012        // fewer than the number that can actually be stored in a TimeDelta, so this
1013        // is not a particular insightful test.
1014        let duration = TimeDelta::microseconds(i64::MIN);
1015        assert_eq!(duration.num_microseconds(), Some(i64::MIN));
1016        assert_eq!(
1017            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1018            i64::MIN as i128 * 1_000
1019        );
1020        // Here we create a TimeDelta with the minimum possible number of
1021        // microseconds by creating a TimeDelta with the minimum number of
1022        // milliseconds and then checking that the number of microseconds matches
1023        // the storage limit.
1024        let duration = TimeDelta::try_milliseconds(-i64::MAX).unwrap();
1025        assert!(duration.num_microseconds().is_none());
1026        assert_eq!(
1027            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1028            -i64::MAX as i128 * 1_000_000
1029        );
1030    }
1031    #[test]
1032    fn test_duration_microseconds_min_underflow() {
1033        // This test establishes that a TimeDelta can store more microseconds than
1034        // are representable through the return of duration.num_microseconds().
1035        let duration = TimeDelta::microseconds(i64::MIN) - TimeDelta::microseconds(1);
1036        assert!(duration.num_microseconds().is_none());
1037        assert_eq!(
1038            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1039            (i64::MIN as i128 - 1) * 1_000
1040        );
1041        // Here we ensure that trying to subtract one microsecond from the minimum
1042        // storable value will fail.
1043        assert!(
1044            TimeDelta::try_milliseconds(-i64::MAX)
1045                .unwrap()
1046                .checked_sub(&TimeDelta::microseconds(1))
1047                .is_none()
1048        );
1049    }
1050
1051    #[test]
1052    fn test_duration_num_nanoseconds() {
1053        assert_eq!(TimeDelta::zero().num_nanoseconds(), Some(0));
1054        assert_eq!(TimeDelta::nanoseconds(1).num_nanoseconds(), Some(1));
1055        assert_eq!(TimeDelta::nanoseconds(-1).num_nanoseconds(), Some(-1));
1056
1057        // overflow checks
1058        const NANOS_PER_DAY: i64 = 86_400_000_000_000;
1059        assert_eq!(
1060            TimeDelta::try_days(i64::MAX / NANOS_PER_DAY).unwrap().num_nanoseconds(),
1061            Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY)
1062        );
1063        assert_eq!(
1064            TimeDelta::try_days(-i64::MAX / NANOS_PER_DAY).unwrap().num_nanoseconds(),
1065            Some(-i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY)
1066        );
1067        assert_eq!(
1068            TimeDelta::try_days(i64::MAX / NANOS_PER_DAY + 1).unwrap().num_nanoseconds(),
1069            None
1070        );
1071        assert_eq!(
1072            TimeDelta::try_days(-i64::MAX / NANOS_PER_DAY - 1).unwrap().num_nanoseconds(),
1073            None
1074        );
1075    }
1076    #[test]
1077    fn test_duration_nanoseconds_max_allowed() {
1078        // The number of nanoseconds acceptable through the constructor is far fewer
1079        // than the number that can actually be stored in a TimeDelta, so this is not
1080        // a particular insightful test.
1081        let duration = TimeDelta::nanoseconds(i64::MAX);
1082        assert_eq!(duration.num_nanoseconds(), Some(i64::MAX));
1083        assert_eq!(
1084            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1085            i64::MAX as i128
1086        );
1087        // Here we create a TimeDelta with the maximum possible number of nanoseconds
1088        // by creating a TimeDelta with the maximum number of milliseconds and then
1089        // checking that the number of nanoseconds matches the storage limit.
1090        let duration = TimeDelta::try_milliseconds(i64::MAX).unwrap();
1091        assert!(duration.num_nanoseconds().is_none());
1092        assert_eq!(
1093            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1094            i64::MAX as i128 * 1_000_000
1095        );
1096    }
1097
1098    #[test]
1099    fn test_duration_nanoseconds_max_overflow() {
1100        // This test establishes that a TimeDelta can store more nanoseconds than are
1101        // representable through the return of duration.num_nanoseconds().
1102        let duration = TimeDelta::nanoseconds(i64::MAX) + TimeDelta::nanoseconds(1);
1103        assert!(duration.num_nanoseconds().is_none());
1104        assert_eq!(
1105            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1106            i64::MAX as i128 + 1
1107        );
1108        // Here we ensure that trying to add one nanosecond to the maximum storable
1109        // value will fail.
1110        assert!(
1111            TimeDelta::try_milliseconds(i64::MAX)
1112                .unwrap()
1113                .checked_add(&TimeDelta::nanoseconds(1))
1114                .is_none()
1115        );
1116    }
1117
1118    #[test]
1119    fn test_duration_nanoseconds_min_allowed() {
1120        // The number of nanoseconds acceptable through the constructor is far fewer
1121        // than the number that can actually be stored in a TimeDelta, so this is not
1122        // a particular insightful test.
1123        let duration = TimeDelta::nanoseconds(i64::MIN);
1124        assert_eq!(duration.num_nanoseconds(), Some(i64::MIN));
1125        assert_eq!(
1126            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1127            i64::MIN as i128
1128        );
1129        // Here we create a TimeDelta with the minimum possible number of nanoseconds
1130        // by creating a TimeDelta with the minimum number of milliseconds and then
1131        // checking that the number of nanoseconds matches the storage limit.
1132        let duration = TimeDelta::try_milliseconds(-i64::MAX).unwrap();
1133        assert!(duration.num_nanoseconds().is_none());
1134        assert_eq!(
1135            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1136            -i64::MAX as i128 * 1_000_000
1137        );
1138    }
1139
1140    #[test]
1141    fn test_duration_nanoseconds_min_underflow() {
1142        // This test establishes that a TimeDelta can store more nanoseconds than are
1143        // representable through the return of duration.num_nanoseconds().
1144        let duration = TimeDelta::nanoseconds(i64::MIN) - TimeDelta::nanoseconds(1);
1145        assert!(duration.num_nanoseconds().is_none());
1146        assert_eq!(
1147            duration.secs as i128 * 1_000_000_000 + duration.nanos as i128,
1148            i64::MIN as i128 - 1
1149        );
1150        // Here we ensure that trying to subtract one nanosecond from the minimum
1151        // storable value will fail.
1152        assert!(
1153            TimeDelta::try_milliseconds(-i64::MAX)
1154                .unwrap()
1155                .checked_sub(&TimeDelta::nanoseconds(1))
1156                .is_none()
1157        );
1158    }
1159
1160    #[test]
1161    fn test_max() {
1162        assert_eq!(
1163            MAX.secs as i128 * 1_000_000_000 + MAX.nanos as i128,
1164            i64::MAX as i128 * 1_000_000
1165        );
1166        assert_eq!(MAX, TimeDelta::try_milliseconds(i64::MAX).unwrap());
1167        assert_eq!(MAX.num_milliseconds(), i64::MAX);
1168        assert_eq!(MAX.num_microseconds(), None);
1169        assert_eq!(MAX.num_nanoseconds(), None);
1170    }
1171
1172    #[test]
1173    fn test_min() {
1174        assert_eq!(
1175            MIN.secs as i128 * 1_000_000_000 + MIN.nanos as i128,
1176            -i64::MAX as i128 * 1_000_000
1177        );
1178        assert_eq!(MIN, TimeDelta::try_milliseconds(-i64::MAX).unwrap());
1179        assert_eq!(MIN.num_milliseconds(), -i64::MAX);
1180        assert_eq!(MIN.num_microseconds(), None);
1181        assert_eq!(MIN.num_nanoseconds(), None);
1182    }
1183
1184    #[test]
1185    fn test_duration_ord() {
1186        let milliseconds = |ms| TimeDelta::try_milliseconds(ms).unwrap();
1187
1188        assert!(milliseconds(1) < milliseconds(2));
1189        assert!(milliseconds(2) > milliseconds(1));
1190        assert!(milliseconds(-1) > milliseconds(-2));
1191        assert!(milliseconds(-2) < milliseconds(-1));
1192        assert!(milliseconds(-1) < milliseconds(1));
1193        assert!(milliseconds(1) > milliseconds(-1));
1194        assert!(milliseconds(0) < milliseconds(1));
1195        assert!(milliseconds(0) > milliseconds(-1));
1196        assert!(milliseconds(1_001) < milliseconds(1_002));
1197        assert!(milliseconds(-1_001) > milliseconds(-1_002));
1198        assert!(TimeDelta::nanoseconds(1_234_567_890) < TimeDelta::nanoseconds(1_234_567_891));
1199        assert!(TimeDelta::nanoseconds(-1_234_567_890) > TimeDelta::nanoseconds(-1_234_567_891));
1200        assert!(milliseconds(i64::MAX) > milliseconds(i64::MAX - 1));
1201        assert!(milliseconds(-i64::MAX) < milliseconds(-i64::MAX + 1));
1202    }
1203
1204    #[test]
1205    fn test_duration_checked_ops() {
1206        let milliseconds = |ms| TimeDelta::try_milliseconds(ms).unwrap();
1207        let seconds = |s| TimeDelta::try_seconds(s).unwrap();
1208
1209        assert_eq!(
1210            milliseconds(i64::MAX).checked_add(&milliseconds(0)),
1211            Some(milliseconds(i64::MAX))
1212        );
1213        assert_eq!(
1214            milliseconds(i64::MAX - 1).checked_add(&TimeDelta::microseconds(999)),
1215            Some(milliseconds(i64::MAX - 2) + TimeDelta::microseconds(1999))
1216        );
1217        assert!(milliseconds(i64::MAX).checked_add(&TimeDelta::microseconds(1000)).is_none());
1218        assert!(milliseconds(i64::MAX).checked_add(&TimeDelta::nanoseconds(1)).is_none());
1219
1220        assert_eq!(
1221            milliseconds(-i64::MAX).checked_sub(&milliseconds(0)),
1222            Some(milliseconds(-i64::MAX))
1223        );
1224        assert_eq!(
1225            milliseconds(-i64::MAX + 1).checked_sub(&TimeDelta::microseconds(999)),
1226            Some(milliseconds(-i64::MAX + 2) - TimeDelta::microseconds(1999))
1227        );
1228        assert!(milliseconds(-i64::MAX).checked_sub(&milliseconds(1)).is_none());
1229        assert!(milliseconds(-i64::MAX).checked_sub(&TimeDelta::nanoseconds(1)).is_none());
1230
1231        assert!(seconds(i64::MAX / 1000).checked_mul(2000).is_none());
1232        assert!(seconds(i64::MIN / 1000).checked_mul(2000).is_none());
1233        assert!(seconds(1).checked_div(0).is_none());
1234    }
1235
1236    #[test]
1237    fn test_duration_abs() {
1238        let milliseconds = |ms| TimeDelta::try_milliseconds(ms).unwrap();
1239
1240        assert_eq!(milliseconds(1300).abs(), milliseconds(1300));
1241        assert_eq!(milliseconds(1000).abs(), milliseconds(1000));
1242        assert_eq!(milliseconds(300).abs(), milliseconds(300));
1243        assert_eq!(milliseconds(0).abs(), milliseconds(0));
1244        assert_eq!(milliseconds(-300).abs(), milliseconds(300));
1245        assert_eq!(milliseconds(-700).abs(), milliseconds(700));
1246        assert_eq!(milliseconds(-1000).abs(), milliseconds(1000));
1247        assert_eq!(milliseconds(-1300).abs(), milliseconds(1300));
1248        assert_eq!(milliseconds(-1700).abs(), milliseconds(1700));
1249        assert_eq!(milliseconds(-i64::MAX).abs(), milliseconds(i64::MAX));
1250    }
1251
1252    #[test]
1253    #[allow(clippy::erasing_op)]
1254    fn test_duration_mul() {
1255        assert_eq!(TimeDelta::zero() * i32::MAX, TimeDelta::zero());
1256        assert_eq!(TimeDelta::zero() * i32::MIN, TimeDelta::zero());
1257        assert_eq!(TimeDelta::nanoseconds(1) * 0, TimeDelta::zero());
1258        assert_eq!(TimeDelta::nanoseconds(1) * 1, TimeDelta::nanoseconds(1));
1259        assert_eq!(TimeDelta::nanoseconds(1) * 1_000_000_000, TimeDelta::try_seconds(1).unwrap());
1260        assert_eq!(TimeDelta::nanoseconds(1) * -1_000_000_000, -TimeDelta::try_seconds(1).unwrap());
1261        assert_eq!(-TimeDelta::nanoseconds(1) * 1_000_000_000, -TimeDelta::try_seconds(1).unwrap());
1262        assert_eq!(
1263            TimeDelta::nanoseconds(30) * 333_333_333,
1264            TimeDelta::try_seconds(10).unwrap() - TimeDelta::nanoseconds(10)
1265        );
1266        assert_eq!(
1267            (TimeDelta::nanoseconds(1)
1268                + TimeDelta::try_seconds(1).unwrap()
1269                + TimeDelta::try_days(1).unwrap())
1270                * 3,
1271            TimeDelta::nanoseconds(3)
1272                + TimeDelta::try_seconds(3).unwrap()
1273                + TimeDelta::try_days(3).unwrap()
1274        );
1275        assert_eq!(
1276            TimeDelta::try_milliseconds(1500).unwrap() * -2,
1277            TimeDelta::try_seconds(-3).unwrap()
1278        );
1279        assert_eq!(
1280            TimeDelta::try_milliseconds(-1500).unwrap() * 2,
1281            TimeDelta::try_seconds(-3).unwrap()
1282        );
1283    }
1284
1285    #[test]
1286    fn test_duration_div() {
1287        assert_eq!(TimeDelta::zero() / i32::MAX, TimeDelta::zero());
1288        assert_eq!(TimeDelta::zero() / i32::MIN, TimeDelta::zero());
1289        assert_eq!(TimeDelta::nanoseconds(123_456_789) / 1, TimeDelta::nanoseconds(123_456_789));
1290        assert_eq!(TimeDelta::nanoseconds(123_456_789) / -1, -TimeDelta::nanoseconds(123_456_789));
1291        assert_eq!(-TimeDelta::nanoseconds(123_456_789) / -1, TimeDelta::nanoseconds(123_456_789));
1292        assert_eq!(-TimeDelta::nanoseconds(123_456_789) / 1, -TimeDelta::nanoseconds(123_456_789));
1293        assert_eq!(TimeDelta::try_seconds(1).unwrap() / 3, TimeDelta::nanoseconds(333_333_333));
1294        assert_eq!(TimeDelta::try_seconds(4).unwrap() / 3, TimeDelta::nanoseconds(1_333_333_333));
1295        assert_eq!(
1296            TimeDelta::try_seconds(-1).unwrap() / 2,
1297            TimeDelta::try_milliseconds(-500).unwrap()
1298        );
1299        assert_eq!(
1300            TimeDelta::try_seconds(1).unwrap() / -2,
1301            TimeDelta::try_milliseconds(-500).unwrap()
1302        );
1303        assert_eq!(
1304            TimeDelta::try_seconds(-1).unwrap() / -2,
1305            TimeDelta::try_milliseconds(500).unwrap()
1306        );
1307        assert_eq!(TimeDelta::try_seconds(-4).unwrap() / 3, TimeDelta::nanoseconds(-1_333_333_333));
1308        assert_eq!(TimeDelta::try_seconds(-4).unwrap() / -3, TimeDelta::nanoseconds(1_333_333_333));
1309    }
1310
1311    #[test]
1312    fn test_duration_sum() {
1313        let duration_list_1 = [TimeDelta::zero(), TimeDelta::try_seconds(1).unwrap()];
1314        let sum_1: TimeDelta = duration_list_1.iter().sum();
1315        assert_eq!(sum_1, TimeDelta::try_seconds(1).unwrap());
1316
1317        let duration_list_2 = [
1318            TimeDelta::zero(),
1319            TimeDelta::try_seconds(1).unwrap(),
1320            TimeDelta::try_seconds(6).unwrap(),
1321            TimeDelta::try_seconds(10).unwrap(),
1322        ];
1323        let sum_2: TimeDelta = duration_list_2.iter().sum();
1324        assert_eq!(sum_2, TimeDelta::try_seconds(17).unwrap());
1325
1326        let duration_arr = [
1327            TimeDelta::zero(),
1328            TimeDelta::try_seconds(1).unwrap(),
1329            TimeDelta::try_seconds(6).unwrap(),
1330            TimeDelta::try_seconds(10).unwrap(),
1331        ];
1332        let sum_3: TimeDelta = duration_arr.into_iter().sum();
1333        assert_eq!(sum_3, TimeDelta::try_seconds(17).unwrap());
1334    }
1335
1336    #[test]
1337    fn test_duration_fmt() {
1338        assert_eq!(TimeDelta::zero().to_string(), "P0D");
1339        assert_eq!(TimeDelta::try_days(42).unwrap().to_string(), "PT3628800S");
1340        assert_eq!(TimeDelta::try_days(-42).unwrap().to_string(), "-PT3628800S");
1341        assert_eq!(TimeDelta::try_seconds(42).unwrap().to_string(), "PT42S");
1342        assert_eq!(TimeDelta::try_milliseconds(42).unwrap().to_string(), "PT0.042S");
1343        assert_eq!(TimeDelta::microseconds(42).to_string(), "PT0.000042S");
1344        assert_eq!(TimeDelta::nanoseconds(42).to_string(), "PT0.000000042S");
1345        assert_eq!(
1346            (TimeDelta::try_days(7).unwrap() + TimeDelta::try_milliseconds(6543).unwrap())
1347                .to_string(),
1348            "PT604806.543S"
1349        );
1350        assert_eq!(TimeDelta::try_seconds(-86_401).unwrap().to_string(), "-PT86401S");
1351        assert_eq!(TimeDelta::nanoseconds(-1).to_string(), "-PT0.000000001S");
1352
1353        // the format specifier should have no effect on `TimeDelta`
1354        assert_eq!(
1355            format!(
1356                "{:30}",
1357                TimeDelta::try_days(1).unwrap() + TimeDelta::try_milliseconds(2345).unwrap()
1358            ),
1359            "PT86402.345S"
1360        );
1361    }
1362
1363    #[test]
1364    fn test_to_std() {
1365        assert_eq!(TimeDelta::try_seconds(1).unwrap().to_std(), Ok(Duration::new(1, 0)));
1366        assert_eq!(TimeDelta::try_seconds(86_401).unwrap().to_std(), Ok(Duration::new(86_401, 0)));
1367        assert_eq!(
1368            TimeDelta::try_milliseconds(123).unwrap().to_std(),
1369            Ok(Duration::new(0, 123_000_000))
1370        );
1371        assert_eq!(
1372            TimeDelta::try_milliseconds(123_765).unwrap().to_std(),
1373            Ok(Duration::new(123, 765_000_000))
1374        );
1375        assert_eq!(TimeDelta::nanoseconds(777).to_std(), Ok(Duration::new(0, 777)));
1376        assert_eq!(MAX.to_std(), Ok(Duration::new(9_223_372_036_854_775, 807_000_000)));
1377        assert_eq!(TimeDelta::try_seconds(-1).unwrap().to_std(), Err(OutOfRangeError(())));
1378        assert_eq!(TimeDelta::try_milliseconds(-1).unwrap().to_std(), Err(OutOfRangeError(())));
1379    }
1380
1381    #[test]
1382    fn test_from_std() {
1383        assert_eq!(
1384            Ok(TimeDelta::try_seconds(1).unwrap()),
1385            TimeDelta::from_std(Duration::new(1, 0))
1386        );
1387        assert_eq!(
1388            Ok(TimeDelta::try_seconds(86_401).unwrap()),
1389            TimeDelta::from_std(Duration::new(86_401, 0))
1390        );
1391        assert_eq!(
1392            Ok(TimeDelta::try_milliseconds(123).unwrap()),
1393            TimeDelta::from_std(Duration::new(0, 123_000_000))
1394        );
1395        assert_eq!(
1396            Ok(TimeDelta::try_milliseconds(123_765).unwrap()),
1397            TimeDelta::from_std(Duration::new(123, 765_000_000))
1398        );
1399        assert_eq!(Ok(TimeDelta::nanoseconds(777)), TimeDelta::from_std(Duration::new(0, 777)));
1400        assert_eq!(Ok(MAX), TimeDelta::from_std(Duration::new(9_223_372_036_854_775, 807_000_000)));
1401        assert_eq!(
1402            TimeDelta::from_std(Duration::new(9_223_372_036_854_776, 0)),
1403            Err(OutOfRangeError(()))
1404        );
1405        assert_eq!(
1406            TimeDelta::from_std(Duration::new(9_223_372_036_854_775, 807_000_001)),
1407            Err(OutOfRangeError(()))
1408        );
1409    }
1410
1411    #[test]
1412    fn test_duration_const() {
1413        const ONE_WEEK: TimeDelta = expect(TimeDelta::try_weeks(1), "");
1414        const ONE_DAY: TimeDelta = expect(TimeDelta::try_days(1), "");
1415        const ONE_HOUR: TimeDelta = expect(TimeDelta::try_hours(1), "");
1416        const ONE_MINUTE: TimeDelta = expect(TimeDelta::try_minutes(1), "");
1417        const ONE_SECOND: TimeDelta = expect(TimeDelta::try_seconds(1), "");
1418        const ONE_MILLI: TimeDelta = expect(TimeDelta::try_milliseconds(1), "");
1419        const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1);
1420        const ONE_NANO: TimeDelta = TimeDelta::nanoseconds(1);
1421        let combo: TimeDelta = ONE_WEEK
1422            + ONE_DAY
1423            + ONE_HOUR
1424            + ONE_MINUTE
1425            + ONE_SECOND
1426            + ONE_MILLI
1427            + ONE_MICRO
1428            + ONE_NANO;
1429
1430        assert!(ONE_WEEK != TimeDelta::zero());
1431        assert!(ONE_DAY != TimeDelta::zero());
1432        assert!(ONE_HOUR != TimeDelta::zero());
1433        assert!(ONE_MINUTE != TimeDelta::zero());
1434        assert!(ONE_SECOND != TimeDelta::zero());
1435        assert!(ONE_MILLI != TimeDelta::zero());
1436        assert!(ONE_MICRO != TimeDelta::zero());
1437        assert!(ONE_NANO != TimeDelta::zero());
1438        assert_eq!(
1439            combo,
1440            TimeDelta::try_seconds(86400 * 7 + 86400 + 3600 + 60 + 1).unwrap()
1441                + TimeDelta::nanoseconds(1 + 1_000 + 1_000_000)
1442        );
1443    }
1444
1445    #[test]
1446    #[cfg(feature = "rkyv-validation")]
1447    fn test_rkyv_validation() {
1448        let duration = TimeDelta::try_seconds(1).unwrap();
1449        let bytes = rkyv::to_bytes::<_, 16>(&duration).unwrap();
1450        assert_eq!(rkyv::from_bytes::<TimeDelta>(&bytes).unwrap(), duration);
1451    }
1452}