core/
time.rs

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