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 = "CURRENT_RUSTC_VERSION")]
340 #[rustc_const_stable(feature = "duration_from_nanos_u128", since = "CURRENT_RUSTC_VERSION")]
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 #[cfg_attr(feature = "ferrocene_certified_runtime", expect(unused_variables))]
976 #[stable(feature = "duration_float", since = "1.38.0")]
977 #[must_use]
978 #[inline]
979 #[cfg(not(feature = "ferrocene_subset"))]
980 pub fn from_secs_f64(secs: f64) -> Duration {
981 match Duration::try_from_secs_f64(secs) {
982 Ok(v) => v,
983 Err(e) => panic!("{e}"),
984 }
985 }
986
987 /// Creates a new `Duration` from the specified number of seconds represented
988 /// as `f32`.
989 ///
990 /// # Panics
991 /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
992 ///
993 /// # Examples
994 /// ```
995 /// use std::time::Duration;
996 ///
997 /// let res = Duration::from_secs_f32(0.0);
998 /// assert_eq!(res, Duration::new(0, 0));
999 /// let res = Duration::from_secs_f32(1e-20);
1000 /// assert_eq!(res, Duration::new(0, 0));
1001 /// let res = Duration::from_secs_f32(4.2e-7);
1002 /// assert_eq!(res, Duration::new(0, 420));
1003 /// let res = Duration::from_secs_f32(2.7);
1004 /// assert_eq!(res, Duration::new(2, 700_000_048));
1005 /// let res = Duration::from_secs_f32(3e10);
1006 /// assert_eq!(res, Duration::new(30_000_001_024, 0));
1007 /// // subnormal float
1008 /// let res = Duration::from_secs_f32(f32::from_bits(1));
1009 /// assert_eq!(res, Duration::new(0, 0));
1010 /// // conversion uses rounding
1011 /// let res = Duration::from_secs_f32(0.999e-9);
1012 /// assert_eq!(res, Duration::new(0, 1));
1013 /// ```
1014 #[cfg_attr(feature = "ferrocene_certified_runtime", expect(unused_variables))]
1015 #[stable(feature = "duration_float", since = "1.38.0")]
1016 #[must_use]
1017 #[inline]
1018 pub fn from_secs_f32(secs: f32) -> Duration {
1019 match Duration::try_from_secs_f32(secs) {
1020 Ok(v) => v,
1021 Err(e) => panic!("{e}"),
1022 }
1023 }
1024
1025 /// Multiplies `Duration` by `f64`.
1026 ///
1027 /// # Panics
1028 /// This method will panic if result is negative, overflows `Duration` or not finite.
1029 ///
1030 /// # Examples
1031 /// ```
1032 /// use std::time::Duration;
1033 ///
1034 /// let dur = Duration::new(2, 700_000_000);
1035 /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
1036 /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
1037 /// ```
1038 #[stable(feature = "duration_float", since = "1.38.0")]
1039 #[must_use = "this returns the result of the operation, \
1040 without modifying the original"]
1041 #[inline]
1042 #[cfg(not(feature = "ferrocene_subset"))]
1043 pub fn mul_f64(self, rhs: f64) -> Duration {
1044 Duration::from_secs_f64(rhs * self.as_secs_f64())
1045 }
1046
1047 /// Multiplies `Duration` by `f32`.
1048 ///
1049 /// # Panics
1050 /// This method will panic if result is negative, overflows `Duration` or not finite.
1051 ///
1052 /// # Examples
1053 /// ```
1054 /// use std::time::Duration;
1055 ///
1056 /// let dur = Duration::new(2, 700_000_000);
1057 /// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_641));
1058 /// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847_800, 0));
1059 /// ```
1060 #[stable(feature = "duration_float", since = "1.38.0")]
1061 #[must_use = "this returns the result of the operation, \
1062 without modifying the original"]
1063 #[inline]
1064 #[cfg(not(feature = "ferrocene_subset"))]
1065 pub fn mul_f32(self, rhs: f32) -> Duration {
1066 Duration::from_secs_f32(rhs * self.as_secs_f32())
1067 }
1068
1069 /// Divides `Duration` by `f64`.
1070 ///
1071 /// # Panics
1072 /// This method will panic if result is negative, overflows `Duration` or not finite.
1073 ///
1074 /// # Examples
1075 /// ```
1076 /// use std::time::Duration;
1077 ///
1078 /// let dur = Duration::new(2, 700_000_000);
1079 /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
1080 /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_599));
1081 /// ```
1082 #[stable(feature = "duration_float", since = "1.38.0")]
1083 #[must_use = "this returns the result of the operation, \
1084 without modifying the original"]
1085 #[inline]
1086 #[cfg(not(feature = "ferrocene_subset"))]
1087 pub fn div_f64(self, rhs: f64) -> Duration {
1088 Duration::from_secs_f64(self.as_secs_f64() / rhs)
1089 }
1090
1091 /// Divides `Duration` by `f32`.
1092 ///
1093 /// # Panics
1094 /// This method will panic if result is negative, overflows `Duration` or not finite.
1095 ///
1096 /// # Examples
1097 /// ```
1098 /// use std::time::Duration;
1099 ///
1100 /// let dur = Duration::new(2, 700_000_000);
1101 /// // note that due to rounding errors result is slightly
1102 /// // different from 0.859_872_611
1103 /// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_580));
1104 /// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_599));
1105 /// ```
1106 #[stable(feature = "duration_float", since = "1.38.0")]
1107 #[must_use = "this returns the result of the operation, \
1108 without modifying the original"]
1109 #[inline]
1110 #[cfg(not(feature = "ferrocene_subset"))]
1111 pub fn div_f32(self, rhs: f32) -> Duration {
1112 Duration::from_secs_f32(self.as_secs_f32() / rhs)
1113 }
1114
1115 /// Divides `Duration` by `Duration` and returns `f64`.
1116 ///
1117 /// # Examples
1118 /// ```
1119 /// use std::time::Duration;
1120 ///
1121 /// let dur1 = Duration::new(2, 700_000_000);
1122 /// let dur2 = Duration::new(5, 400_000_000);
1123 /// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
1124 /// ```
1125 #[stable(feature = "div_duration", since = "1.80.0")]
1126 #[must_use = "this returns the result of the operation, \
1127 without modifying the original"]
1128 #[inline]
1129 #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1130 pub const fn div_duration_f64(self, rhs: Duration) -> f64 {
1131 let self_nanos =
1132 (self.secs as f64) * (NANOS_PER_SEC as f64) + (self.nanos.as_inner() as f64);
1133 let rhs_nanos = (rhs.secs as f64) * (NANOS_PER_SEC as f64) + (rhs.nanos.as_inner() as f64);
1134 self_nanos / rhs_nanos
1135 }
1136
1137 /// Divides `Duration` by `Duration` and returns `f32`.
1138 ///
1139 /// # Examples
1140 /// ```
1141 /// use std::time::Duration;
1142 ///
1143 /// let dur1 = Duration::new(2, 700_000_000);
1144 /// let dur2 = Duration::new(5, 400_000_000);
1145 /// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
1146 /// ```
1147 #[stable(feature = "div_duration", since = "1.80.0")]
1148 #[must_use = "this returns the result of the operation, \
1149 without modifying the original"]
1150 #[inline]
1151 #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1152 pub const fn div_duration_f32(self, rhs: Duration) -> f32 {
1153 let self_nanos =
1154 (self.secs as f32) * (NANOS_PER_SEC as f32) + (self.nanos.as_inner() as f32);
1155 let rhs_nanos = (rhs.secs as f32) * (NANOS_PER_SEC as f32) + (rhs.nanos.as_inner() as f32);
1156 self_nanos / rhs_nanos
1157 }
1158}
1159
1160#[stable(feature = "duration", since = "1.3.0")]
1161#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1162impl const Add for Duration {
1163 type Output = Duration;
1164
1165 #[inline]
1166 fn add(self, rhs: Duration) -> Duration {
1167 self.checked_add(rhs).expect("overflow when adding durations")
1168 }
1169}
1170
1171#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1172#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1173#[cfg(not(feature = "ferrocene_subset"))]
1174impl const AddAssign for Duration {
1175 #[inline]
1176 fn add_assign(&mut self, rhs: Duration) {
1177 *self = *self + rhs;
1178 }
1179}
1180
1181#[stable(feature = "duration", since = "1.3.0")]
1182#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1183impl const Sub for Duration {
1184 type Output = Duration;
1185
1186 #[inline]
1187 fn sub(self, rhs: Duration) -> Duration {
1188 self.checked_sub(rhs).expect("overflow when subtracting durations")
1189 }
1190}
1191
1192#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1193#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1194#[cfg(not(feature = "ferrocene_subset"))]
1195impl const SubAssign for Duration {
1196 #[inline]
1197 fn sub_assign(&mut self, rhs: Duration) {
1198 *self = *self - rhs;
1199 }
1200}
1201
1202#[stable(feature = "duration", since = "1.3.0")]
1203#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1204#[cfg(not(feature = "ferrocene_subset"))]
1205impl const Mul<u32> for Duration {
1206 type Output = Duration;
1207
1208 #[inline]
1209 fn mul(self, rhs: u32) -> Duration {
1210 self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
1211 }
1212}
1213
1214#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")]
1215#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1216#[cfg(not(feature = "ferrocene_subset"))]
1217impl const Mul<Duration> for u32 {
1218 type Output = Duration;
1219
1220 #[inline]
1221 fn mul(self, rhs: Duration) -> Duration {
1222 rhs * self
1223 }
1224}
1225
1226#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1227#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1228#[cfg(not(feature = "ferrocene_subset"))]
1229impl const MulAssign<u32> for Duration {
1230 #[inline]
1231 fn mul_assign(&mut self, rhs: u32) {
1232 *self = *self * rhs;
1233 }
1234}
1235
1236#[stable(feature = "duration", since = "1.3.0")]
1237#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1238impl const Div<u32> for Duration {
1239 type Output = Duration;
1240
1241 #[inline]
1242 #[track_caller]
1243 fn div(self, rhs: u32) -> Duration {
1244 self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
1245 }
1246}
1247
1248#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1249#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1250#[cfg(not(feature = "ferrocene_subset"))]
1251impl const DivAssign<u32> for Duration {
1252 #[inline]
1253 #[track_caller]
1254 fn div_assign(&mut self, rhs: u32) {
1255 *self = *self / rhs;
1256 }
1257}
1258
1259#[cfg(not(feature = "ferrocene_subset"))]
1260macro_rules! sum_durations {
1261 ($iter:expr) => {{
1262 let mut total_secs: u64 = 0;
1263 let mut total_nanos: u64 = 0;
1264
1265 for entry in $iter {
1266 total_secs =
1267 total_secs.checked_add(entry.secs).expect("overflow in iter::sum over durations");
1268 total_nanos = match total_nanos.checked_add(entry.nanos.as_inner() as u64) {
1269 Some(n) => n,
1270 None => {
1271 total_secs = total_secs
1272 .checked_add(total_nanos / NANOS_PER_SEC as u64)
1273 .expect("overflow in iter::sum over durations");
1274 (total_nanos % NANOS_PER_SEC as u64) + entry.nanos.as_inner() as u64
1275 }
1276 };
1277 }
1278 total_secs = total_secs
1279 .checked_add(total_nanos / NANOS_PER_SEC as u64)
1280 .expect("overflow in iter::sum over durations");
1281 total_nanos = total_nanos % NANOS_PER_SEC as u64;
1282 Duration::new(total_secs, total_nanos as u32)
1283 }};
1284}
1285
1286#[stable(feature = "duration_sum", since = "1.16.0")]
1287#[cfg(not(feature = "ferrocene_subset"))]
1288impl Sum for Duration {
1289 fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
1290 sum_durations!(iter)
1291 }
1292}
1293
1294#[stable(feature = "duration_sum", since = "1.16.0")]
1295#[cfg(not(feature = "ferrocene_subset"))]
1296impl<'a> Sum<&'a Duration> for Duration {
1297 fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
1298 sum_durations!(iter)
1299 }
1300}
1301
1302#[stable(feature = "duration_debug_impl", since = "1.27.0")]
1303#[cfg(not(feature = "ferrocene_subset"))]
1304impl fmt::Debug for Duration {
1305 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1306 /// Formats a floating point number in decimal notation.
1307 ///
1308 /// The number is given as the `integer_part` and a fractional part.
1309 /// The value of the fractional part is `fractional_part / divisor`. So
1310 /// `integer_part` = 3, `fractional_part` = 12 and `divisor` = 100
1311 /// represents the number `3.012`. Trailing zeros are omitted.
1312 ///
1313 /// `divisor` must not be above 100_000_000. It also should be a power
1314 /// of 10, everything else doesn't make sense. `fractional_part` has
1315 /// to be less than `10 * divisor`!
1316 ///
1317 /// A prefix and postfix may be added. The whole thing is padded
1318 /// to the formatter's `width`, if specified.
1319 fn fmt_decimal(
1320 f: &mut fmt::Formatter<'_>,
1321 integer_part: u64,
1322 mut fractional_part: u32,
1323 mut divisor: u32,
1324 prefix: &str,
1325 postfix: &str,
1326 ) -> fmt::Result {
1327 // Encode the fractional part into a temporary buffer. The buffer
1328 // only need to hold 9 elements, because `fractional_part` has to
1329 // be smaller than 10^9. The buffer is prefilled with '0' digits
1330 // to simplify the code below.
1331 let mut buf = [b'0'; 9];
1332
1333 // The next digit is written at this position
1334 let mut pos = 0;
1335
1336 // We keep writing digits into the buffer while there are non-zero
1337 // digits left and we haven't written enough digits yet.
1338 while fractional_part > 0 && pos < f.precision().unwrap_or(9) {
1339 // Write new digit into the buffer
1340 buf[pos] = b'0' + (fractional_part / divisor) as u8;
1341
1342 fractional_part %= divisor;
1343 divisor /= 10;
1344 pos += 1;
1345 }
1346
1347 // If a precision < 9 was specified, there may be some non-zero
1348 // digits left that weren't written into the buffer. In that case we
1349 // need to perform rounding to match the semantics of printing
1350 // normal floating point numbers. However, we only need to do work
1351 // when rounding up. This happens if the first digit of the
1352 // remaining ones is >= 5.
1353 let integer_part = if fractional_part > 0 && fractional_part >= divisor * 5 {
1354 // Round up the number contained in the buffer. We go through
1355 // the buffer backwards and keep track of the carry.
1356 let mut rev_pos = pos;
1357 let mut carry = true;
1358 while carry && rev_pos > 0 {
1359 rev_pos -= 1;
1360
1361 // If the digit in the buffer is not '9', we just need to
1362 // increment it and can stop then (since we don't have a
1363 // carry anymore). Otherwise, we set it to '0' (overflow)
1364 // and continue.
1365 if buf[rev_pos] < b'9' {
1366 buf[rev_pos] += 1;
1367 carry = false;
1368 } else {
1369 buf[rev_pos] = b'0';
1370 }
1371 }
1372
1373 // If we still have the carry bit set, that means that we set
1374 // the whole buffer to '0's and need to increment the integer
1375 // part.
1376 if carry {
1377 // If `integer_part == u64::MAX` and precision < 9, any
1378 // carry of the overflow during rounding of the
1379 // `fractional_part` into the `integer_part` will cause the
1380 // `integer_part` itself to overflow. Avoid this by using an
1381 // `Option<u64>`, with `None` representing `u64::MAX + 1`.
1382 integer_part.checked_add(1)
1383 } else {
1384 Some(integer_part)
1385 }
1386 } else {
1387 Some(integer_part)
1388 };
1389
1390 // Determine the end of the buffer: if precision is set, we just
1391 // use as many digits from the buffer (capped to 9). If it isn't
1392 // set, we only use all digits up to the last non-zero one.
1393 let end = f.precision().map(|p| crate::cmp::min(p, 9)).unwrap_or(pos);
1394
1395 // This closure emits the formatted duration without emitting any
1396 // padding (padding is calculated below).
1397 let emit_without_padding = |f: &mut fmt::Formatter<'_>| {
1398 if let Some(integer_part) = integer_part {
1399 write!(f, "{}{}", prefix, integer_part)?;
1400 } else {
1401 // u64::MAX + 1 == 18446744073709551616
1402 write!(f, "{}18446744073709551616", prefix)?;
1403 }
1404
1405 // Write the decimal point and the fractional part (if any).
1406 if end > 0 {
1407 // SAFETY: We are only writing ASCII digits into the buffer and
1408 // it was initialized with '0's, so it contains valid UTF8.
1409 let s = unsafe { crate::str::from_utf8_unchecked(&buf[..end]) };
1410
1411 // If the user request a precision > 9, we pad '0's at the end.
1412 let w = f.precision().unwrap_or(pos);
1413 write!(f, ".{:0<width$}", s, width = w)?;
1414 }
1415
1416 write!(f, "{}", postfix)
1417 };
1418
1419 match f.width() {
1420 None => {
1421 // No `width` specified. There's no need to calculate the
1422 // length of the output in this case, just emit it.
1423 emit_without_padding(f)
1424 }
1425 Some(requested_w) => {
1426 // A `width` was specified. Calculate the actual width of
1427 // the output in order to calculate the required padding.
1428 // It consists of 4 parts:
1429 // 1. The prefix: is either "+" or "", so we can just use len().
1430 // 2. The postfix: can be "µs" so we have to count UTF8 characters.
1431 let mut actual_w = prefix.len() + postfix.chars().count();
1432 // 3. The integer part:
1433 if let Some(integer_part) = integer_part {
1434 if let Some(log) = integer_part.checked_ilog10() {
1435 // integer_part is > 0, so has length log10(x)+1
1436 actual_w += 1 + log as usize;
1437 } else {
1438 // integer_part is 0, so has length 1.
1439 actual_w += 1;
1440 }
1441 } else {
1442 // integer_part is u64::MAX + 1, so has length 20
1443 actual_w += 20;
1444 }
1445 // 4. The fractional part (if any):
1446 if end > 0 {
1447 let frac_part_w = f.precision().unwrap_or(pos);
1448 actual_w += 1 + frac_part_w;
1449 }
1450
1451 if requested_w <= actual_w {
1452 // Output is already longer than `width`, so don't pad.
1453 emit_without_padding(f)
1454 } else {
1455 // We need to add padding. Use the `Formatter::padding` helper function.
1456 let default_align = fmt::Alignment::Left;
1457 let post_padding =
1458 f.padding((requested_w - actual_w) as u16, default_align)?;
1459 emit_without_padding(f)?;
1460 post_padding.write(f)
1461 }
1462 }
1463 }
1464 }
1465
1466 // Print leading '+' sign if requested
1467 let prefix = if f.sign_plus() { "+" } else { "" };
1468
1469 if self.secs > 0 {
1470 fmt_decimal(f, self.secs, self.nanos.as_inner(), NANOS_PER_SEC / 10, prefix, "s")
1471 } else if self.nanos.as_inner() >= NANOS_PER_MILLI {
1472 fmt_decimal(
1473 f,
1474 (self.nanos.as_inner() / NANOS_PER_MILLI) as u64,
1475 self.nanos.as_inner() % NANOS_PER_MILLI,
1476 NANOS_PER_MILLI / 10,
1477 prefix,
1478 "ms",
1479 )
1480 } else if self.nanos.as_inner() >= NANOS_PER_MICRO {
1481 fmt_decimal(
1482 f,
1483 (self.nanos.as_inner() / NANOS_PER_MICRO) as u64,
1484 self.nanos.as_inner() % NANOS_PER_MICRO,
1485 NANOS_PER_MICRO / 10,
1486 prefix,
1487 "µs",
1488 )
1489 } else {
1490 fmt_decimal(f, self.nanos.as_inner() as u64, 0, 1, prefix, "ns")
1491 }
1492 }
1493}
1494
1495/// An error which can be returned when converting a floating-point value of seconds
1496/// into a [`Duration`].
1497///
1498/// This error is used as the error type for [`Duration::try_from_secs_f32`] and
1499/// [`Duration::try_from_secs_f64`].
1500///
1501/// # Example
1502///
1503/// ```
1504/// use std::time::Duration;
1505///
1506/// if let Err(e) = Duration::try_from_secs_f32(-1.0) {
1507/// println!("Failed conversion to Duration: {e}");
1508/// }
1509/// ```
1510#[cfg_attr(not(feature = "ferrocene_subset"), derive(Debug, Clone, PartialEq, Eq))]
1511#[stable(feature = "duration_checked_float", since = "1.66.0")]
1512pub struct TryFromFloatSecsError {
1513 kind: TryFromFloatSecsErrorKind,
1514}
1515
1516#[stable(feature = "duration_checked_float", since = "1.66.0")]
1517#[cfg(not(feature = "ferrocene_subset"))]
1518impl fmt::Display for TryFromFloatSecsError {
1519 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1520 match self.kind {
1521 TryFromFloatSecsErrorKind::Negative => {
1522 "cannot convert float seconds to Duration: value is negative"
1523 }
1524 TryFromFloatSecsErrorKind::OverflowOrNan => {
1525 "cannot convert float seconds to Duration: value is either too big or NaN"
1526 }
1527 }
1528 .fmt(f)
1529 }
1530}
1531
1532#[cfg_attr(not(feature = "ferrocene_subset"), derive(Debug, Clone, PartialEq, Eq))]
1533enum TryFromFloatSecsErrorKind {
1534 // Value is negative.
1535 Negative,
1536 // Value is either too big to be represented as `Duration` or `NaN`.
1537 OverflowOrNan,
1538}
1539
1540macro_rules! try_from_secs {
1541 (
1542 secs = $secs: expr,
1543 mantissa_bits = $mant_bits: literal,
1544 exponent_bits = $exp_bits: literal,
1545 offset = $offset: literal,
1546 bits_ty = $bits_ty:ty,
1547 double_ty = $double_ty:ty,
1548 ) => {{
1549 const MIN_EXP: i16 = 1 - (1i16 << $exp_bits) / 2;
1550 const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
1551 const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
1552
1553 if $secs < 0.0 {
1554 return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::Negative });
1555 }
1556
1557 let bits = $secs.to_bits();
1558 let mant = (bits & MANT_MASK) | (MANT_MASK + 1);
1559 let exp = ((bits >> $mant_bits) & EXP_MASK) as i16 + MIN_EXP;
1560
1561 let (secs, nanos) = if exp < -31 {
1562 // the input represents less than 1ns and can not be rounded to it
1563 (0u64, 0u32)
1564 } else if exp < 0 {
1565 // the input is less than 1 second
1566 let t = <$double_ty>::from(mant) << ($offset + exp);
1567 let nanos_offset = $mant_bits + $offset;
1568 let nanos_tmp = u128::from(NANOS_PER_SEC) * u128::from(t);
1569 let nanos = (nanos_tmp >> nanos_offset) as u32;
1570
1571 let rem_mask = (1 << nanos_offset) - 1;
1572 let rem_msb_mask = 1 << (nanos_offset - 1);
1573 let rem = nanos_tmp & rem_mask;
1574 let is_tie = rem == rem_msb_mask;
1575 let is_even = (nanos & 1) == 0;
1576 let rem_msb = nanos_tmp & rem_msb_mask == 0;
1577 let add_ns = !(rem_msb || (is_even && is_tie));
1578
1579 // f32 does not have enough precision to trigger the second branch
1580 // since it can not represent numbers between 0.999_999_940_395 and 1.0.
1581 let nanos = nanos + add_ns as u32;
1582 if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) { (0, nanos) } else { (1, 0) }
1583 } else if exp < $mant_bits {
1584 let secs = u64::from(mant >> ($mant_bits - exp));
1585 let t = <$double_ty>::from((mant << exp) & MANT_MASK);
1586 let nanos_offset = $mant_bits;
1587 let nanos_tmp = <$double_ty>::from(NANOS_PER_SEC) * t;
1588 let nanos = (nanos_tmp >> nanos_offset) as u32;
1589
1590 let rem_mask = (1 << nanos_offset) - 1;
1591 let rem_msb_mask = 1 << (nanos_offset - 1);
1592 let rem = nanos_tmp & rem_mask;
1593 let is_tie = rem == rem_msb_mask;
1594 let is_even = (nanos & 1) == 0;
1595 let rem_msb = nanos_tmp & rem_msb_mask == 0;
1596 let add_ns = !(rem_msb || (is_even && is_tie));
1597
1598 // f32 does not have enough precision to trigger the second branch.
1599 // For example, it can not represent numbers between 1.999_999_880...
1600 // and 2.0. Bigger values result in even smaller precision of the
1601 // fractional part.
1602 let nanos = nanos + add_ns as u32;
1603 if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) {
1604 (secs, nanos)
1605 } else {
1606 (secs + 1, 0)
1607 }
1608 } else if exp < 64 {
1609 // the input has no fractional part
1610 let secs = u64::from(mant) << (exp - $mant_bits);
1611 (secs, 0)
1612 } else {
1613 return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::OverflowOrNan });
1614 };
1615
1616 Ok(Duration::new(secs, nanos))
1617 }};
1618}
1619
1620impl Duration {
1621 /// The checked version of [`from_secs_f32`].
1622 ///
1623 /// [`from_secs_f32`]: Duration::from_secs_f32
1624 ///
1625 /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1626 ///
1627 /// # Examples
1628 /// ```
1629 /// use std::time::Duration;
1630 ///
1631 /// let res = Duration::try_from_secs_f32(0.0);
1632 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1633 /// let res = Duration::try_from_secs_f32(1e-20);
1634 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1635 /// let res = Duration::try_from_secs_f32(4.2e-7);
1636 /// assert_eq!(res, Ok(Duration::new(0, 420)));
1637 /// let res = Duration::try_from_secs_f32(2.7);
1638 /// assert_eq!(res, Ok(Duration::new(2, 700_000_048)));
1639 /// let res = Duration::try_from_secs_f32(3e10);
1640 /// assert_eq!(res, Ok(Duration::new(30_000_001_024, 0)));
1641 /// // subnormal float:
1642 /// let res = Duration::try_from_secs_f32(f32::from_bits(1));
1643 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1644 ///
1645 /// let res = Duration::try_from_secs_f32(-5.0);
1646 /// assert!(res.is_err());
1647 /// let res = Duration::try_from_secs_f32(f32::NAN);
1648 /// assert!(res.is_err());
1649 /// let res = Duration::try_from_secs_f32(2e19);
1650 /// assert!(res.is_err());
1651 ///
1652 /// // the conversion uses rounding with tie resolution to even
1653 /// let res = Duration::try_from_secs_f32(0.999e-9);
1654 /// assert_eq!(res, Ok(Duration::new(0, 1)));
1655 ///
1656 /// // this float represents exactly 976562.5e-9
1657 /// let val = f32::from_bits(0x3A80_0000);
1658 /// let res = Duration::try_from_secs_f32(val);
1659 /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1660 ///
1661 /// // this float represents exactly 2929687.5e-9
1662 /// let val = f32::from_bits(0x3B40_0000);
1663 /// let res = Duration::try_from_secs_f32(val);
1664 /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1665 ///
1666 /// // this float represents exactly 1.000_976_562_5
1667 /// let val = f32::from_bits(0x3F802000);
1668 /// let res = Duration::try_from_secs_f32(val);
1669 /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1670 ///
1671 /// // this float represents exactly 1.002_929_687_5
1672 /// let val = f32::from_bits(0x3F806000);
1673 /// let res = Duration::try_from_secs_f32(val);
1674 /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1675 /// ```
1676 #[stable(feature = "duration_checked_float", since = "1.66.0")]
1677 #[inline]
1678 pub fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError> {
1679 try_from_secs!(
1680 secs = secs,
1681 mantissa_bits = 23,
1682 exponent_bits = 8,
1683 offset = 41,
1684 bits_ty = u32,
1685 double_ty = u64,
1686 )
1687 }
1688
1689 /// The checked version of [`from_secs_f64`].
1690 ///
1691 /// [`from_secs_f64`]: Duration::from_secs_f64
1692 ///
1693 /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1694 ///
1695 /// # Examples
1696 /// ```
1697 /// use std::time::Duration;
1698 ///
1699 /// let res = Duration::try_from_secs_f64(0.0);
1700 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1701 /// let res = Duration::try_from_secs_f64(1e-20);
1702 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1703 /// let res = Duration::try_from_secs_f64(4.2e-7);
1704 /// assert_eq!(res, Ok(Duration::new(0, 420)));
1705 /// let res = Duration::try_from_secs_f64(2.7);
1706 /// assert_eq!(res, Ok(Duration::new(2, 700_000_000)));
1707 /// let res = Duration::try_from_secs_f64(3e10);
1708 /// assert_eq!(res, Ok(Duration::new(30_000_000_000, 0)));
1709 /// // subnormal float
1710 /// let res = Duration::try_from_secs_f64(f64::from_bits(1));
1711 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1712 ///
1713 /// let res = Duration::try_from_secs_f64(-5.0);
1714 /// assert!(res.is_err());
1715 /// let res = Duration::try_from_secs_f64(f64::NAN);
1716 /// assert!(res.is_err());
1717 /// let res = Duration::try_from_secs_f64(2e19);
1718 /// assert!(res.is_err());
1719 ///
1720 /// // the conversion uses rounding with tie resolution to even
1721 /// let res = Duration::try_from_secs_f64(0.999e-9);
1722 /// assert_eq!(res, Ok(Duration::new(0, 1)));
1723 /// let res = Duration::try_from_secs_f64(0.999_999_999_499);
1724 /// assert_eq!(res, Ok(Duration::new(0, 999_999_999)));
1725 /// let res = Duration::try_from_secs_f64(0.999_999_999_501);
1726 /// assert_eq!(res, Ok(Duration::new(1, 0)));
1727 /// let res = Duration::try_from_secs_f64(42.999_999_999_499);
1728 /// assert_eq!(res, Ok(Duration::new(42, 999_999_999)));
1729 /// let res = Duration::try_from_secs_f64(42.999_999_999_501);
1730 /// assert_eq!(res, Ok(Duration::new(43, 0)));
1731 ///
1732 /// // this float represents exactly 976562.5e-9
1733 /// let val = f64::from_bits(0x3F50_0000_0000_0000);
1734 /// let res = Duration::try_from_secs_f64(val);
1735 /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1736 ///
1737 /// // this float represents exactly 2929687.5e-9
1738 /// let val = f64::from_bits(0x3F68_0000_0000_0000);
1739 /// let res = Duration::try_from_secs_f64(val);
1740 /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1741 ///
1742 /// // this float represents exactly 1.000_976_562_5
1743 /// let val = f64::from_bits(0x3FF0_0400_0000_0000);
1744 /// let res = Duration::try_from_secs_f64(val);
1745 /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1746 ///
1747 /// // this float represents exactly 1.002_929_687_5
1748 /// let val = f64::from_bits(0x3_FF00_C000_0000_000);
1749 /// let res = Duration::try_from_secs_f64(val);
1750 /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1751 /// ```
1752 #[stable(feature = "duration_checked_float", since = "1.66.0")]
1753 #[inline]
1754 #[cfg(not(feature = "ferrocene_subset"))]
1755 pub fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError> {
1756 try_from_secs!(
1757 secs = secs,
1758 mantissa_bits = 52,
1759 exponent_bits = 11,
1760 offset = 44,
1761 bits_ty = u64,
1762 double_ty = u128,
1763 )
1764 }
1765}