core/
time.rs

1#![stable(feature = "duration_core", since = "1.25.0")]
2
3//! Temporal quantification.
4//!
5//! # Examples:
6//!
7//! There are multiple ways to create a new [`Duration`]:
8//!
9//! ```
10//! # use std::time::Duration;
11//! let five_seconds = Duration::from_secs(5);
12//! assert_eq!(five_seconds, Duration::from_millis(5_000));
13//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
14//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
15//!
16//! let ten_seconds = Duration::from_secs(10);
17//! let seven_nanos = Duration::from_nanos(7);
18//! let total = ten_seconds + seven_nanos;
19//! assert_eq!(total, Duration::new(10, 7));
20//! ```
21
22use crate::fmt;
23use crate::iter::Sum;
24use crate::num::niche_types::Nanoseconds;
25use crate::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
26
27const NANOS_PER_SEC: u32 = 1_000_000_000;
28const NANOS_PER_MILLI: u32 = 1_000_000;
29const NANOS_PER_MICRO: u32 = 1_000;
30const MILLIS_PER_SEC: u64 = 1_000;
31const MICROS_PER_SEC: u64 = 1_000_000;
32#[unstable(feature = "duration_units", issue = "120301")]
33const SECS_PER_MINUTE: u64 = 60;
34#[unstable(feature = "duration_units", issue = "120301")]
35const MINS_PER_HOUR: u64 = 60;
36#[unstable(feature = "duration_units", issue = "120301")]
37const HOURS_PER_DAY: u64 = 24;
38#[unstable(feature = "duration_units", issue = "120301")]
39const DAYS_PER_WEEK: u64 = 7;
40
41/// A `Duration` type to represent a span of time, typically used for system
42/// timeouts.
43///
44/// Each `Duration` is composed of a whole number of seconds and a fractional part
45/// represented in nanoseconds. If the underlying system does not support
46/// nanosecond-level precision, APIs binding a system timeout will typically round up
47/// the number of nanoseconds.
48///
49/// [`Duration`]s implement many common traits, including [`Add`], [`Sub`], and other
50/// [`ops`] traits. It implements [`Default`] by returning a zero-length `Duration`.
51///
52/// [`ops`]: crate::ops
53///
54/// # Examples
55///
56/// ```
57/// use std::time::Duration;
58///
59/// let five_seconds = Duration::new(5, 0);
60/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
61///
62/// assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
63/// assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
64///
65/// let ten_millis = Duration::from_millis(10);
66/// ```
67///
68/// # Formatting `Duration` values
69///
70/// `Duration` intentionally does not have a `Display` impl, as there are a
71/// variety of ways to format spans of time for human readability. `Duration`
72/// provides a `Debug` impl that shows the full precision of the value.
73///
74/// The `Debug` output uses the non-ASCII "µs" suffix for microseconds. If your
75/// program output may appear in contexts that cannot rely on full Unicode
76/// compatibility, you may wish to format `Duration` objects yourself or use a
77/// crate to do so.
78#[stable(feature = "duration", since = "1.3.0")]
79#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
80#[rustc_diagnostic_item = "Duration"]
81pub struct Duration {
82    secs: u64,
83    nanos: Nanoseconds, // Always 0 <= nanos < NANOS_PER_SEC
84}
85
86impl Duration {
87    /// The duration of one second.
88    ///
89    /// # Examples
90    ///
91    /// ```
92    /// #![feature(duration_constants)]
93    /// use std::time::Duration;
94    ///
95    /// assert_eq!(Duration::SECOND, Duration::from_secs(1));
96    /// ```
97    #[unstable(feature = "duration_constants", issue = "57391")]
98    pub const SECOND: Duration = Duration::from_secs(1);
99
100    /// The duration of one millisecond.
101    ///
102    /// # Examples
103    ///
104    /// ```
105    /// #![feature(duration_constants)]
106    /// use std::time::Duration;
107    ///
108    /// assert_eq!(Duration::MILLISECOND, Duration::from_millis(1));
109    /// ```
110    #[unstable(feature = "duration_constants", issue = "57391")]
111    pub const MILLISECOND: Duration = Duration::from_millis(1);
112
113    /// The duration of one microsecond.
114    ///
115    /// # Examples
116    ///
117    /// ```
118    /// #![feature(duration_constants)]
119    /// use std::time::Duration;
120    ///
121    /// assert_eq!(Duration::MICROSECOND, Duration::from_micros(1));
122    /// ```
123    #[unstable(feature = "duration_constants", issue = "57391")]
124    pub const MICROSECOND: Duration = Duration::from_micros(1);
125
126    /// The duration of one nanosecond.
127    ///
128    /// # Examples
129    ///
130    /// ```
131    /// #![feature(duration_constants)]
132    /// use std::time::Duration;
133    ///
134    /// assert_eq!(Duration::NANOSECOND, Duration::from_nanos(1));
135    /// ```
136    #[unstable(feature = "duration_constants", issue = "57391")]
137    pub const NANOSECOND: Duration = Duration::from_nanos(1);
138
139    /// A duration of zero time.
140    ///
141    /// # Examples
142    ///
143    /// ```
144    /// use std::time::Duration;
145    ///
146    /// let duration = Duration::ZERO;
147    /// assert!(duration.is_zero());
148    /// assert_eq!(duration.as_nanos(), 0);
149    /// ```
150    #[stable(feature = "duration_zero", since = "1.53.0")]
151    pub const ZERO: Duration = Duration::from_nanos(0);
152
153    /// The maximum duration.
154    ///
155    /// May vary by platform as necessary. Must be able to contain the difference between
156    /// two instances of [`Instant`] or two instances of [`SystemTime`].
157    /// This constraint gives it a value of about 584,942,417,355 years in practice,
158    /// which is currently used on all platforms.
159    ///
160    /// # Examples
161    ///
162    /// ```
163    /// use std::time::Duration;
164    ///
165    /// assert_eq!(Duration::MAX, Duration::new(u64::MAX, 1_000_000_000 - 1));
166    /// ```
167    /// [`Instant`]: ../../std/time/struct.Instant.html
168    /// [`SystemTime`]: ../../std/time/struct.SystemTime.html
169    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
170    pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC - 1);
171
172    /// Creates a new `Duration` from the specified number of whole seconds and
173    /// additional nanoseconds.
174    ///
175    /// If the number of nanoseconds is greater than 1 billion (the number of
176    /// nanoseconds in a second), then it will carry over into the seconds provided.
177    ///
178    /// # Panics
179    ///
180    /// This constructor will panic if the carry from the nanoseconds overflows
181    /// the seconds counter.
182    ///
183    /// # Examples
184    ///
185    /// ```
186    /// use std::time::Duration;
187    ///
188    /// let five_seconds = Duration::new(5, 0);
189    /// ```
190    #[stable(feature = "duration", since = "1.3.0")]
191    #[inline]
192    #[must_use]
193    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
194    pub const fn new(secs: u64, nanos: u32) -> Duration {
195        if nanos < NANOS_PER_SEC {
196            // SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
197            Duration { secs, nanos: unsafe { Nanoseconds::new_unchecked(nanos) } }
198        } else {
199            let secs = secs
200                .checked_add((nanos / NANOS_PER_SEC) as u64)
201                .expect("overflow in Duration::new");
202            let nanos = nanos % NANOS_PER_SEC;
203            // SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
204            Duration { secs, nanos: unsafe { Nanoseconds::new_unchecked(nanos) } }
205        }
206    }
207
208    /// Creates a new `Duration` from the specified number of whole seconds.
209    ///
210    /// # Examples
211    ///
212    /// ```
213    /// use std::time::Duration;
214    ///
215    /// let duration = Duration::from_secs(5);
216    ///
217    /// assert_eq!(5, duration.as_secs());
218    /// assert_eq!(0, duration.subsec_nanos());
219    /// ```
220    #[stable(feature = "duration", since = "1.3.0")]
221    #[must_use]
222    #[inline]
223    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
224    pub const fn from_secs(secs: u64) -> Duration {
225        Duration { secs, nanos: Nanoseconds::ZERO }
226    }
227
228    /// Creates a new `Duration` from the specified number of milliseconds.
229    ///
230    /// # Examples
231    ///
232    /// ```
233    /// use std::time::Duration;
234    ///
235    /// let duration = Duration::from_millis(2_569);
236    ///
237    /// assert_eq!(2, duration.as_secs());
238    /// assert_eq!(569_000_000, duration.subsec_nanos());
239    /// ```
240    #[stable(feature = "duration", since = "1.3.0")]
241    #[must_use]
242    #[inline]
243    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
244    pub const fn from_millis(millis: u64) -> Duration {
245        let secs = millis / MILLIS_PER_SEC;
246        let subsec_millis = (millis % MILLIS_PER_SEC) as u32;
247        // SAFETY: (x % 1_000) * 1_000_000 < 1_000_000_000
248        //         => x % 1_000 < 1_000
249        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_millis * NANOS_PER_MILLI) };
250
251        Duration { secs, nanos: subsec_nanos }
252    }
253
254    /// Creates a new `Duration` from the specified number of microseconds.
255    ///
256    /// # Examples
257    ///
258    /// ```
259    /// use std::time::Duration;
260    ///
261    /// let duration = Duration::from_micros(1_000_002);
262    ///
263    /// assert_eq!(1, duration.as_secs());
264    /// assert_eq!(2_000, duration.subsec_nanos());
265    /// ```
266    #[stable(feature = "duration_from_micros", since = "1.27.0")]
267    #[must_use]
268    #[inline]
269    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
270    pub const fn from_micros(micros: u64) -> Duration {
271        let secs = micros / MICROS_PER_SEC;
272        let subsec_micros = (micros % MICROS_PER_SEC) as u32;
273        // SAFETY: (x % 1_000_000) * 1_000 < 1_000_000_000
274        //         => x % 1_000_000 < 1_000_000
275        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_micros * NANOS_PER_MICRO) };
276
277        Duration { secs, nanos: subsec_nanos }
278    }
279
280    /// Creates a new `Duration` from the specified number of nanoseconds.
281    ///
282    /// Note: Using this on the return value of `as_nanos()` might cause unexpected behavior:
283    /// `as_nanos()` returns a u128, and can return values that do not fit in u64, e.g. 585 years.
284    /// Instead, consider using the pattern `Duration::new(d.as_secs(), d.subsec_nanos())`
285    /// if you cannot copy/clone the Duration directly.
286    ///
287    /// # Examples
288    ///
289    /// ```
290    /// use std::time::Duration;
291    ///
292    /// let duration = Duration::from_nanos(1_000_000_123);
293    ///
294    /// assert_eq!(1, duration.as_secs());
295    /// assert_eq!(123, duration.subsec_nanos());
296    /// ```
297    #[stable(feature = "duration_extras", since = "1.27.0")]
298    #[must_use]
299    #[inline]
300    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
301    pub const fn from_nanos(nanos: u64) -> Duration {
302        const NANOS_PER_SEC: u64 = self::NANOS_PER_SEC as u64;
303        let secs = nanos / NANOS_PER_SEC;
304        let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
305        // SAFETY: x % 1_000_000_000 < 1_000_000_000
306        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
307
308        Duration { secs, nanos: subsec_nanos }
309    }
310
311    /// Creates a new `Duration` from the specified number of nanoseconds.
312    ///
313    /// # Panics
314    ///
315    /// Panics if the given number of nanoseconds is greater than [`Duration::MAX`].
316    ///
317    /// # Examples
318    ///
319    /// ```
320    /// #![feature(duration_from_nanos_u128)]
321    /// use std::time::Duration;
322    ///
323    /// let nanos = 10_u128.pow(24) + 321;
324    /// let duration = Duration::from_nanos_u128(nanos);
325    ///
326    /// assert_eq!(10_u64.pow(15), duration.as_secs());
327    /// assert_eq!(321, duration.subsec_nanos());
328    /// ```
329    #[unstable(feature = "duration_from_nanos_u128", issue = "139201")]
330    // This is necessary because of const `try_from`, but can be removed if a trait-free impl is used instead
331    #[rustc_const_unstable(feature = "duration_from_nanos_u128", issue = "139201")]
332    #[must_use]
333    #[inline]
334    #[track_caller]
335    pub const fn from_nanos_u128(nanos: u128) -> Duration {
336        const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128;
337        let Ok(secs) = u64::try_from(nanos / NANOS_PER_SEC) else {
338            panic!("overflow in `Duration::from_nanos_u128`");
339        };
340        let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
341        // SAFETY: x % 1_000_000_000 < 1_000_000_000 also, subsec_nanos >= 0 since u128 >=0 and u32 >=0
342        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
343
344        Duration { secs: secs as u64, nanos: subsec_nanos }
345    }
346
347    /// Creates a new `Duration` from the specified number of weeks.
348    ///
349    /// # Panics
350    ///
351    /// Panics if the given number of weeks overflows the `Duration` size.
352    ///
353    /// # Examples
354    ///
355    /// ```
356    /// #![feature(duration_constructors)]
357    /// use std::time::Duration;
358    ///
359    /// let duration = Duration::from_weeks(4);
360    ///
361    /// assert_eq!(4 * 7 * 24 * 60 * 60, duration.as_secs());
362    /// assert_eq!(0, duration.subsec_nanos());
363    /// ```
364    #[unstable(feature = "duration_constructors", issue = "120301")]
365    #[must_use]
366    #[inline]
367    pub const fn from_weeks(weeks: u64) -> Duration {
368        if weeks > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY * DAYS_PER_WEEK) {
369            panic!("overflow in Duration::from_weeks");
370        }
371
372        Duration::from_secs(weeks * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY * DAYS_PER_WEEK)
373    }
374
375    /// Creates a new `Duration` from the specified number of days.
376    ///
377    /// # Panics
378    ///
379    /// Panics if the given number of days overflows the `Duration` size.
380    ///
381    /// # Examples
382    ///
383    /// ```
384    /// #![feature(duration_constructors)]
385    /// use std::time::Duration;
386    ///
387    /// let duration = Duration::from_days(7);
388    ///
389    /// assert_eq!(7 * 24 * 60 * 60, duration.as_secs());
390    /// assert_eq!(0, duration.subsec_nanos());
391    /// ```
392    #[unstable(feature = "duration_constructors", issue = "120301")]
393    #[must_use]
394    #[inline]
395    pub const fn from_days(days: u64) -> Duration {
396        if days > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY) {
397            panic!("overflow in Duration::from_days");
398        }
399
400        Duration::from_secs(days * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY)
401    }
402
403    /// Creates a new `Duration` from the specified number of hours.
404    ///
405    /// # Panics
406    ///
407    /// Panics if the given number of hours overflows the `Duration` size.
408    ///
409    /// # Examples
410    ///
411    /// ```
412    /// use std::time::Duration;
413    ///
414    /// let duration = Duration::from_hours(6);
415    ///
416    /// assert_eq!(6 * 60 * 60, duration.as_secs());
417    /// assert_eq!(0, duration.subsec_nanos());
418    /// ```
419    #[stable(feature = "duration_constructors_lite", since = "CURRENT_RUSTC_VERSION")]
420    #[rustc_const_stable(feature = "duration_constructors_lite", since = "CURRENT_RUSTC_VERSION")]
421    #[must_use]
422    #[inline]
423    pub const fn from_hours(hours: u64) -> Duration {
424        if hours > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR) {
425            panic!("overflow in Duration::from_hours");
426        }
427
428        Duration::from_secs(hours * MINS_PER_HOUR * SECS_PER_MINUTE)
429    }
430
431    /// Creates a new `Duration` from the specified number of minutes.
432    ///
433    /// # Panics
434    ///
435    /// Panics if the given number of minutes overflows the `Duration` size.
436    ///
437    /// # Examples
438    ///
439    /// ```
440    /// use std::time::Duration;
441    ///
442    /// let duration = Duration::from_mins(10);
443    ///
444    /// assert_eq!(10 * 60, duration.as_secs());
445    /// assert_eq!(0, duration.subsec_nanos());
446    /// ```
447    #[stable(feature = "duration_constructors_lite", since = "CURRENT_RUSTC_VERSION")]
448    #[rustc_const_stable(feature = "duration_constructors_lite", since = "CURRENT_RUSTC_VERSION")]
449    #[must_use]
450    #[inline]
451    pub const fn from_mins(mins: u64) -> Duration {
452        if mins > u64::MAX / SECS_PER_MINUTE {
453            panic!("overflow in Duration::from_mins");
454        }
455
456        Duration::from_secs(mins * SECS_PER_MINUTE)
457    }
458
459    /// Returns true if this `Duration` spans no time.
460    ///
461    /// # Examples
462    ///
463    /// ```
464    /// use std::time::Duration;
465    ///
466    /// assert!(Duration::ZERO.is_zero());
467    /// assert!(Duration::new(0, 0).is_zero());
468    /// assert!(Duration::from_nanos(0).is_zero());
469    /// assert!(Duration::from_secs(0).is_zero());
470    ///
471    /// assert!(!Duration::new(1, 1).is_zero());
472    /// assert!(!Duration::from_nanos(1).is_zero());
473    /// assert!(!Duration::from_secs(1).is_zero());
474    /// ```
475    #[must_use]
476    #[stable(feature = "duration_zero", since = "1.53.0")]
477    #[rustc_const_stable(feature = "duration_zero", since = "1.53.0")]
478    #[inline]
479    pub const fn is_zero(&self) -> bool {
480        self.secs == 0 && self.nanos.as_inner() == 0
481    }
482
483    /// Returns the number of _whole_ seconds contained by this `Duration`.
484    ///
485    /// The returned value does not include the fractional (nanosecond) part of the
486    /// duration, which can be obtained using [`subsec_nanos`].
487    ///
488    /// # Examples
489    ///
490    /// ```
491    /// use std::time::Duration;
492    ///
493    /// let duration = Duration::new(5, 730_023_852);
494    /// assert_eq!(duration.as_secs(), 5);
495    /// ```
496    ///
497    /// To determine the total number of seconds represented by the `Duration`
498    /// including the fractional part, use [`as_secs_f64`] or [`as_secs_f32`]
499    ///
500    /// [`as_secs_f64`]: Duration::as_secs_f64
501    /// [`as_secs_f32`]: Duration::as_secs_f32
502    /// [`subsec_nanos`]: Duration::subsec_nanos
503    #[stable(feature = "duration", since = "1.3.0")]
504    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
505    #[must_use]
506    #[inline]
507    pub const fn as_secs(&self) -> u64 {
508        self.secs
509    }
510
511    /// Returns the fractional part of this `Duration`, in whole milliseconds.
512    ///
513    /// This method does **not** return the length of the duration when
514    /// represented by milliseconds. The returned number always represents a
515    /// fractional portion of a second (i.e., it is less than one thousand).
516    ///
517    /// # Examples
518    ///
519    /// ```
520    /// use std::time::Duration;
521    ///
522    /// let duration = Duration::from_millis(5_432);
523    /// assert_eq!(duration.as_secs(), 5);
524    /// assert_eq!(duration.subsec_millis(), 432);
525    /// ```
526    #[stable(feature = "duration_extras", since = "1.27.0")]
527    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
528    #[must_use]
529    #[inline]
530    pub const fn subsec_millis(&self) -> u32 {
531        self.nanos.as_inner() / NANOS_PER_MILLI
532    }
533
534    /// Returns the fractional part of this `Duration`, in whole microseconds.
535    ///
536    /// This method does **not** return the length of the duration when
537    /// represented by microseconds. The returned number always represents a
538    /// fractional portion of a second (i.e., it is less than one million).
539    ///
540    /// # Examples
541    ///
542    /// ```
543    /// use std::time::Duration;
544    ///
545    /// let duration = Duration::from_micros(1_234_567);
546    /// assert_eq!(duration.as_secs(), 1);
547    /// assert_eq!(duration.subsec_micros(), 234_567);
548    /// ```
549    #[stable(feature = "duration_extras", since = "1.27.0")]
550    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
551    #[must_use]
552    #[inline]
553    pub const fn subsec_micros(&self) -> u32 {
554        self.nanos.as_inner() / NANOS_PER_MICRO
555    }
556
557    /// Returns the fractional part of this `Duration`, in nanoseconds.
558    ///
559    /// This method does **not** return the length of the duration when
560    /// represented by nanoseconds. The returned number always represents a
561    /// fractional portion of a second (i.e., it is less than one billion).
562    ///
563    /// # Examples
564    ///
565    /// ```
566    /// use std::time::Duration;
567    ///
568    /// let duration = Duration::from_millis(5_010);
569    /// assert_eq!(duration.as_secs(), 5);
570    /// assert_eq!(duration.subsec_nanos(), 10_000_000);
571    /// ```
572    #[stable(feature = "duration", since = "1.3.0")]
573    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
574    #[must_use]
575    #[inline]
576    pub const fn subsec_nanos(&self) -> u32 {
577        self.nanos.as_inner()
578    }
579
580    /// Returns the total number of whole milliseconds contained by this `Duration`.
581    ///
582    /// # Examples
583    ///
584    /// ```
585    /// use std::time::Duration;
586    ///
587    /// let duration = Duration::new(5, 730_023_852);
588    /// assert_eq!(duration.as_millis(), 5_730);
589    /// ```
590    #[stable(feature = "duration_as_u128", since = "1.33.0")]
591    #[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
592    #[must_use]
593    #[inline]
594    pub const fn as_millis(&self) -> u128 {
595        self.secs as u128 * MILLIS_PER_SEC as u128
596            + (self.nanos.as_inner() / NANOS_PER_MILLI) as u128
597    }
598
599    /// Returns the total number of whole microseconds contained by this `Duration`.
600    ///
601    /// # Examples
602    ///
603    /// ```
604    /// use std::time::Duration;
605    ///
606    /// let duration = Duration::new(5, 730_023_852);
607    /// assert_eq!(duration.as_micros(), 5_730_023);
608    /// ```
609    #[stable(feature = "duration_as_u128", since = "1.33.0")]
610    #[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
611    #[must_use]
612    #[inline]
613    pub const fn as_micros(&self) -> u128 {
614        self.secs as u128 * MICROS_PER_SEC as u128
615            + (self.nanos.as_inner() / NANOS_PER_MICRO) as u128
616    }
617
618    /// Returns the total number of nanoseconds contained by this `Duration`.
619    ///
620    /// # Examples
621    ///
622    /// ```
623    /// use std::time::Duration;
624    ///
625    /// let duration = Duration::new(5, 730_023_852);
626    /// assert_eq!(duration.as_nanos(), 5_730_023_852);
627    /// ```
628    #[stable(feature = "duration_as_u128", since = "1.33.0")]
629    #[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
630    #[must_use]
631    #[inline]
632    pub const fn as_nanos(&self) -> u128 {
633        self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos.as_inner() as u128
634    }
635
636    /// Computes the absolute difference between `self` and `other`.
637    ///
638    /// # Examples
639    ///
640    /// ```
641    /// use std::time::Duration;
642    ///
643    /// assert_eq!(Duration::new(100, 0).abs_diff(Duration::new(80, 0)), Duration::new(20, 0));
644    /// assert_eq!(Duration::new(100, 400_000_000).abs_diff(Duration::new(110, 0)), Duration::new(9, 600_000_000));
645    /// ```
646    #[stable(feature = "duration_abs_diff", since = "1.81.0")]
647    #[rustc_const_stable(feature = "duration_abs_diff", since = "1.81.0")]
648    #[must_use = "this returns the result of the operation, \
649                  without modifying the original"]
650    #[inline]
651    pub const fn abs_diff(self, other: Duration) -> Duration {
652        if let Some(res) = self.checked_sub(other) { res } else { other.checked_sub(self).unwrap() }
653    }
654
655    /// Checked `Duration` addition. Computes `self + other`, returning [`None`]
656    /// if overflow occurred.
657    ///
658    /// # Examples
659    ///
660    /// ```
661    /// use std::time::Duration;
662    ///
663    /// assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
664    /// assert_eq!(Duration::new(1, 0).checked_add(Duration::new(u64::MAX, 0)), None);
665    /// ```
666    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
667    #[must_use = "this returns the result of the operation, \
668                  without modifying the original"]
669    #[inline]
670    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
671    pub const fn checked_add(self, rhs: Duration) -> Option<Duration> {
672        if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
673            let mut nanos = self.nanos.as_inner() + rhs.nanos.as_inner();
674            if nanos >= NANOS_PER_SEC {
675                nanos -= NANOS_PER_SEC;
676                if let Some(new_secs) = secs.checked_add(1) {
677                    secs = new_secs;
678                } else {
679                    return None;
680                }
681            }
682            debug_assert!(nanos < NANOS_PER_SEC);
683            Some(Duration::new(secs, nanos))
684        } else {
685            None
686        }
687    }
688
689    /// Saturating `Duration` addition. Computes `self + other`, returning [`Duration::MAX`]
690    /// if overflow occurred.
691    ///
692    /// # Examples
693    ///
694    /// ```
695    /// #![feature(duration_constants)]
696    /// use std::time::Duration;
697    ///
698    /// assert_eq!(Duration::new(0, 0).saturating_add(Duration::new(0, 1)), Duration::new(0, 1));
699    /// assert_eq!(Duration::new(1, 0).saturating_add(Duration::new(u64::MAX, 0)), Duration::MAX);
700    /// ```
701    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
702    #[must_use = "this returns the result of the operation, \
703                  without modifying the original"]
704    #[inline]
705    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
706    pub const fn saturating_add(self, rhs: Duration) -> Duration {
707        match self.checked_add(rhs) {
708            Some(res) => res,
709            None => Duration::MAX,
710        }
711    }
712
713    /// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
714    /// if the result would be negative or if overflow occurred.
715    ///
716    /// # Examples
717    ///
718    /// ```
719    /// use std::time::Duration;
720    ///
721    /// assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
722    /// assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);
723    /// ```
724    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
725    #[must_use = "this returns the result of the operation, \
726                  without modifying the original"]
727    #[inline]
728    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
729    pub const fn checked_sub(self, rhs: Duration) -> Option<Duration> {
730        if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
731            let nanos = if self.nanos.as_inner() >= rhs.nanos.as_inner() {
732                self.nanos.as_inner() - rhs.nanos.as_inner()
733            } else if let Some(sub_secs) = secs.checked_sub(1) {
734                secs = sub_secs;
735                self.nanos.as_inner() + NANOS_PER_SEC - rhs.nanos.as_inner()
736            } else {
737                return None;
738            };
739            debug_assert!(nanos < NANOS_PER_SEC);
740            Some(Duration::new(secs, nanos))
741        } else {
742            None
743        }
744    }
745
746    /// Saturating `Duration` subtraction. Computes `self - other`, returning [`Duration::ZERO`]
747    /// if the result would be negative or if overflow occurred.
748    ///
749    /// # Examples
750    ///
751    /// ```
752    /// use std::time::Duration;
753    ///
754    /// assert_eq!(Duration::new(0, 1).saturating_sub(Duration::new(0, 0)), Duration::new(0, 1));
755    /// assert_eq!(Duration::new(0, 0).saturating_sub(Duration::new(0, 1)), Duration::ZERO);
756    /// ```
757    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
758    #[must_use = "this returns the result of the operation, \
759                  without modifying the original"]
760    #[inline]
761    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
762    pub const fn saturating_sub(self, rhs: Duration) -> Duration {
763        match self.checked_sub(rhs) {
764            Some(res) => res,
765            None => Duration::ZERO,
766        }
767    }
768
769    /// Checked `Duration` multiplication. Computes `self * other`, returning
770    /// [`None`] if overflow occurred.
771    ///
772    /// # Examples
773    ///
774    /// ```
775    /// use std::time::Duration;
776    ///
777    /// assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
778    /// assert_eq!(Duration::new(u64::MAX - 1, 0).checked_mul(2), None);
779    /// ```
780    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
781    #[must_use = "this returns the result of the operation, \
782                  without modifying the original"]
783    #[inline]
784    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
785    pub const fn checked_mul(self, rhs: u32) -> Option<Duration> {
786        // Multiply nanoseconds as u64, because it cannot overflow that way.
787        let total_nanos = self.nanos.as_inner() as u64 * rhs as u64;
788        let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
789        let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
790        // FIXME(const-hack): use `and_then` once that is possible.
791        if let Some(s) = self.secs.checked_mul(rhs as u64) {
792            if let Some(secs) = s.checked_add(extra_secs) {
793                debug_assert!(nanos < NANOS_PER_SEC);
794                return Some(Duration::new(secs, nanos));
795            }
796        }
797        None
798    }
799
800    /// Saturating `Duration` multiplication. Computes `self * other`, returning
801    /// [`Duration::MAX`] if overflow occurred.
802    ///
803    /// # Examples
804    ///
805    /// ```
806    /// #![feature(duration_constants)]
807    /// use std::time::Duration;
808    ///
809    /// assert_eq!(Duration::new(0, 500_000_001).saturating_mul(2), Duration::new(1, 2));
810    /// assert_eq!(Duration::new(u64::MAX - 1, 0).saturating_mul(2), Duration::MAX);
811    /// ```
812    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
813    #[must_use = "this returns the result of the operation, \
814                  without modifying the original"]
815    #[inline]
816    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
817    pub const fn saturating_mul(self, rhs: u32) -> Duration {
818        match self.checked_mul(rhs) {
819            Some(res) => res,
820            None => Duration::MAX,
821        }
822    }
823
824    /// Checked `Duration` division. Computes `self / other`, returning [`None`]
825    /// if `other == 0`.
826    ///
827    /// # Examples
828    ///
829    /// ```
830    /// use std::time::Duration;
831    ///
832    /// assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
833    /// assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
834    /// assert_eq!(Duration::new(2, 0).checked_div(0), None);
835    /// ```
836    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
837    #[must_use = "this returns the result of the operation, \
838                  without modifying the original"]
839    #[inline]
840    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
841    pub const fn checked_div(self, rhs: u32) -> Option<Duration> {
842        if rhs != 0 {
843            let (secs, extra_secs) = (self.secs / (rhs as u64), self.secs % (rhs as u64));
844            let (mut nanos, extra_nanos) =
845                (self.nanos.as_inner() / rhs, self.nanos.as_inner() % rhs);
846            nanos +=
847                ((extra_secs * (NANOS_PER_SEC as u64) + extra_nanos as u64) / (rhs as u64)) as u32;
848            debug_assert!(nanos < NANOS_PER_SEC);
849            Some(Duration::new(secs, nanos))
850        } else {
851            None
852        }
853    }
854
855    /// Returns the number of seconds contained by this `Duration` as `f64`.
856    ///
857    /// The returned value includes the fractional (nanosecond) part of the duration.
858    ///
859    /// # Examples
860    /// ```
861    /// use std::time::Duration;
862    ///
863    /// let dur = Duration::new(2, 700_000_000);
864    /// assert_eq!(dur.as_secs_f64(), 2.7);
865    /// ```
866    #[stable(feature = "duration_float", since = "1.38.0")]
867    #[must_use]
868    #[inline]
869    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
870    pub const fn as_secs_f64(&self) -> f64 {
871        (self.secs as f64) + (self.nanos.as_inner() as f64) / (NANOS_PER_SEC as f64)
872    }
873
874    /// Returns the number of seconds contained by this `Duration` as `f32`.
875    ///
876    /// The returned value includes the fractional (nanosecond) part of the duration.
877    ///
878    /// # Examples
879    /// ```
880    /// use std::time::Duration;
881    ///
882    /// let dur = Duration::new(2, 700_000_000);
883    /// assert_eq!(dur.as_secs_f32(), 2.7);
884    /// ```
885    #[stable(feature = "duration_float", since = "1.38.0")]
886    #[must_use]
887    #[inline]
888    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
889    pub const fn as_secs_f32(&self) -> f32 {
890        (self.secs as f32) + (self.nanos.as_inner() as f32) / (NANOS_PER_SEC as f32)
891    }
892
893    /// Returns the number of milliseconds contained by this `Duration` as `f64`.
894    ///
895    /// The returned value includes the fractional (nanosecond) part of the duration.
896    ///
897    /// # Examples
898    /// ```
899    /// #![feature(duration_millis_float)]
900    /// use std::time::Duration;
901    ///
902    /// let dur = Duration::new(2, 345_678_000);
903    /// assert_eq!(dur.as_millis_f64(), 2_345.678);
904    /// ```
905    #[unstable(feature = "duration_millis_float", issue = "122451")]
906    #[must_use]
907    #[inline]
908    pub const fn as_millis_f64(&self) -> f64 {
909        (self.secs as f64) * (MILLIS_PER_SEC as f64)
910            + (self.nanos.as_inner() as f64) / (NANOS_PER_MILLI as f64)
911    }
912
913    /// Returns the number of milliseconds contained by this `Duration` as `f32`.
914    ///
915    /// The returned value includes the fractional (nanosecond) part of the duration.
916    ///
917    /// # Examples
918    /// ```
919    /// #![feature(duration_millis_float)]
920    /// use std::time::Duration;
921    ///
922    /// let dur = Duration::new(2, 345_678_000);
923    /// assert_eq!(dur.as_millis_f32(), 2_345.678);
924    /// ```
925    #[unstable(feature = "duration_millis_float", issue = "122451")]
926    #[must_use]
927    #[inline]
928    pub const fn as_millis_f32(&self) -> f32 {
929        (self.secs as f32) * (MILLIS_PER_SEC as f32)
930            + (self.nanos.as_inner() as f32) / (NANOS_PER_MILLI as f32)
931    }
932
933    /// Creates a new `Duration` from the specified number of seconds represented
934    /// as `f64`.
935    ///
936    /// # Panics
937    /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
938    ///
939    /// # Examples
940    /// ```
941    /// use std::time::Duration;
942    ///
943    /// let res = Duration::from_secs_f64(0.0);
944    /// assert_eq!(res, Duration::new(0, 0));
945    /// let res = Duration::from_secs_f64(1e-20);
946    /// assert_eq!(res, Duration::new(0, 0));
947    /// let res = Duration::from_secs_f64(4.2e-7);
948    /// assert_eq!(res, Duration::new(0, 420));
949    /// let res = Duration::from_secs_f64(2.7);
950    /// assert_eq!(res, Duration::new(2, 700_000_000));
951    /// let res = Duration::from_secs_f64(3e10);
952    /// assert_eq!(res, Duration::new(30_000_000_000, 0));
953    /// // subnormal float
954    /// let res = Duration::from_secs_f64(f64::from_bits(1));
955    /// assert_eq!(res, Duration::new(0, 0));
956    /// // conversion uses rounding
957    /// let res = Duration::from_secs_f64(0.999e-9);
958    /// assert_eq!(res, Duration::new(0, 1));
959    /// ```
960    #[stable(feature = "duration_float", since = "1.38.0")]
961    #[must_use]
962    #[inline]
963    pub fn from_secs_f64(secs: f64) -> Duration {
964        match Duration::try_from_secs_f64(secs) {
965            Ok(v) => v,
966            Err(e) => panic!("{e}"),
967        }
968    }
969
970    /// Creates a new `Duration` from the specified number of seconds represented
971    /// as `f32`.
972    ///
973    /// # Panics
974    /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
975    ///
976    /// # Examples
977    /// ```
978    /// use std::time::Duration;
979    ///
980    /// let res = Duration::from_secs_f32(0.0);
981    /// assert_eq!(res, Duration::new(0, 0));
982    /// let res = Duration::from_secs_f32(1e-20);
983    /// assert_eq!(res, Duration::new(0, 0));
984    /// let res = Duration::from_secs_f32(4.2e-7);
985    /// assert_eq!(res, Duration::new(0, 420));
986    /// let res = Duration::from_secs_f32(2.7);
987    /// assert_eq!(res, Duration::new(2, 700_000_048));
988    /// let res = Duration::from_secs_f32(3e10);
989    /// assert_eq!(res, Duration::new(30_000_001_024, 0));
990    /// // subnormal float
991    /// let res = Duration::from_secs_f32(f32::from_bits(1));
992    /// assert_eq!(res, Duration::new(0, 0));
993    /// // conversion uses rounding
994    /// let res = Duration::from_secs_f32(0.999e-9);
995    /// assert_eq!(res, Duration::new(0, 1));
996    /// ```
997    #[stable(feature = "duration_float", since = "1.38.0")]
998    #[must_use]
999    #[inline]
1000    pub fn from_secs_f32(secs: f32) -> Duration {
1001        match Duration::try_from_secs_f32(secs) {
1002            Ok(v) => v,
1003            Err(e) => panic!("{e}"),
1004        }
1005    }
1006
1007    /// Multiplies `Duration` by `f64`.
1008    ///
1009    /// # Panics
1010    /// This method will panic if result is negative, overflows `Duration` or not finite.
1011    ///
1012    /// # Examples
1013    /// ```
1014    /// use std::time::Duration;
1015    ///
1016    /// let dur = Duration::new(2, 700_000_000);
1017    /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
1018    /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
1019    /// ```
1020    #[stable(feature = "duration_float", since = "1.38.0")]
1021    #[must_use = "this returns the result of the operation, \
1022                  without modifying the original"]
1023    #[inline]
1024    pub fn mul_f64(self, rhs: f64) -> Duration {
1025        Duration::from_secs_f64(rhs * self.as_secs_f64())
1026    }
1027
1028    /// Multiplies `Duration` by `f32`.
1029    ///
1030    /// # Panics
1031    /// This method will panic if result is negative, overflows `Duration` or not finite.
1032    ///
1033    /// # Examples
1034    /// ```
1035    /// use std::time::Duration;
1036    ///
1037    /// let dur = Duration::new(2, 700_000_000);
1038    /// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_641));
1039    /// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847_800, 0));
1040    /// ```
1041    #[stable(feature = "duration_float", since = "1.38.0")]
1042    #[must_use = "this returns the result of the operation, \
1043                  without modifying the original"]
1044    #[inline]
1045    pub fn mul_f32(self, rhs: f32) -> Duration {
1046        Duration::from_secs_f32(rhs * self.as_secs_f32())
1047    }
1048
1049    /// Divides `Duration` by `f64`.
1050    ///
1051    /// # Panics
1052    /// This method will panic if result is negative, overflows `Duration` or not finite.
1053    ///
1054    /// # Examples
1055    /// ```
1056    /// use std::time::Duration;
1057    ///
1058    /// let dur = Duration::new(2, 700_000_000);
1059    /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
1060    /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_599));
1061    /// ```
1062    #[stable(feature = "duration_float", since = "1.38.0")]
1063    #[must_use = "this returns the result of the operation, \
1064                  without modifying the original"]
1065    #[inline]
1066    pub fn div_f64(self, rhs: f64) -> Duration {
1067        Duration::from_secs_f64(self.as_secs_f64() / rhs)
1068    }
1069
1070    /// Divides `Duration` by `f32`.
1071    ///
1072    /// # Panics
1073    /// This method will panic if result is negative, overflows `Duration` or not finite.
1074    ///
1075    /// # Examples
1076    /// ```
1077    /// use std::time::Duration;
1078    ///
1079    /// let dur = Duration::new(2, 700_000_000);
1080    /// // note that due to rounding errors result is slightly
1081    /// // different from 0.859_872_611
1082    /// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_580));
1083    /// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_599));
1084    /// ```
1085    #[stable(feature = "duration_float", since = "1.38.0")]
1086    #[must_use = "this returns the result of the operation, \
1087                  without modifying the original"]
1088    #[inline]
1089    pub fn div_f32(self, rhs: f32) -> Duration {
1090        Duration::from_secs_f32(self.as_secs_f32() / rhs)
1091    }
1092
1093    /// Divides `Duration` by `Duration` and returns `f64`.
1094    ///
1095    /// # Examples
1096    /// ```
1097    /// use std::time::Duration;
1098    ///
1099    /// let dur1 = Duration::new(2, 700_000_000);
1100    /// let dur2 = Duration::new(5, 400_000_000);
1101    /// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
1102    /// ```
1103    #[stable(feature = "div_duration", since = "1.80.0")]
1104    #[must_use = "this returns the result of the operation, \
1105                  without modifying the original"]
1106    #[inline]
1107    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1108    pub const fn div_duration_f64(self, rhs: Duration) -> f64 {
1109        let self_nanos =
1110            (self.secs as f64) * (NANOS_PER_SEC as f64) + (self.nanos.as_inner() as f64);
1111        let rhs_nanos = (rhs.secs as f64) * (NANOS_PER_SEC as f64) + (rhs.nanos.as_inner() as f64);
1112        self_nanos / rhs_nanos
1113    }
1114
1115    /// Divides `Duration` by `Duration` and returns `f32`.
1116    ///
1117    /// # Examples
1118    /// ```
1119    /// use std::time::Duration;
1120    ///
1121    /// let dur1 = Duration::new(2, 700_000_000);
1122    /// let dur2 = Duration::new(5, 400_000_000);
1123    /// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
1124    /// ```
1125    #[stable(feature = "div_duration", since = "1.80.0")]
1126    #[must_use = "this returns the result of the operation, \
1127                  without modifying the original"]
1128    #[inline]
1129    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1130    pub const fn div_duration_f32(self, rhs: Duration) -> f32 {
1131        let self_nanos =
1132            (self.secs as f32) * (NANOS_PER_SEC as f32) + (self.nanos.as_inner() as f32);
1133        let rhs_nanos = (rhs.secs as f32) * (NANOS_PER_SEC as f32) + (rhs.nanos.as_inner() as f32);
1134        self_nanos / rhs_nanos
1135    }
1136}
1137
1138#[stable(feature = "duration", since = "1.3.0")]
1139#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1140impl const Add for Duration {
1141    type Output = Duration;
1142
1143    #[inline]
1144    fn add(self, rhs: Duration) -> Duration {
1145        self.checked_add(rhs).expect("overflow when adding durations")
1146    }
1147}
1148
1149#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1150#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1151impl const AddAssign for Duration {
1152    #[inline]
1153    fn add_assign(&mut self, rhs: Duration) {
1154        *self = *self + rhs;
1155    }
1156}
1157
1158#[stable(feature = "duration", since = "1.3.0")]
1159#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1160impl const Sub for Duration {
1161    type Output = Duration;
1162
1163    #[inline]
1164    fn sub(self, rhs: Duration) -> Duration {
1165        self.checked_sub(rhs).expect("overflow when subtracting durations")
1166    }
1167}
1168
1169#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1170#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1171impl const SubAssign for Duration {
1172    #[inline]
1173    fn sub_assign(&mut self, rhs: Duration) {
1174        *self = *self - rhs;
1175    }
1176}
1177
1178#[stable(feature = "duration", since = "1.3.0")]
1179#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1180impl const Mul<u32> for Duration {
1181    type Output = Duration;
1182
1183    #[inline]
1184    fn mul(self, rhs: u32) -> Duration {
1185        self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
1186    }
1187}
1188
1189#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")]
1190#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1191impl const Mul<Duration> for u32 {
1192    type Output = Duration;
1193
1194    #[inline]
1195    fn mul(self, rhs: Duration) -> Duration {
1196        rhs * self
1197    }
1198}
1199
1200#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1201#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1202impl const MulAssign<u32> for Duration {
1203    #[inline]
1204    fn mul_assign(&mut self, rhs: u32) {
1205        *self = *self * rhs;
1206    }
1207}
1208
1209#[stable(feature = "duration", since = "1.3.0")]
1210#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1211impl const Div<u32> for Duration {
1212    type Output = Duration;
1213
1214    #[inline]
1215    #[track_caller]
1216    fn div(self, rhs: u32) -> Duration {
1217        self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
1218    }
1219}
1220
1221#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1222#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1223impl const DivAssign<u32> for Duration {
1224    #[inline]
1225    #[track_caller]
1226    fn div_assign(&mut self, rhs: u32) {
1227        *self = *self / rhs;
1228    }
1229}
1230
1231macro_rules! sum_durations {
1232    ($iter:expr) => {{
1233        let mut total_secs: u64 = 0;
1234        let mut total_nanos: u64 = 0;
1235
1236        for entry in $iter {
1237            total_secs =
1238                total_secs.checked_add(entry.secs).expect("overflow in iter::sum over durations");
1239            total_nanos = match total_nanos.checked_add(entry.nanos.as_inner() as u64) {
1240                Some(n) => n,
1241                None => {
1242                    total_secs = total_secs
1243                        .checked_add(total_nanos / NANOS_PER_SEC as u64)
1244                        .expect("overflow in iter::sum over durations");
1245                    (total_nanos % NANOS_PER_SEC as u64) + entry.nanos.as_inner() as u64
1246                }
1247            };
1248        }
1249        total_secs = total_secs
1250            .checked_add(total_nanos / NANOS_PER_SEC as u64)
1251            .expect("overflow in iter::sum over durations");
1252        total_nanos = total_nanos % NANOS_PER_SEC as u64;
1253        Duration::new(total_secs, total_nanos as u32)
1254    }};
1255}
1256
1257#[stable(feature = "duration_sum", since = "1.16.0")]
1258impl Sum for Duration {
1259    fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
1260        sum_durations!(iter)
1261    }
1262}
1263
1264#[stable(feature = "duration_sum", since = "1.16.0")]
1265impl<'a> Sum<&'a Duration> for Duration {
1266    fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
1267        sum_durations!(iter)
1268    }
1269}
1270
1271#[stable(feature = "duration_debug_impl", since = "1.27.0")]
1272impl fmt::Debug for Duration {
1273    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1274        /// Formats a floating point number in decimal notation.
1275        ///
1276        /// The number is given as the `integer_part` and a fractional part.
1277        /// The value of the fractional part is `fractional_part / divisor`. So
1278        /// `integer_part` = 3, `fractional_part` = 12 and `divisor` = 100
1279        /// represents the number `3.012`. Trailing zeros are omitted.
1280        ///
1281        /// `divisor` must not be above 100_000_000. It also should be a power
1282        /// of 10, everything else doesn't make sense. `fractional_part` has
1283        /// to be less than `10 * divisor`!
1284        ///
1285        /// A prefix and postfix may be added. The whole thing is padded
1286        /// to the formatter's `width`, if specified.
1287        fn fmt_decimal(
1288            f: &mut fmt::Formatter<'_>,
1289            integer_part: u64,
1290            mut fractional_part: u32,
1291            mut divisor: u32,
1292            prefix: &str,
1293            postfix: &str,
1294        ) -> fmt::Result {
1295            // Encode the fractional part into a temporary buffer. The buffer
1296            // only need to hold 9 elements, because `fractional_part` has to
1297            // be smaller than 10^9. The buffer is prefilled with '0' digits
1298            // to simplify the code below.
1299            let mut buf = [b'0'; 9];
1300
1301            // The next digit is written at this position
1302            let mut pos = 0;
1303
1304            // We keep writing digits into the buffer while there are non-zero
1305            // digits left and we haven't written enough digits yet.
1306            while fractional_part > 0 && pos < f.precision().unwrap_or(9) {
1307                // Write new digit into the buffer
1308                buf[pos] = b'0' + (fractional_part / divisor) as u8;
1309
1310                fractional_part %= divisor;
1311                divisor /= 10;
1312                pos += 1;
1313            }
1314
1315            // If a precision < 9 was specified, there may be some non-zero
1316            // digits left that weren't written into the buffer. In that case we
1317            // need to perform rounding to match the semantics of printing
1318            // normal floating point numbers. However, we only need to do work
1319            // when rounding up. This happens if the first digit of the
1320            // remaining ones is >= 5.
1321            let integer_part = if fractional_part > 0 && fractional_part >= divisor * 5 {
1322                // Round up the number contained in the buffer. We go through
1323                // the buffer backwards and keep track of the carry.
1324                let mut rev_pos = pos;
1325                let mut carry = true;
1326                while carry && rev_pos > 0 {
1327                    rev_pos -= 1;
1328
1329                    // If the digit in the buffer is not '9', we just need to
1330                    // increment it and can stop then (since we don't have a
1331                    // carry anymore). Otherwise, we set it to '0' (overflow)
1332                    // and continue.
1333                    if buf[rev_pos] < b'9' {
1334                        buf[rev_pos] += 1;
1335                        carry = false;
1336                    } else {
1337                        buf[rev_pos] = b'0';
1338                    }
1339                }
1340
1341                // If we still have the carry bit set, that means that we set
1342                // the whole buffer to '0's and need to increment the integer
1343                // part.
1344                if carry {
1345                    // If `integer_part == u64::MAX` and precision < 9, any
1346                    // carry of the overflow during rounding of the
1347                    // `fractional_part` into the `integer_part` will cause the
1348                    // `integer_part` itself to overflow. Avoid this by using an
1349                    // `Option<u64>`, with `None` representing `u64::MAX + 1`.
1350                    integer_part.checked_add(1)
1351                } else {
1352                    Some(integer_part)
1353                }
1354            } else {
1355                Some(integer_part)
1356            };
1357
1358            // Determine the end of the buffer: if precision is set, we just
1359            // use as many digits from the buffer (capped to 9). If it isn't
1360            // set, we only use all digits up to the last non-zero one.
1361            let end = f.precision().map(|p| crate::cmp::min(p, 9)).unwrap_or(pos);
1362
1363            // This closure emits the formatted duration without emitting any
1364            // padding (padding is calculated below).
1365            let emit_without_padding = |f: &mut fmt::Formatter<'_>| {
1366                if let Some(integer_part) = integer_part {
1367                    write!(f, "{}{}", prefix, integer_part)?;
1368                } else {
1369                    // u64::MAX + 1 == 18446744073709551616
1370                    write!(f, "{}18446744073709551616", prefix)?;
1371                }
1372
1373                // Write the decimal point and the fractional part (if any).
1374                if end > 0 {
1375                    // SAFETY: We are only writing ASCII digits into the buffer and
1376                    // it was initialized with '0's, so it contains valid UTF8.
1377                    let s = unsafe { crate::str::from_utf8_unchecked(&buf[..end]) };
1378
1379                    // If the user request a precision > 9, we pad '0's at the end.
1380                    let w = f.precision().unwrap_or(pos);
1381                    write!(f, ".{:0<width$}", s, width = w)?;
1382                }
1383
1384                write!(f, "{}", postfix)
1385            };
1386
1387            match f.width() {
1388                None => {
1389                    // No `width` specified. There's no need to calculate the
1390                    // length of the output in this case, just emit it.
1391                    emit_without_padding(f)
1392                }
1393                Some(requested_w) => {
1394                    // A `width` was specified. Calculate the actual width of
1395                    // the output in order to calculate the required padding.
1396                    // It consists of 4 parts:
1397                    // 1. The prefix: is either "+" or "", so we can just use len().
1398                    // 2. The postfix: can be "µs" so we have to count UTF8 characters.
1399                    let mut actual_w = prefix.len() + postfix.chars().count();
1400                    // 3. The integer part:
1401                    if let Some(integer_part) = integer_part {
1402                        if let Some(log) = integer_part.checked_ilog10() {
1403                            // integer_part is > 0, so has length log10(x)+1
1404                            actual_w += 1 + log as usize;
1405                        } else {
1406                            // integer_part is 0, so has length 1.
1407                            actual_w += 1;
1408                        }
1409                    } else {
1410                        // integer_part is u64::MAX + 1, so has length 20
1411                        actual_w += 20;
1412                    }
1413                    // 4. The fractional part (if any):
1414                    if end > 0 {
1415                        let frac_part_w = f.precision().unwrap_or(pos);
1416                        actual_w += 1 + frac_part_w;
1417                    }
1418
1419                    if requested_w <= actual_w {
1420                        // Output is already longer than `width`, so don't pad.
1421                        emit_without_padding(f)
1422                    } else {
1423                        // We need to add padding. Use the `Formatter::padding` helper function.
1424                        let default_align = fmt::Alignment::Left;
1425                        let post_padding =
1426                            f.padding((requested_w - actual_w) as u16, default_align)?;
1427                        emit_without_padding(f)?;
1428                        post_padding.write(f)
1429                    }
1430                }
1431            }
1432        }
1433
1434        // Print leading '+' sign if requested
1435        let prefix = if f.sign_plus() { "+" } else { "" };
1436
1437        if self.secs > 0 {
1438            fmt_decimal(f, self.secs, self.nanos.as_inner(), NANOS_PER_SEC / 10, prefix, "s")
1439        } else if self.nanos.as_inner() >= NANOS_PER_MILLI {
1440            fmt_decimal(
1441                f,
1442                (self.nanos.as_inner() / NANOS_PER_MILLI) as u64,
1443                self.nanos.as_inner() % NANOS_PER_MILLI,
1444                NANOS_PER_MILLI / 10,
1445                prefix,
1446                "ms",
1447            )
1448        } else if self.nanos.as_inner() >= NANOS_PER_MICRO {
1449            fmt_decimal(
1450                f,
1451                (self.nanos.as_inner() / NANOS_PER_MICRO) as u64,
1452                self.nanos.as_inner() % NANOS_PER_MICRO,
1453                NANOS_PER_MICRO / 10,
1454                prefix,
1455                "µs",
1456            )
1457        } else {
1458            fmt_decimal(f, self.nanos.as_inner() as u64, 0, 1, prefix, "ns")
1459        }
1460    }
1461}
1462
1463/// An error which can be returned when converting a floating-point value of seconds
1464/// into a [`Duration`].
1465///
1466/// This error is used as the error type for [`Duration::try_from_secs_f32`] and
1467/// [`Duration::try_from_secs_f64`].
1468///
1469/// # Example
1470///
1471/// ```
1472/// use std::time::Duration;
1473///
1474/// if let Err(e) = Duration::try_from_secs_f32(-1.0) {
1475///     println!("Failed conversion to Duration: {e}");
1476/// }
1477/// ```
1478#[derive(Debug, Clone, PartialEq, Eq)]
1479#[stable(feature = "duration_checked_float", since = "1.66.0")]
1480pub struct TryFromFloatSecsError {
1481    kind: TryFromFloatSecsErrorKind,
1482}
1483
1484#[stable(feature = "duration_checked_float", since = "1.66.0")]
1485impl fmt::Display for TryFromFloatSecsError {
1486    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1487        match self.kind {
1488            TryFromFloatSecsErrorKind::Negative => {
1489                "cannot convert float seconds to Duration: value is negative"
1490            }
1491            TryFromFloatSecsErrorKind::OverflowOrNan => {
1492                "cannot convert float seconds to Duration: value is either too big or NaN"
1493            }
1494        }
1495        .fmt(f)
1496    }
1497}
1498
1499#[derive(Debug, Clone, PartialEq, Eq)]
1500enum TryFromFloatSecsErrorKind {
1501    // Value is negative.
1502    Negative,
1503    // Value is either too big to be represented as `Duration` or `NaN`.
1504    OverflowOrNan,
1505}
1506
1507macro_rules! try_from_secs {
1508    (
1509        secs = $secs: expr,
1510        mantissa_bits = $mant_bits: literal,
1511        exponent_bits = $exp_bits: literal,
1512        offset = $offset: literal,
1513        bits_ty = $bits_ty:ty,
1514        double_ty = $double_ty:ty,
1515    ) => {{
1516        const MIN_EXP: i16 = 1 - (1i16 << $exp_bits) / 2;
1517        const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
1518        const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
1519
1520        if $secs < 0.0 {
1521            return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::Negative });
1522        }
1523
1524        let bits = $secs.to_bits();
1525        let mant = (bits & MANT_MASK) | (MANT_MASK + 1);
1526        let exp = ((bits >> $mant_bits) & EXP_MASK) as i16 + MIN_EXP;
1527
1528        let (secs, nanos) = if exp < -31 {
1529            // the input represents less than 1ns and can not be rounded to it
1530            (0u64, 0u32)
1531        } else if exp < 0 {
1532            // the input is less than 1 second
1533            let t = <$double_ty>::from(mant) << ($offset + exp);
1534            let nanos_offset = $mant_bits + $offset;
1535            let nanos_tmp = u128::from(NANOS_PER_SEC) * u128::from(t);
1536            let nanos = (nanos_tmp >> nanos_offset) as u32;
1537
1538            let rem_mask = (1 << nanos_offset) - 1;
1539            let rem_msb_mask = 1 << (nanos_offset - 1);
1540            let rem = nanos_tmp & rem_mask;
1541            let is_tie = rem == rem_msb_mask;
1542            let is_even = (nanos & 1) == 0;
1543            let rem_msb = nanos_tmp & rem_msb_mask == 0;
1544            let add_ns = !(rem_msb || (is_even && is_tie));
1545
1546            // f32 does not have enough precision to trigger the second branch
1547            // since it can not represent numbers between 0.999_999_940_395 and 1.0.
1548            let nanos = nanos + add_ns as u32;
1549            if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) { (0, nanos) } else { (1, 0) }
1550        } else if exp < $mant_bits {
1551            let secs = u64::from(mant >> ($mant_bits - exp));
1552            let t = <$double_ty>::from((mant << exp) & MANT_MASK);
1553            let nanos_offset = $mant_bits;
1554            let nanos_tmp = <$double_ty>::from(NANOS_PER_SEC) * t;
1555            let nanos = (nanos_tmp >> nanos_offset) as u32;
1556
1557            let rem_mask = (1 << nanos_offset) - 1;
1558            let rem_msb_mask = 1 << (nanos_offset - 1);
1559            let rem = nanos_tmp & rem_mask;
1560            let is_tie = rem == rem_msb_mask;
1561            let is_even = (nanos & 1) == 0;
1562            let rem_msb = nanos_tmp & rem_msb_mask == 0;
1563            let add_ns = !(rem_msb || (is_even && is_tie));
1564
1565            // f32 does not have enough precision to trigger the second branch.
1566            // For example, it can not represent numbers between 1.999_999_880...
1567            // and 2.0. Bigger values result in even smaller precision of the
1568            // fractional part.
1569            let nanos = nanos + add_ns as u32;
1570            if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) {
1571                (secs, nanos)
1572            } else {
1573                (secs + 1, 0)
1574            }
1575        } else if exp < 64 {
1576            // the input has no fractional part
1577            let secs = u64::from(mant) << (exp - $mant_bits);
1578            (secs, 0)
1579        } else {
1580            return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::OverflowOrNan });
1581        };
1582
1583        Ok(Duration::new(secs, nanos))
1584    }};
1585}
1586
1587impl Duration {
1588    /// The checked version of [`from_secs_f32`].
1589    ///
1590    /// [`from_secs_f32`]: Duration::from_secs_f32
1591    ///
1592    /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1593    ///
1594    /// # Examples
1595    /// ```
1596    /// use std::time::Duration;
1597    ///
1598    /// let res = Duration::try_from_secs_f32(0.0);
1599    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1600    /// let res = Duration::try_from_secs_f32(1e-20);
1601    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1602    /// let res = Duration::try_from_secs_f32(4.2e-7);
1603    /// assert_eq!(res, Ok(Duration::new(0, 420)));
1604    /// let res = Duration::try_from_secs_f32(2.7);
1605    /// assert_eq!(res, Ok(Duration::new(2, 700_000_048)));
1606    /// let res = Duration::try_from_secs_f32(3e10);
1607    /// assert_eq!(res, Ok(Duration::new(30_000_001_024, 0)));
1608    /// // subnormal float:
1609    /// let res = Duration::try_from_secs_f32(f32::from_bits(1));
1610    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1611    ///
1612    /// let res = Duration::try_from_secs_f32(-5.0);
1613    /// assert!(res.is_err());
1614    /// let res = Duration::try_from_secs_f32(f32::NAN);
1615    /// assert!(res.is_err());
1616    /// let res = Duration::try_from_secs_f32(2e19);
1617    /// assert!(res.is_err());
1618    ///
1619    /// // the conversion uses rounding with tie resolution to even
1620    /// let res = Duration::try_from_secs_f32(0.999e-9);
1621    /// assert_eq!(res, Ok(Duration::new(0, 1)));
1622    ///
1623    /// // this float represents exactly 976562.5e-9
1624    /// let val = f32::from_bits(0x3A80_0000);
1625    /// let res = Duration::try_from_secs_f32(val);
1626    /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1627    ///
1628    /// // this float represents exactly 2929687.5e-9
1629    /// let val = f32::from_bits(0x3B40_0000);
1630    /// let res = Duration::try_from_secs_f32(val);
1631    /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1632    ///
1633    /// // this float represents exactly 1.000_976_562_5
1634    /// let val = f32::from_bits(0x3F802000);
1635    /// let res = Duration::try_from_secs_f32(val);
1636    /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1637    ///
1638    /// // this float represents exactly 1.002_929_687_5
1639    /// let val = f32::from_bits(0x3F806000);
1640    /// let res = Duration::try_from_secs_f32(val);
1641    /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1642    /// ```
1643    #[stable(feature = "duration_checked_float", since = "1.66.0")]
1644    #[inline]
1645    pub fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError> {
1646        try_from_secs!(
1647            secs = secs,
1648            mantissa_bits = 23,
1649            exponent_bits = 8,
1650            offset = 41,
1651            bits_ty = u32,
1652            double_ty = u64,
1653        )
1654    }
1655
1656    /// The checked version of [`from_secs_f64`].
1657    ///
1658    /// [`from_secs_f64`]: Duration::from_secs_f64
1659    ///
1660    /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1661    ///
1662    /// # Examples
1663    /// ```
1664    /// use std::time::Duration;
1665    ///
1666    /// let res = Duration::try_from_secs_f64(0.0);
1667    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1668    /// let res = Duration::try_from_secs_f64(1e-20);
1669    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1670    /// let res = Duration::try_from_secs_f64(4.2e-7);
1671    /// assert_eq!(res, Ok(Duration::new(0, 420)));
1672    /// let res = Duration::try_from_secs_f64(2.7);
1673    /// assert_eq!(res, Ok(Duration::new(2, 700_000_000)));
1674    /// let res = Duration::try_from_secs_f64(3e10);
1675    /// assert_eq!(res, Ok(Duration::new(30_000_000_000, 0)));
1676    /// // subnormal float
1677    /// let res = Duration::try_from_secs_f64(f64::from_bits(1));
1678    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1679    ///
1680    /// let res = Duration::try_from_secs_f64(-5.0);
1681    /// assert!(res.is_err());
1682    /// let res = Duration::try_from_secs_f64(f64::NAN);
1683    /// assert!(res.is_err());
1684    /// let res = Duration::try_from_secs_f64(2e19);
1685    /// assert!(res.is_err());
1686    ///
1687    /// // the conversion uses rounding with tie resolution to even
1688    /// let res = Duration::try_from_secs_f64(0.999e-9);
1689    /// assert_eq!(res, Ok(Duration::new(0, 1)));
1690    /// let res = Duration::try_from_secs_f64(0.999_999_999_499);
1691    /// assert_eq!(res, Ok(Duration::new(0, 999_999_999)));
1692    /// let res = Duration::try_from_secs_f64(0.999_999_999_501);
1693    /// assert_eq!(res, Ok(Duration::new(1, 0)));
1694    /// let res = Duration::try_from_secs_f64(42.999_999_999_499);
1695    /// assert_eq!(res, Ok(Duration::new(42, 999_999_999)));
1696    /// let res = Duration::try_from_secs_f64(42.999_999_999_501);
1697    /// assert_eq!(res, Ok(Duration::new(43, 0)));
1698    ///
1699    /// // this float represents exactly 976562.5e-9
1700    /// let val = f64::from_bits(0x3F50_0000_0000_0000);
1701    /// let res = Duration::try_from_secs_f64(val);
1702    /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1703    ///
1704    /// // this float represents exactly 2929687.5e-9
1705    /// let val = f64::from_bits(0x3F68_0000_0000_0000);
1706    /// let res = Duration::try_from_secs_f64(val);
1707    /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1708    ///
1709    /// // this float represents exactly 1.000_976_562_5
1710    /// let val = f64::from_bits(0x3FF0_0400_0000_0000);
1711    /// let res = Duration::try_from_secs_f64(val);
1712    /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1713    ///
1714    /// // this float represents exactly 1.002_929_687_5
1715    /// let val = f64::from_bits(0x3_FF00_C000_0000_000);
1716    /// let res = Duration::try_from_secs_f64(val);
1717    /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1718    /// ```
1719    #[stable(feature = "duration_checked_float", since = "1.66.0")]
1720    #[inline]
1721    pub fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError> {
1722        try_from_secs!(
1723            secs = secs,
1724            mantissa_bits = 52,
1725            exponent_bits = 11,
1726            offset = 44,
1727            bits_ty = u64,
1728            double_ty = u128,
1729        )
1730    }
1731}