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