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