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
31// Ferrocene addition: imports for certified subset
32#[cfg(feature = "ferrocene_certified")]
33#[rustfmt::skip]
34use crate::ops::{Add, Div};
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#[cfg_attr(not(feature = "ferrocene_certified"), derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default))]
90#[cfg_attr(feature = "ferrocene_certified", derive(Clone, Copy, PartialEq, PartialOrd))]
91#[rustc_diagnostic_item = "Duration"]
92pub struct Duration {
93    secs: u64,
94    nanos: Nanoseconds, // Always 0 <= nanos < NANOS_PER_SEC
95}
96
97impl Duration {
98    /// The duration of one second.
99    ///
100    /// # Examples
101    ///
102    /// ```
103    /// #![feature(duration_constants)]
104    /// use std::time::Duration;
105    ///
106    /// assert_eq!(Duration::SECOND, Duration::from_secs(1));
107    /// ```
108    #[unstable(feature = "duration_constants", issue = "57391")]
109    pub const SECOND: Duration = Duration::from_secs(1);
110
111    /// The duration of one millisecond.
112    ///
113    /// # Examples
114    ///
115    /// ```
116    /// #![feature(duration_constants)]
117    /// use std::time::Duration;
118    ///
119    /// assert_eq!(Duration::MILLISECOND, Duration::from_millis(1));
120    /// ```
121    #[unstable(feature = "duration_constants", issue = "57391")]
122    pub const MILLISECOND: Duration = Duration::from_millis(1);
123
124    /// The duration of one microsecond.
125    ///
126    /// # Examples
127    ///
128    /// ```
129    /// #![feature(duration_constants)]
130    /// use std::time::Duration;
131    ///
132    /// assert_eq!(Duration::MICROSECOND, Duration::from_micros(1));
133    /// ```
134    #[unstable(feature = "duration_constants", issue = "57391")]
135    pub const MICROSECOND: Duration = Duration::from_micros(1);
136
137    /// The duration of one nanosecond.
138    ///
139    /// # Examples
140    ///
141    /// ```
142    /// #![feature(duration_constants)]
143    /// use std::time::Duration;
144    ///
145    /// assert_eq!(Duration::NANOSECOND, Duration::from_nanos(1));
146    /// ```
147    #[unstable(feature = "duration_constants", issue = "57391")]
148    pub const NANOSECOND: Duration = Duration::from_nanos(1);
149
150    /// A duration of zero time.
151    ///
152    /// # Examples
153    ///
154    /// ```
155    /// use std::time::Duration;
156    ///
157    /// let duration = Duration::ZERO;
158    /// assert!(duration.is_zero());
159    /// assert_eq!(duration.as_nanos(), 0);
160    /// ```
161    #[stable(feature = "duration_zero", since = "1.53.0")]
162    pub const ZERO: Duration = Duration::from_nanos(0);
163
164    /// The maximum duration.
165    ///
166    /// May vary by platform as necessary. Must be able to contain the difference between
167    /// two instances of [`Instant`] or two instances of [`SystemTime`].
168    /// This constraint gives it a value of about 584,942,417,355 years in practice,
169    /// which is currently used on all platforms.
170    ///
171    /// # Examples
172    ///
173    /// ```
174    /// use std::time::Duration;
175    ///
176    /// assert_eq!(Duration::MAX, Duration::new(u64::MAX, 1_000_000_000 - 1));
177    /// ```
178    /// [`Instant`]: ../../std/time/struct.Instant.html
179    /// [`SystemTime`]: ../../std/time/struct.SystemTime.html
180    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
181    #[cfg(not(feature = "ferrocene_certified"))]
182    pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC - 1);
183
184    /// Creates a new `Duration` from the specified number of whole seconds and
185    /// additional nanoseconds.
186    ///
187    /// If the number of nanoseconds is greater than 1 billion (the number of
188    /// nanoseconds in a second), then it will carry over into the seconds provided.
189    ///
190    /// # Panics
191    ///
192    /// This constructor will panic if the carry from the nanoseconds overflows
193    /// the seconds counter.
194    ///
195    /// # Examples
196    ///
197    /// ```
198    /// use std::time::Duration;
199    ///
200    /// let five_seconds = Duration::new(5, 0);
201    /// ```
202    #[stable(feature = "duration", since = "1.3.0")]
203    #[inline]
204    #[must_use]
205    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
206    pub const fn new(secs: u64, nanos: u32) -> Duration {
207        if nanos < NANOS_PER_SEC {
208            // SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
209            Duration { secs, nanos: unsafe { Nanoseconds::new_unchecked(nanos) } }
210        } else {
211            let secs = secs
212                .checked_add((nanos / NANOS_PER_SEC) as u64)
213                .expect("overflow in Duration::new");
214            let nanos = nanos % NANOS_PER_SEC;
215            // SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
216            Duration { secs, nanos: unsafe { Nanoseconds::new_unchecked(nanos) } }
217        }
218    }
219
220    /// Creates a new `Duration` from the specified number of whole seconds.
221    ///
222    /// # Examples
223    ///
224    /// ```
225    /// use std::time::Duration;
226    ///
227    /// let duration = Duration::from_secs(5);
228    ///
229    /// assert_eq!(5, duration.as_secs());
230    /// assert_eq!(0, duration.subsec_nanos());
231    /// ```
232    #[stable(feature = "duration", since = "1.3.0")]
233    #[must_use]
234    #[inline]
235    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
236    pub const fn from_secs(secs: u64) -> Duration {
237        Duration { secs, nanos: Nanoseconds::ZERO }
238    }
239
240    /// Creates a new `Duration` from the specified number of milliseconds.
241    ///
242    /// # Examples
243    ///
244    /// ```
245    /// use std::time::Duration;
246    ///
247    /// let duration = Duration::from_millis(2_569);
248    ///
249    /// assert_eq!(2, duration.as_secs());
250    /// assert_eq!(569_000_000, duration.subsec_nanos());
251    /// ```
252    #[stable(feature = "duration", since = "1.3.0")]
253    #[must_use]
254    #[inline]
255    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
256    pub const fn from_millis(millis: u64) -> Duration {
257        let secs = millis / MILLIS_PER_SEC;
258        let subsec_millis = (millis % MILLIS_PER_SEC) as u32;
259        // SAFETY: (x % 1_000) * 1_000_000 < 1_000_000_000
260        //         => x % 1_000 < 1_000
261        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_millis * NANOS_PER_MILLI) };
262
263        Duration { secs, nanos: subsec_nanos }
264    }
265
266    /// Creates a new `Duration` from the specified number of microseconds.
267    ///
268    /// # Examples
269    ///
270    /// ```
271    /// use std::time::Duration;
272    ///
273    /// let duration = Duration::from_micros(1_000_002);
274    ///
275    /// assert_eq!(1, duration.as_secs());
276    /// assert_eq!(2_000, duration.subsec_nanos());
277    /// ```
278    #[stable(feature = "duration_from_micros", since = "1.27.0")]
279    #[must_use]
280    #[inline]
281    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
282    pub const fn from_micros(micros: u64) -> Duration {
283        let secs = micros / MICROS_PER_SEC;
284        let subsec_micros = (micros % MICROS_PER_SEC) as u32;
285        // SAFETY: (x % 1_000_000) * 1_000 < 1_000_000_000
286        //         => x % 1_000_000 < 1_000_000
287        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_micros * NANOS_PER_MICRO) };
288
289        Duration { secs, nanos: subsec_nanos }
290    }
291
292    /// Creates a new `Duration` from the specified number of nanoseconds.
293    ///
294    /// Note: Using this on the return value of `as_nanos()` might cause unexpected behavior:
295    /// `as_nanos()` returns a u128, and can return values that do not fit in u64, e.g. 585 years.
296    /// Instead, consider using the pattern `Duration::new(d.as_secs(), d.subsec_nanos())`
297    /// if you cannot copy/clone the Duration directly.
298    ///
299    /// # Examples
300    ///
301    /// ```
302    /// use std::time::Duration;
303    ///
304    /// let duration = Duration::from_nanos(1_000_000_123);
305    ///
306    /// assert_eq!(1, duration.as_secs());
307    /// assert_eq!(123, duration.subsec_nanos());
308    /// ```
309    #[stable(feature = "duration_extras", since = "1.27.0")]
310    #[must_use]
311    #[inline]
312    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
313    pub const fn from_nanos(nanos: u64) -> Duration {
314        const NANOS_PER_SEC: u64 = self::NANOS_PER_SEC as u64;
315        let secs = nanos / NANOS_PER_SEC;
316        let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
317        // SAFETY: x % 1_000_000_000 < 1_000_000_000
318        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
319
320        Duration { secs, nanos: subsec_nanos }
321    }
322
323    /// Creates a new `Duration` from the specified number of nanoseconds.
324    ///
325    /// # Panics
326    ///
327    /// Panics if the given number of nanoseconds is greater than [`Duration::MAX`].
328    ///
329    /// # Examples
330    ///
331    /// ```
332    /// use std::time::Duration;
333    ///
334    /// let nanos = 10_u128.pow(24) + 321;
335    /// let duration = Duration::from_nanos_u128(nanos);
336    ///
337    /// assert_eq!(10_u64.pow(15), duration.as_secs());
338    /// assert_eq!(321, duration.subsec_nanos());
339    /// ```
340    #[stable(feature = "duration_from_nanos_u128", since = "CURRENT_RUSTC_VERSION")]
341    #[rustc_const_stable(feature = "duration_from_nanos_u128", since = "CURRENT_RUSTC_VERSION")]
342    #[must_use]
343    #[inline]
344    #[track_caller]
345    #[cfg(not(feature = "ferrocene_certified"))]
346    #[rustc_allow_const_fn_unstable(const_trait_impl, const_convert)] // for `u64::try_from`
347    pub const fn from_nanos_u128(nanos: u128) -> Duration {
348        const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128;
349        let Ok(secs) = u64::try_from(nanos / NANOS_PER_SEC) else {
350            panic!("overflow in `Duration::from_nanos_u128`");
351        };
352        let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
353        // SAFETY: x % 1_000_000_000 < 1_000_000_000 also, subsec_nanos >= 0 since u128 >=0 and u32 >=0
354        let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
355
356        Duration { secs: secs as u64, nanos: subsec_nanos }
357    }
358
359    /// Creates a new `Duration` from the specified number of weeks.
360    ///
361    /// # Panics
362    ///
363    /// Panics if the given number of weeks overflows the `Duration` size.
364    ///
365    /// # Examples
366    ///
367    /// ```
368    /// #![feature(duration_constructors)]
369    /// use std::time::Duration;
370    ///
371    /// let duration = Duration::from_weeks(4);
372    ///
373    /// assert_eq!(4 * 7 * 24 * 60 * 60, duration.as_secs());
374    /// assert_eq!(0, duration.subsec_nanos());
375    /// ```
376    #[unstable(feature = "duration_constructors", issue = "120301")]
377    #[must_use]
378    #[inline]
379    pub const fn from_weeks(weeks: u64) -> Duration {
380        if weeks > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY * DAYS_PER_WEEK) {
381            panic!("overflow in Duration::from_weeks");
382        }
383
384        Duration::from_secs(weeks * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY * DAYS_PER_WEEK)
385    }
386
387    /// Creates a new `Duration` from the specified number of days.
388    ///
389    /// # Panics
390    ///
391    /// Panics if the given number of days overflows the `Duration` size.
392    ///
393    /// # Examples
394    ///
395    /// ```
396    /// #![feature(duration_constructors)]
397    /// use std::time::Duration;
398    ///
399    /// let duration = Duration::from_days(7);
400    ///
401    /// assert_eq!(7 * 24 * 60 * 60, duration.as_secs());
402    /// assert_eq!(0, duration.subsec_nanos());
403    /// ```
404    #[unstable(feature = "duration_constructors", issue = "120301")]
405    #[must_use]
406    #[inline]
407    pub const fn from_days(days: u64) -> Duration {
408        if days > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY) {
409            panic!("overflow in Duration::from_days");
410        }
411
412        Duration::from_secs(days * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY)
413    }
414
415    /// Creates a new `Duration` from the specified number of hours.
416    ///
417    /// # Panics
418    ///
419    /// Panics if the given number of hours overflows the `Duration` size.
420    ///
421    /// # Examples
422    ///
423    /// ```
424    /// use std::time::Duration;
425    ///
426    /// let duration = Duration::from_hours(6);
427    ///
428    /// assert_eq!(6 * 60 * 60, duration.as_secs());
429    /// assert_eq!(0, duration.subsec_nanos());
430    /// ```
431    #[stable(feature = "duration_constructors_lite", since = "1.91.0")]
432    #[rustc_const_stable(feature = "duration_constructors_lite", since = "1.91.0")]
433    #[must_use]
434    #[inline]
435    pub const fn from_hours(hours: u64) -> Duration {
436        if hours > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR) {
437            panic!("overflow in Duration::from_hours");
438        }
439
440        Duration::from_secs(hours * MINS_PER_HOUR * SECS_PER_MINUTE)
441    }
442
443    /// Creates a new `Duration` from the specified number of minutes.
444    ///
445    /// # Panics
446    ///
447    /// Panics if the given number of minutes overflows the `Duration` size.
448    ///
449    /// # Examples
450    ///
451    /// ```
452    /// use std::time::Duration;
453    ///
454    /// let duration = Duration::from_mins(10);
455    ///
456    /// assert_eq!(10 * 60, duration.as_secs());
457    /// assert_eq!(0, duration.subsec_nanos());
458    /// ```
459    #[stable(feature = "duration_constructors_lite", since = "1.91.0")]
460    #[rustc_const_stable(feature = "duration_constructors_lite", since = "1.91.0")]
461    #[must_use]
462    #[inline]
463    pub const fn from_mins(mins: u64) -> Duration {
464        if mins > u64::MAX / SECS_PER_MINUTE {
465            panic!("overflow in Duration::from_mins");
466        }
467
468        Duration::from_secs(mins * SECS_PER_MINUTE)
469    }
470
471    /// Returns true if this `Duration` spans no time.
472    ///
473    /// # Examples
474    ///
475    /// ```
476    /// use std::time::Duration;
477    ///
478    /// assert!(Duration::ZERO.is_zero());
479    /// assert!(Duration::new(0, 0).is_zero());
480    /// assert!(Duration::from_nanos(0).is_zero());
481    /// assert!(Duration::from_secs(0).is_zero());
482    ///
483    /// assert!(!Duration::new(1, 1).is_zero());
484    /// assert!(!Duration::from_nanos(1).is_zero());
485    /// assert!(!Duration::from_secs(1).is_zero());
486    /// ```
487    #[must_use]
488    #[stable(feature = "duration_zero", since = "1.53.0")]
489    #[rustc_const_stable(feature = "duration_zero", since = "1.53.0")]
490    #[inline]
491    pub const fn is_zero(&self) -> bool {
492        self.secs == 0 && self.nanos.as_inner() == 0
493    }
494
495    /// Returns the number of _whole_ seconds contained by this `Duration`.
496    ///
497    /// The returned value does not include the fractional (nanosecond) part of the
498    /// duration, which can be obtained using [`subsec_nanos`].
499    ///
500    /// # Examples
501    ///
502    /// ```
503    /// use std::time::Duration;
504    ///
505    /// let duration = Duration::new(5, 730_023_852);
506    /// assert_eq!(duration.as_secs(), 5);
507    /// ```
508    ///
509    /// To determine the total number of seconds represented by the `Duration`
510    /// including the fractional part, use [`as_secs_f64`] or [`as_secs_f32`]
511    ///
512    /// [`as_secs_f64`]: Duration::as_secs_f64
513    /// [`as_secs_f32`]: Duration::as_secs_f32
514    /// [`subsec_nanos`]: Duration::subsec_nanos
515    #[stable(feature = "duration", since = "1.3.0")]
516    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
517    #[must_use]
518    #[inline]
519    pub const fn as_secs(&self) -> u64 {
520        self.secs
521    }
522
523    /// Returns the fractional part of this `Duration`, in whole milliseconds.
524    ///
525    /// This method does **not** return the length of the duration when
526    /// represented by milliseconds. The returned number always represents a
527    /// fractional portion of a second (i.e., it is less than one thousand).
528    ///
529    /// # Examples
530    ///
531    /// ```
532    /// use std::time::Duration;
533    ///
534    /// let duration = Duration::from_millis(5_432);
535    /// assert_eq!(duration.as_secs(), 5);
536    /// assert_eq!(duration.subsec_millis(), 432);
537    /// ```
538    #[stable(feature = "duration_extras", since = "1.27.0")]
539    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
540    #[must_use]
541    #[inline]
542    pub const fn subsec_millis(&self) -> u32 {
543        self.nanos.as_inner() / NANOS_PER_MILLI
544    }
545
546    /// Returns the fractional part of this `Duration`, in whole microseconds.
547    ///
548    /// This method does **not** return the length of the duration when
549    /// represented by microseconds. The returned number always represents a
550    /// fractional portion of a second (i.e., it is less than one million).
551    ///
552    /// # Examples
553    ///
554    /// ```
555    /// use std::time::Duration;
556    ///
557    /// let duration = Duration::from_micros(1_234_567);
558    /// assert_eq!(duration.as_secs(), 1);
559    /// assert_eq!(duration.subsec_micros(), 234_567);
560    /// ```
561    #[stable(feature = "duration_extras", since = "1.27.0")]
562    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
563    #[must_use]
564    #[inline]
565    pub const fn subsec_micros(&self) -> u32 {
566        self.nanos.as_inner() / NANOS_PER_MICRO
567    }
568
569    /// Returns the fractional part of this `Duration`, in nanoseconds.
570    ///
571    /// This method does **not** return the length of the duration when
572    /// represented by nanoseconds. The returned number always represents a
573    /// fractional portion of a second (i.e., it is less than one billion).
574    ///
575    /// # Examples
576    ///
577    /// ```
578    /// use std::time::Duration;
579    ///
580    /// let duration = Duration::from_millis(5_010);
581    /// assert_eq!(duration.as_secs(), 5);
582    /// assert_eq!(duration.subsec_nanos(), 10_000_000);
583    /// ```
584    #[stable(feature = "duration", since = "1.3.0")]
585    #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
586    #[must_use]
587    #[inline]
588    pub const fn subsec_nanos(&self) -> u32 {
589        self.nanos.as_inner()
590    }
591
592    /// Returns the total number of whole milliseconds contained by this `Duration`.
593    ///
594    /// # Examples
595    ///
596    /// ```
597    /// use std::time::Duration;
598    ///
599    /// let duration = Duration::new(5, 730_023_852);
600    /// assert_eq!(duration.as_millis(), 5_730);
601    /// ```
602    #[stable(feature = "duration_as_u128", since = "1.33.0")]
603    #[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
604    #[must_use]
605    #[inline]
606    pub const fn as_millis(&self) -> u128 {
607        self.secs as u128 * MILLIS_PER_SEC as u128
608            + (self.nanos.as_inner() / NANOS_PER_MILLI) as u128
609    }
610
611    /// Returns the total number of whole microseconds contained by this `Duration`.
612    ///
613    /// # Examples
614    ///
615    /// ```
616    /// use std::time::Duration;
617    ///
618    /// let duration = Duration::new(5, 730_023_852);
619    /// assert_eq!(duration.as_micros(), 5_730_023);
620    /// ```
621    #[stable(feature = "duration_as_u128", since = "1.33.0")]
622    #[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
623    #[must_use]
624    #[inline]
625    pub const fn as_micros(&self) -> u128 {
626        self.secs as u128 * MICROS_PER_SEC as u128
627            + (self.nanos.as_inner() / NANOS_PER_MICRO) as u128
628    }
629
630    /// Returns the total number of nanoseconds contained by this `Duration`.
631    ///
632    /// # Examples
633    ///
634    /// ```
635    /// use std::time::Duration;
636    ///
637    /// let duration = Duration::new(5, 730_023_852);
638    /// assert_eq!(duration.as_nanos(), 5_730_023_852);
639    /// ```
640    #[stable(feature = "duration_as_u128", since = "1.33.0")]
641    #[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
642    #[must_use]
643    #[inline]
644    pub const fn as_nanos(&self) -> u128 {
645        self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos.as_inner() as u128
646    }
647
648    /// Computes the absolute difference between `self` and `other`.
649    ///
650    /// # Examples
651    ///
652    /// ```
653    /// use std::time::Duration;
654    ///
655    /// assert_eq!(Duration::new(100, 0).abs_diff(Duration::new(80, 0)), Duration::new(20, 0));
656    /// assert_eq!(Duration::new(100, 400_000_000).abs_diff(Duration::new(110, 0)), Duration::new(9, 600_000_000));
657    /// ```
658    #[stable(feature = "duration_abs_diff", since = "1.81.0")]
659    #[rustc_const_stable(feature = "duration_abs_diff", since = "1.81.0")]
660    #[must_use = "this returns the result of the operation, \
661                  without modifying the original"]
662    #[inline]
663    #[cfg(not(feature = "ferrocene_certified"))]
664    pub const fn abs_diff(self, other: Duration) -> Duration {
665        if let Some(res) = self.checked_sub(other) { res } else { other.checked_sub(self).unwrap() }
666    }
667
668    /// Checked `Duration` addition. Computes `self + other`, returning [`None`]
669    /// if overflow occurred.
670    ///
671    /// # Examples
672    ///
673    /// ```
674    /// use std::time::Duration;
675    ///
676    /// assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
677    /// assert_eq!(Duration::new(1, 0).checked_add(Duration::new(u64::MAX, 0)), None);
678    /// ```
679    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
680    #[must_use = "this returns the result of the operation, \
681                  without modifying the original"]
682    #[inline]
683    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
684    pub const fn checked_add(self, rhs: Duration) -> Option<Duration> {
685        if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
686            let mut nanos = self.nanos.as_inner() + rhs.nanos.as_inner();
687            if nanos >= NANOS_PER_SEC {
688                nanos -= NANOS_PER_SEC;
689                if let Some(new_secs) = secs.checked_add(1) {
690                    secs = new_secs;
691                } else {
692                    return None;
693                }
694            }
695            debug_assert!(nanos < NANOS_PER_SEC);
696            Some(Duration::new(secs, nanos))
697        } else {
698            None
699        }
700    }
701
702    /// Saturating `Duration` addition. Computes `self + other`, returning [`Duration::MAX`]
703    /// if overflow occurred.
704    ///
705    /// # Examples
706    ///
707    /// ```
708    /// #![feature(duration_constants)]
709    /// use std::time::Duration;
710    ///
711    /// assert_eq!(Duration::new(0, 0).saturating_add(Duration::new(0, 1)), Duration::new(0, 1));
712    /// assert_eq!(Duration::new(1, 0).saturating_add(Duration::new(u64::MAX, 0)), Duration::MAX);
713    /// ```
714    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
715    #[must_use = "this returns the result of the operation, \
716                  without modifying the original"]
717    #[inline]
718    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
719    #[cfg(not(feature = "ferrocene_certified"))]
720    pub const fn saturating_add(self, rhs: Duration) -> Duration {
721        match self.checked_add(rhs) {
722            Some(res) => res,
723            None => Duration::MAX,
724        }
725    }
726
727    /// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
728    /// if the result would be negative or if overflow occurred.
729    ///
730    /// # Examples
731    ///
732    /// ```
733    /// use std::time::Duration;
734    ///
735    /// assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
736    /// assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);
737    /// ```
738    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
739    #[must_use = "this returns the result of the operation, \
740                  without modifying the original"]
741    #[inline]
742    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
743    #[cfg(not(feature = "ferrocene_certified"))]
744    pub const fn checked_sub(self, rhs: Duration) -> Option<Duration> {
745        if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
746            let nanos = if self.nanos.as_inner() >= rhs.nanos.as_inner() {
747                self.nanos.as_inner() - rhs.nanos.as_inner()
748            } else if let Some(sub_secs) = secs.checked_sub(1) {
749                secs = sub_secs;
750                self.nanos.as_inner() + NANOS_PER_SEC - rhs.nanos.as_inner()
751            } else {
752                return None;
753            };
754            debug_assert!(nanos < NANOS_PER_SEC);
755            Some(Duration::new(secs, nanos))
756        } else {
757            None
758        }
759    }
760
761    /// Saturating `Duration` subtraction. Computes `self - other`, returning [`Duration::ZERO`]
762    /// if the result would be negative or if overflow occurred.
763    ///
764    /// # Examples
765    ///
766    /// ```
767    /// use std::time::Duration;
768    ///
769    /// assert_eq!(Duration::new(0, 1).saturating_sub(Duration::new(0, 0)), Duration::new(0, 1));
770    /// assert_eq!(Duration::new(0, 0).saturating_sub(Duration::new(0, 1)), Duration::ZERO);
771    /// ```
772    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
773    #[must_use = "this returns the result of the operation, \
774                  without modifying the original"]
775    #[inline]
776    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
777    #[cfg(not(feature = "ferrocene_certified"))]
778    pub const fn saturating_sub(self, rhs: Duration) -> Duration {
779        match self.checked_sub(rhs) {
780            Some(res) => res,
781            None => Duration::ZERO,
782        }
783    }
784
785    /// Checked `Duration` multiplication. Computes `self * other`, returning
786    /// [`None`] if overflow occurred.
787    ///
788    /// # Examples
789    ///
790    /// ```
791    /// use std::time::Duration;
792    ///
793    /// assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
794    /// assert_eq!(Duration::new(u64::MAX - 1, 0).checked_mul(2), None);
795    /// ```
796    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
797    #[must_use = "this returns the result of the operation, \
798                  without modifying the original"]
799    #[inline]
800    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
801    #[cfg(not(feature = "ferrocene_certified"))]
802    pub const fn checked_mul(self, rhs: u32) -> Option<Duration> {
803        // Multiply nanoseconds as u64, because it cannot overflow that way.
804        let total_nanos = self.nanos.as_inner() as u64 * rhs as u64;
805        let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
806        let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
807        // FIXME(const-hack): use `and_then` once that is possible.
808        if let Some(s) = self.secs.checked_mul(rhs as u64) {
809            if let Some(secs) = s.checked_add(extra_secs) {
810                debug_assert!(nanos < NANOS_PER_SEC);
811                return Some(Duration::new(secs, nanos));
812            }
813        }
814        None
815    }
816
817    /// Saturating `Duration` multiplication. Computes `self * other`, returning
818    /// [`Duration::MAX`] if overflow occurred.
819    ///
820    /// # Examples
821    ///
822    /// ```
823    /// #![feature(duration_constants)]
824    /// use std::time::Duration;
825    ///
826    /// assert_eq!(Duration::new(0, 500_000_001).saturating_mul(2), Duration::new(1, 2));
827    /// assert_eq!(Duration::new(u64::MAX - 1, 0).saturating_mul(2), Duration::MAX);
828    /// ```
829    #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
830    #[must_use = "this returns the result of the operation, \
831                  without modifying the original"]
832    #[inline]
833    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
834    #[cfg(not(feature = "ferrocene_certified"))]
835    pub const fn saturating_mul(self, rhs: u32) -> Duration {
836        match self.checked_mul(rhs) {
837            Some(res) => res,
838            None => Duration::MAX,
839        }
840    }
841
842    /// Checked `Duration` division. Computes `self / other`, returning [`None`]
843    /// if `other == 0`.
844    ///
845    /// # Examples
846    ///
847    /// ```
848    /// use std::time::Duration;
849    ///
850    /// assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
851    /// assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
852    /// assert_eq!(Duration::new(2, 0).checked_div(0), None);
853    /// ```
854    #[stable(feature = "duration_checked_ops", since = "1.16.0")]
855    #[must_use = "this returns the result of the operation, \
856                  without modifying the original"]
857    #[inline]
858    #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
859    pub const fn checked_div(self, rhs: u32) -> Option<Duration> {
860        if rhs != 0 {
861            let (secs, extra_secs) = (self.secs / (rhs as u64), self.secs % (rhs as u64));
862            let (mut nanos, extra_nanos) =
863                (self.nanos.as_inner() / rhs, self.nanos.as_inner() % rhs);
864            nanos +=
865                ((extra_secs * (NANOS_PER_SEC as u64) + extra_nanos as u64) / (rhs as u64)) as u32;
866            debug_assert!(nanos < NANOS_PER_SEC);
867            Some(Duration::new(secs, nanos))
868        } else {
869            None
870        }
871    }
872
873    /// Returns the number of seconds contained by this `Duration` as `f64`.
874    ///
875    /// The returned value includes the fractional (nanosecond) part of the duration.
876    ///
877    /// # Examples
878    /// ```
879    /// use std::time::Duration;
880    ///
881    /// let dur = Duration::new(2, 700_000_000);
882    /// assert_eq!(dur.as_secs_f64(), 2.7);
883    /// ```
884    #[stable(feature = "duration_float", since = "1.38.0")]
885    #[must_use]
886    #[inline]
887    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
888    pub const fn as_secs_f64(&self) -> f64 {
889        (self.secs as f64) + (self.nanos.as_inner() as f64) / (NANOS_PER_SEC as f64)
890    }
891
892    /// Returns the number of seconds contained by this `Duration` as `f32`.
893    ///
894    /// The returned value includes the fractional (nanosecond) part of the duration.
895    ///
896    /// # Examples
897    /// ```
898    /// use std::time::Duration;
899    ///
900    /// let dur = Duration::new(2, 700_000_000);
901    /// assert_eq!(dur.as_secs_f32(), 2.7);
902    /// ```
903    #[stable(feature = "duration_float", since = "1.38.0")]
904    #[must_use]
905    #[inline]
906    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
907    pub const fn as_secs_f32(&self) -> f32 {
908        (self.secs as f32) + (self.nanos.as_inner() as f32) / (NANOS_PER_SEC as f32)
909    }
910
911    /// Returns the number of milliseconds contained by this `Duration` as `f64`.
912    ///
913    /// The returned value includes the fractional (nanosecond) part of the duration.
914    ///
915    /// # Examples
916    /// ```
917    /// #![feature(duration_millis_float)]
918    /// use std::time::Duration;
919    ///
920    /// let dur = Duration::new(2, 345_678_000);
921    /// assert_eq!(dur.as_millis_f64(), 2_345.678);
922    /// ```
923    #[unstable(feature = "duration_millis_float", issue = "122451")]
924    #[must_use]
925    #[inline]
926    pub const fn as_millis_f64(&self) -> f64 {
927        (self.secs as f64) * (MILLIS_PER_SEC as f64)
928            + (self.nanos.as_inner() as f64) / (NANOS_PER_MILLI as f64)
929    }
930
931    /// Returns the number of milliseconds contained by this `Duration` as `f32`.
932    ///
933    /// The returned value includes the fractional (nanosecond) part of the duration.
934    ///
935    /// # Examples
936    /// ```
937    /// #![feature(duration_millis_float)]
938    /// use std::time::Duration;
939    ///
940    /// let dur = Duration::new(2, 345_678_000);
941    /// assert_eq!(dur.as_millis_f32(), 2_345.678);
942    /// ```
943    #[unstable(feature = "duration_millis_float", issue = "122451")]
944    #[must_use]
945    #[inline]
946    pub const fn as_millis_f32(&self) -> f32 {
947        (self.secs as f32) * (MILLIS_PER_SEC as f32)
948            + (self.nanos.as_inner() as f32) / (NANOS_PER_MILLI as f32)
949    }
950
951    /// Creates a new `Duration` from the specified number of seconds represented
952    /// as `f64`.
953    ///
954    /// # Panics
955    /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
956    ///
957    /// # Examples
958    /// ```
959    /// use std::time::Duration;
960    ///
961    /// let res = Duration::from_secs_f64(0.0);
962    /// assert_eq!(res, Duration::new(0, 0));
963    /// let res = Duration::from_secs_f64(1e-20);
964    /// assert_eq!(res, Duration::new(0, 0));
965    /// let res = Duration::from_secs_f64(4.2e-7);
966    /// assert_eq!(res, Duration::new(0, 420));
967    /// let res = Duration::from_secs_f64(2.7);
968    /// assert_eq!(res, Duration::new(2, 700_000_000));
969    /// let res = Duration::from_secs_f64(3e10);
970    /// assert_eq!(res, Duration::new(30_000_000_000, 0));
971    /// // subnormal float
972    /// let res = Duration::from_secs_f64(f64::from_bits(1));
973    /// assert_eq!(res, Duration::new(0, 0));
974    /// // conversion uses rounding
975    /// let res = Duration::from_secs_f64(0.999e-9);
976    /// assert_eq!(res, Duration::new(0, 1));
977    /// ```
978    #[stable(feature = "duration_float", since = "1.38.0")]
979    #[must_use]
980    #[inline]
981    #[cfg(not(feature = "ferrocene_certified"))]
982    pub fn from_secs_f64(secs: f64) -> Duration {
983        match Duration::try_from_secs_f64(secs) {
984            Ok(v) => v,
985            Err(e) => panic!("{e}"),
986        }
987    }
988
989    /// Creates a new `Duration` from the specified number of seconds represented
990    /// as `f32`.
991    ///
992    /// # Panics
993    /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
994    ///
995    /// # Examples
996    /// ```
997    /// use std::time::Duration;
998    ///
999    /// let res = Duration::from_secs_f32(0.0);
1000    /// assert_eq!(res, Duration::new(0, 0));
1001    /// let res = Duration::from_secs_f32(1e-20);
1002    /// assert_eq!(res, Duration::new(0, 0));
1003    /// let res = Duration::from_secs_f32(4.2e-7);
1004    /// assert_eq!(res, Duration::new(0, 420));
1005    /// let res = Duration::from_secs_f32(2.7);
1006    /// assert_eq!(res, Duration::new(2, 700_000_048));
1007    /// let res = Duration::from_secs_f32(3e10);
1008    /// assert_eq!(res, Duration::new(30_000_001_024, 0));
1009    /// // subnormal float
1010    /// let res = Duration::from_secs_f32(f32::from_bits(1));
1011    /// assert_eq!(res, Duration::new(0, 0));
1012    /// // conversion uses rounding
1013    /// let res = Duration::from_secs_f32(0.999e-9);
1014    /// assert_eq!(res, Duration::new(0, 1));
1015    /// ```
1016    #[stable(feature = "duration_float", since = "1.38.0")]
1017    #[must_use]
1018    #[inline]
1019    #[cfg_attr(feature = "ferrocene_certified", expect(unused_variables))]
1020    pub fn from_secs_f32(secs: f32) -> Duration {
1021        match Duration::try_from_secs_f32(secs) {
1022            Ok(v) => v,
1023            Err(e) => panic!("{e}"),
1024        }
1025    }
1026
1027    /// Multiplies `Duration` by `f64`.
1028    ///
1029    /// # Panics
1030    /// This method will panic if result is negative, overflows `Duration` or not finite.
1031    ///
1032    /// # Examples
1033    /// ```
1034    /// use std::time::Duration;
1035    ///
1036    /// let dur = Duration::new(2, 700_000_000);
1037    /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
1038    /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
1039    /// ```
1040    #[stable(feature = "duration_float", since = "1.38.0")]
1041    #[must_use = "this returns the result of the operation, \
1042                  without modifying the original"]
1043    #[inline]
1044    #[cfg(not(feature = "ferrocene_certified"))]
1045    pub fn mul_f64(self, rhs: f64) -> Duration {
1046        Duration::from_secs_f64(rhs * self.as_secs_f64())
1047    }
1048
1049    /// Multiplies `Duration` by `f32`.
1050    ///
1051    /// # Panics
1052    /// This method will panic if result is negative, overflows `Duration` or not finite.
1053    ///
1054    /// # Examples
1055    /// ```
1056    /// use std::time::Duration;
1057    ///
1058    /// let dur = Duration::new(2, 700_000_000);
1059    /// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_641));
1060    /// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847_800, 0));
1061    /// ```
1062    #[stable(feature = "duration_float", since = "1.38.0")]
1063    #[must_use = "this returns the result of the operation, \
1064                  without modifying the original"]
1065    #[inline]
1066    #[cfg(not(feature = "ferrocene_certified"))]
1067    pub fn mul_f32(self, rhs: f32) -> Duration {
1068        Duration::from_secs_f32(rhs * self.as_secs_f32())
1069    }
1070
1071    /// Divides `Duration` by `f64`.
1072    ///
1073    /// # Panics
1074    /// This method will panic if result is negative, overflows `Duration` or not finite.
1075    ///
1076    /// # Examples
1077    /// ```
1078    /// use std::time::Duration;
1079    ///
1080    /// let dur = Duration::new(2, 700_000_000);
1081    /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
1082    /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_599));
1083    /// ```
1084    #[stable(feature = "duration_float", since = "1.38.0")]
1085    #[must_use = "this returns the result of the operation, \
1086                  without modifying the original"]
1087    #[inline]
1088    #[cfg(not(feature = "ferrocene_certified"))]
1089    pub fn div_f64(self, rhs: f64) -> Duration {
1090        Duration::from_secs_f64(self.as_secs_f64() / rhs)
1091    }
1092
1093    /// Divides `Duration` by `f32`.
1094    ///
1095    /// # Panics
1096    /// This method will panic if result is negative, overflows `Duration` or not finite.
1097    ///
1098    /// # Examples
1099    /// ```
1100    /// use std::time::Duration;
1101    ///
1102    /// let dur = Duration::new(2, 700_000_000);
1103    /// // note that due to rounding errors result is slightly
1104    /// // different from 0.859_872_611
1105    /// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_580));
1106    /// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_599));
1107    /// ```
1108    #[stable(feature = "duration_float", since = "1.38.0")]
1109    #[must_use = "this returns the result of the operation, \
1110                  without modifying the original"]
1111    #[inline]
1112    #[cfg(not(feature = "ferrocene_certified"))]
1113    pub fn div_f32(self, rhs: f32) -> Duration {
1114        Duration::from_secs_f32(self.as_secs_f32() / rhs)
1115    }
1116
1117    /// Divides `Duration` by `Duration` and returns `f64`.
1118    ///
1119    /// # Examples
1120    /// ```
1121    /// use std::time::Duration;
1122    ///
1123    /// let dur1 = Duration::new(2, 700_000_000);
1124    /// let dur2 = Duration::new(5, 400_000_000);
1125    /// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
1126    /// ```
1127    #[stable(feature = "div_duration", since = "1.80.0")]
1128    #[must_use = "this returns the result of the operation, \
1129                  without modifying the original"]
1130    #[inline]
1131    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1132    pub const fn div_duration_f64(self, rhs: Duration) -> f64 {
1133        let self_nanos =
1134            (self.secs as f64) * (NANOS_PER_SEC as f64) + (self.nanos.as_inner() as f64);
1135        let rhs_nanos = (rhs.secs as f64) * (NANOS_PER_SEC as f64) + (rhs.nanos.as_inner() as f64);
1136        self_nanos / rhs_nanos
1137    }
1138
1139    /// Divides `Duration` by `Duration` and returns `f32`.
1140    ///
1141    /// # Examples
1142    /// ```
1143    /// use std::time::Duration;
1144    ///
1145    /// let dur1 = Duration::new(2, 700_000_000);
1146    /// let dur2 = Duration::new(5, 400_000_000);
1147    /// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
1148    /// ```
1149    #[stable(feature = "div_duration", since = "1.80.0")]
1150    #[must_use = "this returns the result of the operation, \
1151                  without modifying the original"]
1152    #[inline]
1153    #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1154    pub const fn div_duration_f32(self, rhs: Duration) -> f32 {
1155        let self_nanos =
1156            (self.secs as f32) * (NANOS_PER_SEC as f32) + (self.nanos.as_inner() as f32);
1157        let rhs_nanos = (rhs.secs as f32) * (NANOS_PER_SEC as f32) + (rhs.nanos.as_inner() as f32);
1158        self_nanos / rhs_nanos
1159    }
1160}
1161
1162#[stable(feature = "duration", since = "1.3.0")]
1163#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1164impl const Add for Duration {
1165    type Output = Duration;
1166
1167    #[inline]
1168    fn add(self, rhs: Duration) -> Duration {
1169        self.checked_add(rhs).expect("overflow when adding durations")
1170    }
1171}
1172
1173#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1174#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1175#[cfg(not(feature = "ferrocene_certified"))]
1176impl const AddAssign for Duration {
1177    #[inline]
1178    fn add_assign(&mut self, rhs: Duration) {
1179        *self = *self + rhs;
1180    }
1181}
1182
1183#[stable(feature = "duration", since = "1.3.0")]
1184#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1185#[cfg(not(feature = "ferrocene_certified"))]
1186impl const Sub for Duration {
1187    type Output = Duration;
1188
1189    #[inline]
1190    fn sub(self, rhs: Duration) -> Duration {
1191        self.checked_sub(rhs).expect("overflow when subtracting durations")
1192    }
1193}
1194
1195#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1196#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1197#[cfg(not(feature = "ferrocene_certified"))]
1198impl const SubAssign for Duration {
1199    #[inline]
1200    fn sub_assign(&mut self, rhs: Duration) {
1201        *self = *self - rhs;
1202    }
1203}
1204
1205#[stable(feature = "duration", since = "1.3.0")]
1206#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1207#[cfg(not(feature = "ferrocene_certified"))]
1208impl const Mul<u32> for Duration {
1209    type Output = Duration;
1210
1211    #[inline]
1212    fn mul(self, rhs: u32) -> Duration {
1213        self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
1214    }
1215}
1216
1217#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")]
1218#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1219#[cfg(not(feature = "ferrocene_certified"))]
1220impl const Mul<Duration> for u32 {
1221    type Output = Duration;
1222
1223    #[inline]
1224    fn mul(self, rhs: Duration) -> Duration {
1225        rhs * self
1226    }
1227}
1228
1229#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1230#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1231#[cfg(not(feature = "ferrocene_certified"))]
1232impl const MulAssign<u32> for Duration {
1233    #[inline]
1234    fn mul_assign(&mut self, rhs: u32) {
1235        *self = *self * rhs;
1236    }
1237}
1238
1239#[stable(feature = "duration", since = "1.3.0")]
1240#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
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#[cfg_attr(not(feature = "ferrocene_certified"), derive(Debug, Clone, PartialEq, Eq))]
1514#[stable(feature = "duration_checked_float", since = "1.66.0")]
1515pub struct TryFromFloatSecsError {
1516    kind: TryFromFloatSecsErrorKind,
1517}
1518
1519#[stable(feature = "duration_checked_float", since = "1.66.0")]
1520#[cfg(not(feature = "ferrocene_certified"))]
1521impl fmt::Display for TryFromFloatSecsError {
1522    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1523        match self.kind {
1524            TryFromFloatSecsErrorKind::Negative => {
1525                "cannot convert float seconds to Duration: value is negative"
1526            }
1527            TryFromFloatSecsErrorKind::OverflowOrNan => {
1528                "cannot convert float seconds to Duration: value is either too big or NaN"
1529            }
1530        }
1531        .fmt(f)
1532    }
1533}
1534
1535#[cfg_attr(not(feature = "ferrocene_certified"), derive(Debug, Clone, PartialEq, Eq))]
1536enum TryFromFloatSecsErrorKind {
1537    // Value is negative.
1538    Negative,
1539    // Value is either too big to be represented as `Duration` or `NaN`.
1540    OverflowOrNan,
1541}
1542
1543macro_rules! try_from_secs {
1544    (
1545        secs = $secs: expr,
1546        mantissa_bits = $mant_bits: literal,
1547        exponent_bits = $exp_bits: literal,
1548        offset = $offset: literal,
1549        bits_ty = $bits_ty:ty,
1550        double_ty = $double_ty:ty,
1551    ) => {{
1552        const MIN_EXP: i16 = 1 - (1i16 << $exp_bits) / 2;
1553        const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
1554        const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
1555
1556        if $secs < 0.0 {
1557            return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::Negative });
1558        }
1559
1560        let bits = $secs.to_bits();
1561        let mant = (bits & MANT_MASK) | (MANT_MASK + 1);
1562        let exp = ((bits >> $mant_bits) & EXP_MASK) as i16 + MIN_EXP;
1563
1564        let (secs, nanos) = if exp < -31 {
1565            // the input represents less than 1ns and can not be rounded to it
1566            (0u64, 0u32)
1567        } else if exp < 0 {
1568            // the input is less than 1 second
1569            let t = <$double_ty>::from(mant) << ($offset + exp);
1570            let nanos_offset = $mant_bits + $offset;
1571            let nanos_tmp = u128::from(NANOS_PER_SEC) * u128::from(t);
1572            let nanos = (nanos_tmp >> nanos_offset) as u32;
1573
1574            let rem_mask = (1 << nanos_offset) - 1;
1575            let rem_msb_mask = 1 << (nanos_offset - 1);
1576            let rem = nanos_tmp & rem_mask;
1577            let is_tie = rem == rem_msb_mask;
1578            let is_even = (nanos & 1) == 0;
1579            let rem_msb = nanos_tmp & rem_msb_mask == 0;
1580            let add_ns = !(rem_msb || (is_even && is_tie));
1581
1582            // f32 does not have enough precision to trigger the second branch
1583            // since it can not represent numbers between 0.999_999_940_395 and 1.0.
1584            let nanos = nanos + add_ns as u32;
1585            if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) { (0, nanos) } else { (1, 0) }
1586        } else if exp < $mant_bits {
1587            let secs = u64::from(mant >> ($mant_bits - exp));
1588            let t = <$double_ty>::from((mant << exp) & MANT_MASK);
1589            let nanos_offset = $mant_bits;
1590            let nanos_tmp = <$double_ty>::from(NANOS_PER_SEC) * t;
1591            let nanos = (nanos_tmp >> nanos_offset) as u32;
1592
1593            let rem_mask = (1 << nanos_offset) - 1;
1594            let rem_msb_mask = 1 << (nanos_offset - 1);
1595            let rem = nanos_tmp & rem_mask;
1596            let is_tie = rem == rem_msb_mask;
1597            let is_even = (nanos & 1) == 0;
1598            let rem_msb = nanos_tmp & rem_msb_mask == 0;
1599            let add_ns = !(rem_msb || (is_even && is_tie));
1600
1601            // f32 does not have enough precision to trigger the second branch.
1602            // For example, it can not represent numbers between 1.999_999_880...
1603            // and 2.0. Bigger values result in even smaller precision of the
1604            // fractional part.
1605            let nanos = nanos + add_ns as u32;
1606            if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) {
1607                (secs, nanos)
1608            } else {
1609                (secs + 1, 0)
1610            }
1611        } else if exp < 64 {
1612            // the input has no fractional part
1613            let secs = u64::from(mant) << (exp - $mant_bits);
1614            (secs, 0)
1615        } else {
1616            return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::OverflowOrNan });
1617        };
1618
1619        Ok(Duration::new(secs, nanos))
1620    }};
1621}
1622
1623impl Duration {
1624    /// The checked version of [`from_secs_f32`].
1625    ///
1626    /// [`from_secs_f32`]: Duration::from_secs_f32
1627    ///
1628    /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1629    ///
1630    /// # Examples
1631    /// ```
1632    /// use std::time::Duration;
1633    ///
1634    /// let res = Duration::try_from_secs_f32(0.0);
1635    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1636    /// let res = Duration::try_from_secs_f32(1e-20);
1637    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1638    /// let res = Duration::try_from_secs_f32(4.2e-7);
1639    /// assert_eq!(res, Ok(Duration::new(0, 420)));
1640    /// let res = Duration::try_from_secs_f32(2.7);
1641    /// assert_eq!(res, Ok(Duration::new(2, 700_000_048)));
1642    /// let res = Duration::try_from_secs_f32(3e10);
1643    /// assert_eq!(res, Ok(Duration::new(30_000_001_024, 0)));
1644    /// // subnormal float:
1645    /// let res = Duration::try_from_secs_f32(f32::from_bits(1));
1646    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1647    ///
1648    /// let res = Duration::try_from_secs_f32(-5.0);
1649    /// assert!(res.is_err());
1650    /// let res = Duration::try_from_secs_f32(f32::NAN);
1651    /// assert!(res.is_err());
1652    /// let res = Duration::try_from_secs_f32(2e19);
1653    /// assert!(res.is_err());
1654    ///
1655    /// // the conversion uses rounding with tie resolution to even
1656    /// let res = Duration::try_from_secs_f32(0.999e-9);
1657    /// assert_eq!(res, Ok(Duration::new(0, 1)));
1658    ///
1659    /// // this float represents exactly 976562.5e-9
1660    /// let val = f32::from_bits(0x3A80_0000);
1661    /// let res = Duration::try_from_secs_f32(val);
1662    /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1663    ///
1664    /// // this float represents exactly 2929687.5e-9
1665    /// let val = f32::from_bits(0x3B40_0000);
1666    /// let res = Duration::try_from_secs_f32(val);
1667    /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1668    ///
1669    /// // this float represents exactly 1.000_976_562_5
1670    /// let val = f32::from_bits(0x3F802000);
1671    /// let res = Duration::try_from_secs_f32(val);
1672    /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1673    ///
1674    /// // this float represents exactly 1.002_929_687_5
1675    /// let val = f32::from_bits(0x3F806000);
1676    /// let res = Duration::try_from_secs_f32(val);
1677    /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1678    /// ```
1679    #[stable(feature = "duration_checked_float", since = "1.66.0")]
1680    #[inline]
1681    pub fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError> {
1682        try_from_secs!(
1683            secs = secs,
1684            mantissa_bits = 23,
1685            exponent_bits = 8,
1686            offset = 41,
1687            bits_ty = u32,
1688            double_ty = u64,
1689        )
1690    }
1691
1692    /// The checked version of [`from_secs_f64`].
1693    ///
1694    /// [`from_secs_f64`]: Duration::from_secs_f64
1695    ///
1696    /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1697    ///
1698    /// # Examples
1699    /// ```
1700    /// use std::time::Duration;
1701    ///
1702    /// let res = Duration::try_from_secs_f64(0.0);
1703    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1704    /// let res = Duration::try_from_secs_f64(1e-20);
1705    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1706    /// let res = Duration::try_from_secs_f64(4.2e-7);
1707    /// assert_eq!(res, Ok(Duration::new(0, 420)));
1708    /// let res = Duration::try_from_secs_f64(2.7);
1709    /// assert_eq!(res, Ok(Duration::new(2, 700_000_000)));
1710    /// let res = Duration::try_from_secs_f64(3e10);
1711    /// assert_eq!(res, Ok(Duration::new(30_000_000_000, 0)));
1712    /// // subnormal float
1713    /// let res = Duration::try_from_secs_f64(f64::from_bits(1));
1714    /// assert_eq!(res, Ok(Duration::new(0, 0)));
1715    ///
1716    /// let res = Duration::try_from_secs_f64(-5.0);
1717    /// assert!(res.is_err());
1718    /// let res = Duration::try_from_secs_f64(f64::NAN);
1719    /// assert!(res.is_err());
1720    /// let res = Duration::try_from_secs_f64(2e19);
1721    /// assert!(res.is_err());
1722    ///
1723    /// // the conversion uses rounding with tie resolution to even
1724    /// let res = Duration::try_from_secs_f64(0.999e-9);
1725    /// assert_eq!(res, Ok(Duration::new(0, 1)));
1726    /// let res = Duration::try_from_secs_f64(0.999_999_999_499);
1727    /// assert_eq!(res, Ok(Duration::new(0, 999_999_999)));
1728    /// let res = Duration::try_from_secs_f64(0.999_999_999_501);
1729    /// assert_eq!(res, Ok(Duration::new(1, 0)));
1730    /// let res = Duration::try_from_secs_f64(42.999_999_999_499);
1731    /// assert_eq!(res, Ok(Duration::new(42, 999_999_999)));
1732    /// let res = Duration::try_from_secs_f64(42.999_999_999_501);
1733    /// assert_eq!(res, Ok(Duration::new(43, 0)));
1734    ///
1735    /// // this float represents exactly 976562.5e-9
1736    /// let val = f64::from_bits(0x3F50_0000_0000_0000);
1737    /// let res = Duration::try_from_secs_f64(val);
1738    /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1739    ///
1740    /// // this float represents exactly 2929687.5e-9
1741    /// let val = f64::from_bits(0x3F68_0000_0000_0000);
1742    /// let res = Duration::try_from_secs_f64(val);
1743    /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1744    ///
1745    /// // this float represents exactly 1.000_976_562_5
1746    /// let val = f64::from_bits(0x3FF0_0400_0000_0000);
1747    /// let res = Duration::try_from_secs_f64(val);
1748    /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1749    ///
1750    /// // this float represents exactly 1.002_929_687_5
1751    /// let val = f64::from_bits(0x3_FF00_C000_0000_000);
1752    /// let res = Duration::try_from_secs_f64(val);
1753    /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1754    /// ```
1755    #[stable(feature = "duration_checked_float", since = "1.66.0")]
1756    #[inline]
1757    #[cfg(not(feature = "ferrocene_certified"))]
1758    pub fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError> {
1759        try_from_secs!(
1760            secs = secs,
1761            mantissa_bits = 52,
1762            exponent_bits = 11,
1763            offset = 44,
1764            bits_ty = u64,
1765            double_ty = u128,
1766        )
1767    }
1768}