core/
time.rs

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