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 /// use std::time::Duration;
705 ///
706 /// assert_eq!(Duration::new(0, 0).saturating_add(Duration::new(0, 1)), Duration::new(0, 1));
707 /// assert_eq!(Duration::new(1, 0).saturating_add(Duration::new(u64::MAX, 0)), Duration::MAX);
708 /// ```
709 #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
710 #[must_use = "this returns the result of the operation, \
711 without modifying the original"]
712 #[inline]
713 #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
714 #[cfg(not(feature = "ferrocene_subset"))]
715 pub const fn saturating_add(self, rhs: Duration) -> Duration {
716 match self.checked_add(rhs) {
717 Some(res) => res,
718 None => Duration::MAX,
719 }
720 }
721
722 /// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
723 /// if the result would be negative or if overflow occurred.
724 ///
725 /// # Examples
726 ///
727 /// ```
728 /// use std::time::Duration;
729 ///
730 /// assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
731 /// assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);
732 /// ```
733 #[stable(feature = "duration_checked_ops", since = "1.16.0")]
734 #[must_use = "this returns the result of the operation, \
735 without modifying the original"]
736 #[inline]
737 #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
738 pub const fn checked_sub(self, rhs: Duration) -> Option<Duration> {
739 if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
740 let nanos = if self.nanos.as_inner() >= rhs.nanos.as_inner() {
741 self.nanos.as_inner() - rhs.nanos.as_inner()
742 } else if let Some(sub_secs) = secs.checked_sub(1) {
743 secs = sub_secs;
744 self.nanos.as_inner() + NANOS_PER_SEC - rhs.nanos.as_inner()
745 } else {
746 return None;
747 };
748 debug_assert!(nanos < NANOS_PER_SEC);
749 Some(Duration::new(secs, nanos))
750 } else {
751 None
752 }
753 }
754
755 /// Saturating `Duration` subtraction. Computes `self - other`, returning [`Duration::ZERO`]
756 /// if the result would be negative or if overflow occurred.
757 ///
758 /// # Examples
759 ///
760 /// ```
761 /// use std::time::Duration;
762 ///
763 /// assert_eq!(Duration::new(0, 1).saturating_sub(Duration::new(0, 0)), Duration::new(0, 1));
764 /// assert_eq!(Duration::new(0, 0).saturating_sub(Duration::new(0, 1)), Duration::ZERO);
765 /// ```
766 #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
767 #[must_use = "this returns the result of the operation, \
768 without modifying the original"]
769 #[inline]
770 #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
771 #[cfg(not(feature = "ferrocene_subset"))]
772 pub const fn saturating_sub(self, rhs: Duration) -> Duration {
773 match self.checked_sub(rhs) {
774 Some(res) => res,
775 None => Duration::ZERO,
776 }
777 }
778
779 /// Checked `Duration` multiplication. Computes `self * other`, returning
780 /// [`None`] if overflow occurred.
781 ///
782 /// # Examples
783 ///
784 /// ```
785 /// use std::time::Duration;
786 ///
787 /// assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
788 /// assert_eq!(Duration::new(u64::MAX - 1, 0).checked_mul(2), None);
789 /// ```
790 #[stable(feature = "duration_checked_ops", since = "1.16.0")]
791 #[must_use = "this returns the result of the operation, \
792 without modifying the original"]
793 #[inline]
794 #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
795 #[cfg(not(feature = "ferrocene_subset"))]
796 pub const fn checked_mul(self, rhs: u32) -> Option<Duration> {
797 // Multiply nanoseconds as u64, because it cannot overflow that way.
798 let total_nanos = self.nanos.as_inner() as u64 * rhs as u64;
799 let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
800 let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
801 // FIXME(const-hack): use `and_then` once that is possible.
802 if let Some(s) = self.secs.checked_mul(rhs as u64) {
803 if let Some(secs) = s.checked_add(extra_secs) {
804 debug_assert!(nanos < NANOS_PER_SEC);
805 return Some(Duration::new(secs, nanos));
806 }
807 }
808 None
809 }
810
811 /// Saturating `Duration` multiplication. Computes `self * other`, returning
812 /// [`Duration::MAX`] if overflow occurred.
813 ///
814 /// # Examples
815 ///
816 /// ```
817 /// use std::time::Duration;
818 ///
819 /// assert_eq!(Duration::new(0, 500_000_001).saturating_mul(2), Duration::new(1, 2));
820 /// assert_eq!(Duration::new(u64::MAX - 1, 0).saturating_mul(2), Duration::MAX);
821 /// ```
822 #[stable(feature = "duration_saturating_ops", since = "1.53.0")]
823 #[must_use = "this returns the result of the operation, \
824 without modifying the original"]
825 #[inline]
826 #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
827 #[cfg(not(feature = "ferrocene_subset"))]
828 pub const fn saturating_mul(self, rhs: u32) -> Duration {
829 match self.checked_mul(rhs) {
830 Some(res) => res,
831 None => Duration::MAX,
832 }
833 }
834
835 /// Checked `Duration` division. Computes `self / other`, returning [`None`]
836 /// if `other == 0`.
837 ///
838 /// # Examples
839 ///
840 /// ```
841 /// use std::time::Duration;
842 ///
843 /// assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
844 /// assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
845 /// assert_eq!(Duration::new(2, 0).checked_div(0), None);
846 /// ```
847 #[stable(feature = "duration_checked_ops", since = "1.16.0")]
848 #[must_use = "this returns the result of the operation, \
849 without modifying the original"]
850 #[inline]
851 #[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
852 pub const fn checked_div(self, rhs: u32) -> Option<Duration> {
853 if rhs != 0 {
854 let (secs, extra_secs) = (self.secs / (rhs as u64), self.secs % (rhs as u64));
855 let (mut nanos, extra_nanos) =
856 (self.nanos.as_inner() / rhs, self.nanos.as_inner() % rhs);
857 nanos +=
858 ((extra_secs * (NANOS_PER_SEC as u64) + extra_nanos as u64) / (rhs as u64)) as u32;
859 debug_assert!(nanos < NANOS_PER_SEC);
860 Some(Duration::new(secs, nanos))
861 } else {
862 None
863 }
864 }
865
866 /// Returns the number of seconds contained by this `Duration` as `f64`.
867 ///
868 /// The returned value includes the fractional (nanosecond) part of the duration.
869 ///
870 /// # Examples
871 /// ```
872 /// use std::time::Duration;
873 ///
874 /// let dur = Duration::new(2, 700_000_000);
875 /// assert_eq!(dur.as_secs_f64(), 2.7);
876 /// ```
877 #[stable(feature = "duration_float", since = "1.38.0")]
878 #[must_use]
879 #[inline]
880 #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
881 pub const fn as_secs_f64(&self) -> f64 {
882 (self.secs as f64) + (self.nanos.as_inner() as f64) / (NANOS_PER_SEC as f64)
883 }
884
885 /// Returns the number of seconds contained by this `Duration` as `f32`.
886 ///
887 /// The returned value includes the fractional (nanosecond) part of the duration.
888 ///
889 /// # Examples
890 /// ```
891 /// use std::time::Duration;
892 ///
893 /// let dur = Duration::new(2, 700_000_000);
894 /// assert_eq!(dur.as_secs_f32(), 2.7);
895 /// ```
896 #[stable(feature = "duration_float", since = "1.38.0")]
897 #[must_use]
898 #[inline]
899 #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
900 pub const fn as_secs_f32(&self) -> f32 {
901 (self.secs as f32) + (self.nanos.as_inner() as f32) / (NANOS_PER_SEC as f32)
902 }
903
904 /// Returns the number of milliseconds contained by this `Duration` as `f64`.
905 ///
906 /// The returned value includes the fractional (nanosecond) part of the duration.
907 ///
908 /// # Examples
909 /// ```
910 /// #![feature(duration_millis_float)]
911 /// use std::time::Duration;
912 ///
913 /// let dur = Duration::new(2, 345_678_000);
914 /// assert_eq!(dur.as_millis_f64(), 2_345.678);
915 /// ```
916 #[unstable(feature = "duration_millis_float", issue = "122451")]
917 #[must_use]
918 #[inline]
919 pub const fn as_millis_f64(&self) -> f64 {
920 (self.secs as f64) * (MILLIS_PER_SEC as f64)
921 + (self.nanos.as_inner() as f64) / (NANOS_PER_MILLI as f64)
922 }
923
924 /// Returns the number of milliseconds contained by this `Duration` as `f32`.
925 ///
926 /// The returned value includes the fractional (nanosecond) part of the duration.
927 ///
928 /// # Examples
929 /// ```
930 /// #![feature(duration_millis_float)]
931 /// use std::time::Duration;
932 ///
933 /// let dur = Duration::new(2, 345_678_000);
934 /// assert_eq!(dur.as_millis_f32(), 2_345.678);
935 /// ```
936 #[unstable(feature = "duration_millis_float", issue = "122451")]
937 #[must_use]
938 #[inline]
939 pub const fn as_millis_f32(&self) -> f32 {
940 (self.secs as f32) * (MILLIS_PER_SEC as f32)
941 + (self.nanos.as_inner() as f32) / (NANOS_PER_MILLI as f32)
942 }
943
944 /// Creates a new `Duration` from the specified number of seconds represented
945 /// as `f64`.
946 ///
947 /// # Panics
948 /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
949 ///
950 /// # Examples
951 /// ```
952 /// use std::time::Duration;
953 ///
954 /// let res = Duration::from_secs_f64(0.0);
955 /// assert_eq!(res, Duration::new(0, 0));
956 /// let res = Duration::from_secs_f64(1e-20);
957 /// assert_eq!(res, Duration::new(0, 0));
958 /// let res = Duration::from_secs_f64(4.2e-7);
959 /// assert_eq!(res, Duration::new(0, 420));
960 /// let res = Duration::from_secs_f64(2.7);
961 /// assert_eq!(res, Duration::new(2, 700_000_000));
962 /// let res = Duration::from_secs_f64(3e10);
963 /// assert_eq!(res, Duration::new(30_000_000_000, 0));
964 /// // subnormal float
965 /// let res = Duration::from_secs_f64(f64::from_bits(1));
966 /// assert_eq!(res, Duration::new(0, 0));
967 /// // conversion uses rounding
968 /// let res = Duration::from_secs_f64(0.999e-9);
969 /// assert_eq!(res, Duration::new(0, 1));
970 /// ```
971 #[stable(feature = "duration_float", since = "1.38.0")]
972 #[must_use]
973 #[inline]
974 #[cfg(not(feature = "ferrocene_subset"))]
975 pub fn from_secs_f64(secs: f64) -> Duration {
976 match Duration::try_from_secs_f64(secs) {
977 Ok(v) => v,
978 Err(e) => panic!("{e}"),
979 }
980 }
981
982 /// Creates a new `Duration` from the specified number of seconds represented
983 /// as `f32`.
984 ///
985 /// # Panics
986 /// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
987 ///
988 /// # Examples
989 /// ```
990 /// use std::time::Duration;
991 ///
992 /// let res = Duration::from_secs_f32(0.0);
993 /// assert_eq!(res, Duration::new(0, 0));
994 /// let res = Duration::from_secs_f32(1e-20);
995 /// assert_eq!(res, Duration::new(0, 0));
996 /// let res = Duration::from_secs_f32(4.2e-7);
997 /// assert_eq!(res, Duration::new(0, 420));
998 /// let res = Duration::from_secs_f32(2.7);
999 /// assert_eq!(res, Duration::new(2, 700_000_048));
1000 /// let res = Duration::from_secs_f32(3e10);
1001 /// assert_eq!(res, Duration::new(30_000_001_024, 0));
1002 /// // subnormal float
1003 /// let res = Duration::from_secs_f32(f32::from_bits(1));
1004 /// assert_eq!(res, Duration::new(0, 0));
1005 /// // conversion uses rounding
1006 /// let res = Duration::from_secs_f32(0.999e-9);
1007 /// assert_eq!(res, Duration::new(0, 1));
1008 /// ```
1009 #[stable(feature = "duration_float", since = "1.38.0")]
1010 #[must_use]
1011 #[inline]
1012 pub fn from_secs_f32(secs: f32) -> Duration {
1013 match Duration::try_from_secs_f32(secs) {
1014 Ok(v) => v,
1015 Err(e) => panic!("{e}"),
1016 }
1017 }
1018
1019 /// Multiplies `Duration` by `f64`.
1020 ///
1021 /// # Panics
1022 /// This method will panic if result is negative, overflows `Duration` or not finite.
1023 ///
1024 /// # Examples
1025 /// ```
1026 /// use std::time::Duration;
1027 ///
1028 /// let dur = Duration::new(2, 700_000_000);
1029 /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
1030 /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
1031 /// ```
1032 #[stable(feature = "duration_float", since = "1.38.0")]
1033 #[must_use = "this returns the result of the operation, \
1034 without modifying the original"]
1035 #[inline]
1036 #[cfg(not(feature = "ferrocene_subset"))]
1037 pub fn mul_f64(self, rhs: f64) -> Duration {
1038 Duration::from_secs_f64(rhs * self.as_secs_f64())
1039 }
1040
1041 /// Multiplies `Duration` by `f32`.
1042 ///
1043 /// # Panics
1044 /// This method will panic if result is negative, overflows `Duration` or not finite.
1045 ///
1046 /// # Examples
1047 /// ```
1048 /// use std::time::Duration;
1049 ///
1050 /// let dur = Duration::new(2, 700_000_000);
1051 /// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_641));
1052 /// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847_800, 0));
1053 /// ```
1054 #[stable(feature = "duration_float", since = "1.38.0")]
1055 #[must_use = "this returns the result of the operation, \
1056 without modifying the original"]
1057 #[inline]
1058 #[cfg(not(feature = "ferrocene_subset"))]
1059 pub fn mul_f32(self, rhs: f32) -> Duration {
1060 Duration::from_secs_f32(rhs * self.as_secs_f32())
1061 }
1062
1063 /// Divides `Duration` by `f64`.
1064 ///
1065 /// # Panics
1066 /// This method will panic if result is negative, overflows `Duration` or not finite.
1067 ///
1068 /// # Examples
1069 /// ```
1070 /// use std::time::Duration;
1071 ///
1072 /// let dur = Duration::new(2, 700_000_000);
1073 /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
1074 /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_599));
1075 /// ```
1076 #[stable(feature = "duration_float", since = "1.38.0")]
1077 #[must_use = "this returns the result of the operation, \
1078 without modifying the original"]
1079 #[inline]
1080 #[cfg(not(feature = "ferrocene_subset"))]
1081 pub fn div_f64(self, rhs: f64) -> Duration {
1082 Duration::from_secs_f64(self.as_secs_f64() / rhs)
1083 }
1084
1085 /// Divides `Duration` by `f32`.
1086 ///
1087 /// # Panics
1088 /// This method will panic if result is negative, overflows `Duration` or not finite.
1089 ///
1090 /// # Examples
1091 /// ```
1092 /// use std::time::Duration;
1093 ///
1094 /// let dur = Duration::new(2, 700_000_000);
1095 /// // note that due to rounding errors result is slightly
1096 /// // different from 0.859_872_611
1097 /// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_580));
1098 /// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_599));
1099 /// ```
1100 #[stable(feature = "duration_float", since = "1.38.0")]
1101 #[must_use = "this returns the result of the operation, \
1102 without modifying the original"]
1103 #[inline]
1104 #[cfg(not(feature = "ferrocene_subset"))]
1105 pub fn div_f32(self, rhs: f32) -> Duration {
1106 Duration::from_secs_f32(self.as_secs_f32() / rhs)
1107 }
1108
1109 /// Divides `Duration` by `Duration` and returns `f64`.
1110 ///
1111 /// # Examples
1112 /// ```
1113 /// use std::time::Duration;
1114 ///
1115 /// let dur1 = Duration::new(2, 700_000_000);
1116 /// let dur2 = Duration::new(5, 400_000_000);
1117 /// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
1118 /// ```
1119 #[stable(feature = "div_duration", since = "1.80.0")]
1120 #[must_use = "this returns the result of the operation, \
1121 without modifying the original"]
1122 #[inline]
1123 #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1124 pub const fn div_duration_f64(self, rhs: Duration) -> f64 {
1125 let self_nanos =
1126 (self.secs as f64) * (NANOS_PER_SEC as f64) + (self.nanos.as_inner() as f64);
1127 let rhs_nanos = (rhs.secs as f64) * (NANOS_PER_SEC as f64) + (rhs.nanos.as_inner() as f64);
1128 self_nanos / rhs_nanos
1129 }
1130
1131 /// Divides `Duration` by `Duration` and returns `f32`.
1132 ///
1133 /// # Examples
1134 /// ```
1135 /// use std::time::Duration;
1136 ///
1137 /// let dur1 = Duration::new(2, 700_000_000);
1138 /// let dur2 = Duration::new(5, 400_000_000);
1139 /// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
1140 /// ```
1141 #[stable(feature = "div_duration", since = "1.80.0")]
1142 #[must_use = "this returns the result of the operation, \
1143 without modifying the original"]
1144 #[inline]
1145 #[rustc_const_stable(feature = "duration_consts_float", since = "1.83.0")]
1146 pub const fn div_duration_f32(self, rhs: Duration) -> f32 {
1147 let self_nanos =
1148 (self.secs as f32) * (NANOS_PER_SEC as f32) + (self.nanos.as_inner() as f32);
1149 let rhs_nanos = (rhs.secs as f32) * (NANOS_PER_SEC as f32) + (rhs.nanos.as_inner() as f32);
1150 self_nanos / rhs_nanos
1151 }
1152
1153 /// Divides `Duration` by `Duration` and returns `u128`, rounding the result towards zero.
1154 ///
1155 /// # Examples
1156 /// ```
1157 /// #![feature(duration_integer_division)]
1158 /// use std::time::Duration;
1159 ///
1160 /// let dur = Duration::new(2, 0);
1161 /// assert_eq!(dur.div_duration_floor(Duration::new(1, 000_000_001)), 1);
1162 /// assert_eq!(dur.div_duration_floor(Duration::new(1, 000_000_000)), 2);
1163 /// assert_eq!(dur.div_duration_floor(Duration::new(0, 999_999_999)), 2);
1164 /// ```
1165 #[cfg(not(feature = "ferrocene_subset"))]
1166 #[unstable(feature = "duration_integer_division", issue = "149573")]
1167 #[must_use = "this returns the result of the operation, \
1168 without modifying the original"]
1169 #[inline]
1170 pub const fn div_duration_floor(self, rhs: Duration) -> u128 {
1171 self.as_nanos().div_floor(rhs.as_nanos())
1172 }
1173
1174 /// Divides `Duration` by `Duration` and returns `u128`, rounding the result towards positive infinity.
1175 ///
1176 /// # Examples
1177 /// ```
1178 /// #![feature(duration_integer_division)]
1179 /// use std::time::Duration;
1180 ///
1181 /// let dur = Duration::new(2, 0);
1182 /// assert_eq!(dur.div_duration_ceil(Duration::new(1, 000_000_001)), 2);
1183 /// assert_eq!(dur.div_duration_ceil(Duration::new(1, 000_000_000)), 2);
1184 /// assert_eq!(dur.div_duration_ceil(Duration::new(0, 999_999_999)), 3);
1185 /// ```
1186 #[cfg(not(feature = "ferrocene_subset"))]
1187 #[unstable(feature = "duration_integer_division", issue = "149573")]
1188 #[must_use = "this returns the result of the operation, \
1189 without modifying the original"]
1190 #[inline]
1191 pub const fn div_duration_ceil(self, rhs: Duration) -> u128 {
1192 self.as_nanos().div_ceil(rhs.as_nanos())
1193 }
1194}
1195
1196#[stable(feature = "duration", since = "1.3.0")]
1197#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1198impl const Add for Duration {
1199 type Output = Duration;
1200
1201 #[inline]
1202 fn add(self, rhs: Duration) -> Duration {
1203 self.checked_add(rhs).expect("overflow when adding durations")
1204 }
1205}
1206
1207#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1208#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1209#[cfg(not(feature = "ferrocene_subset"))]
1210impl const AddAssign for Duration {
1211 #[inline]
1212 fn add_assign(&mut self, rhs: Duration) {
1213 *self = *self + rhs;
1214 }
1215}
1216
1217#[stable(feature = "duration", since = "1.3.0")]
1218#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1219impl const Sub for Duration {
1220 type Output = Duration;
1221
1222 #[inline]
1223 fn sub(self, rhs: Duration) -> Duration {
1224 self.checked_sub(rhs).expect("overflow when subtracting durations")
1225 }
1226}
1227
1228#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1229#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1230#[cfg(not(feature = "ferrocene_subset"))]
1231impl const SubAssign for Duration {
1232 #[inline]
1233 fn sub_assign(&mut self, rhs: Duration) {
1234 *self = *self - rhs;
1235 }
1236}
1237
1238#[stable(feature = "duration", since = "1.3.0")]
1239#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1240#[cfg(not(feature = "ferrocene_subset"))]
1241impl const Mul<u32> for Duration {
1242 type Output = Duration;
1243
1244 #[inline]
1245 fn mul(self, rhs: u32) -> Duration {
1246 self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
1247 }
1248}
1249
1250#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")]
1251#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1252#[cfg(not(feature = "ferrocene_subset"))]
1253impl const Mul<Duration> for u32 {
1254 type Output = Duration;
1255
1256 #[inline]
1257 fn mul(self, rhs: Duration) -> Duration {
1258 rhs * self
1259 }
1260}
1261
1262#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1263#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1264#[cfg(not(feature = "ferrocene_subset"))]
1265impl const MulAssign<u32> for Duration {
1266 #[inline]
1267 fn mul_assign(&mut self, rhs: u32) {
1268 *self = *self * rhs;
1269 }
1270}
1271
1272#[stable(feature = "duration", since = "1.3.0")]
1273#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1274impl const Div<u32> for Duration {
1275 type Output = Duration;
1276
1277 #[inline]
1278 #[track_caller]
1279 fn div(self, rhs: u32) -> Duration {
1280 self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
1281 }
1282}
1283
1284#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
1285#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1286#[cfg(not(feature = "ferrocene_subset"))]
1287impl const DivAssign<u32> for Duration {
1288 #[inline]
1289 #[track_caller]
1290 fn div_assign(&mut self, rhs: u32) {
1291 *self = *self / rhs;
1292 }
1293}
1294
1295#[cfg(not(feature = "ferrocene_subset"))]
1296macro_rules! sum_durations {
1297 ($iter:expr) => {{
1298 let mut total_secs: u64 = 0;
1299 let mut total_nanos: u64 = 0;
1300
1301 for entry in $iter {
1302 total_secs =
1303 total_secs.checked_add(entry.secs).expect("overflow in iter::sum over durations");
1304 total_nanos = match total_nanos.checked_add(entry.nanos.as_inner() as u64) {
1305 Some(n) => n,
1306 None => {
1307 total_secs = total_secs
1308 .checked_add(total_nanos / NANOS_PER_SEC as u64)
1309 .expect("overflow in iter::sum over durations");
1310 (total_nanos % NANOS_PER_SEC as u64) + entry.nanos.as_inner() as u64
1311 }
1312 };
1313 }
1314 total_secs = total_secs
1315 .checked_add(total_nanos / NANOS_PER_SEC as u64)
1316 .expect("overflow in iter::sum over durations");
1317 total_nanos = total_nanos % NANOS_PER_SEC as u64;
1318 Duration::new(total_secs, total_nanos as u32)
1319 }};
1320}
1321
1322#[stable(feature = "duration_sum", since = "1.16.0")]
1323#[cfg(not(feature = "ferrocene_subset"))]
1324impl Sum for Duration {
1325 fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
1326 sum_durations!(iter)
1327 }
1328}
1329
1330#[stable(feature = "duration_sum", since = "1.16.0")]
1331#[cfg(not(feature = "ferrocene_subset"))]
1332impl<'a> Sum<&'a Duration> for Duration {
1333 fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
1334 sum_durations!(iter)
1335 }
1336}
1337
1338#[stable(feature = "duration_debug_impl", since = "1.27.0")]
1339impl fmt::Debug for Duration {
1340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1341 /// Formats a floating point number in decimal notation.
1342 ///
1343 /// The number is given as the `integer_part` and a fractional part.
1344 /// The value of the fractional part is `fractional_part / divisor`. So
1345 /// `integer_part` = 3, `fractional_part` = 12 and `divisor` = 100
1346 /// represents the number `3.012`. Trailing zeros are omitted.
1347 ///
1348 /// `divisor` must not be above 100_000_000. It also should be a power
1349 /// of 10, everything else doesn't make sense. `fractional_part` has
1350 /// to be less than `10 * divisor`!
1351 ///
1352 /// A prefix and postfix may be added. The whole thing is padded
1353 /// to the formatter's `width`, if specified.
1354 fn fmt_decimal(
1355 f: &mut fmt::Formatter<'_>,
1356 integer_part: u64,
1357 mut fractional_part: u32,
1358 mut divisor: u32,
1359 prefix: &str,
1360 postfix: &str,
1361 ) -> fmt::Result {
1362 // Encode the fractional part into a temporary buffer. The buffer
1363 // only need to hold 9 elements, because `fractional_part` has to
1364 // be smaller than 10^9. The buffer is prefilled with '0' digits
1365 // to simplify the code below.
1366 let mut buf = [b'0'; 9];
1367
1368 // The next digit is written at this position
1369 let mut pos = 0;
1370
1371 // We keep writing digits into the buffer while there are non-zero
1372 // digits left and we haven't written enough digits yet.
1373 while fractional_part > 0 && pos < f.precision().unwrap_or(9) {
1374 // Write new digit into the buffer
1375 buf[pos] = b'0' + (fractional_part / divisor) as u8;
1376
1377 fractional_part %= divisor;
1378 divisor /= 10;
1379 pos += 1;
1380 }
1381
1382 // If a precision < 9 was specified, there may be some non-zero
1383 // digits left that weren't written into the buffer. In that case we
1384 // need to perform rounding to match the semantics of printing
1385 // normal floating point numbers. However, we only need to do work
1386 // when rounding up. This happens if the first digit of the
1387 // remaining ones is >= 5. When the first digit is exactly 5, rounding
1388 // follows IEEE-754 round-ties-to-even semantics: we only round up
1389 // if the last written digit is odd.
1390 let integer_part = if fractional_part > 0 && fractional_part >= divisor * 5 {
1391 // For ties (fractional_part == divisor * 5), only round up if last digit is odd
1392 let is_tie = fractional_part == divisor * 5;
1393 let last_digit_is_odd = if pos > 0 {
1394 (buf[pos - 1] - b'0') % 2 == 1
1395 } else {
1396 // No fractional digits - check the integer part
1397 (integer_part % 2) == 1
1398 };
1399
1400 if is_tie && !last_digit_is_odd {
1401 Some(integer_part)
1402 } else {
1403 // Round up the number contained in the buffer. We go through
1404 // the buffer backwards and keep track of the carry.
1405 let mut rev_pos = pos;
1406 let mut carry = true;
1407 while carry && rev_pos > 0 {
1408 rev_pos -= 1;
1409
1410 // If the digit in the buffer is not '9', we just need to
1411 // increment it and can stop then (since we don't have a
1412 // carry anymore). Otherwise, we set it to '0' (overflow)
1413 // and continue.
1414 if buf[rev_pos] < b'9' {
1415 buf[rev_pos] += 1;
1416 carry = false;
1417 } else {
1418 buf[rev_pos] = b'0';
1419 }
1420 }
1421
1422 // If we still have the carry bit set, that means that we set
1423 // the whole buffer to '0's and need to increment the integer
1424 // part.
1425 if carry {
1426 // If `integer_part == u64::MAX` and precision < 9, any
1427 // carry of the overflow during rounding of the
1428 // `fractional_part` into the `integer_part` will cause the
1429 // `integer_part` itself to overflow. Avoid this by using an
1430 // `Option<u64>`, with `None` representing `u64::MAX + 1`.
1431 integer_part.checked_add(1)
1432 } else {
1433 Some(integer_part)
1434 }
1435 }
1436 } else {
1437 Some(integer_part)
1438 };
1439
1440 // Determine the end of the buffer: if precision is set, we just
1441 // use as many digits from the buffer (capped to 9). If it isn't
1442 // set, we only use all digits up to the last non-zero one.
1443 let end = f.precision().map(|p| crate::cmp::min(p, 9)).unwrap_or(pos);
1444
1445 // This closure emits the formatted duration without emitting any
1446 // padding (padding is calculated below).
1447 let emit_without_padding = |f: &mut fmt::Formatter<'_>| {
1448 if let Some(integer_part) = integer_part {
1449 write!(f, "{}{}", prefix, integer_part)?;
1450 } else {
1451 // u64::MAX + 1 == 18446744073709551616
1452 write!(f, "{}18446744073709551616", prefix)?;
1453 }
1454
1455 // Write the decimal point and the fractional part (if any).
1456 if end > 0 {
1457 // SAFETY: We are only writing ASCII digits into the buffer and
1458 // it was initialized with '0's, so it contains valid UTF8.
1459 let s = unsafe { crate::str::from_utf8_unchecked(&buf[..end]) };
1460
1461 // If the user request a precision > 9, we pad '0's at the end.
1462 let w = f.precision().unwrap_or(pos);
1463 write!(f, ".{:0<width$}", s, width = w)?;
1464 }
1465
1466 write!(f, "{}", postfix)
1467 };
1468
1469 match f.width() {
1470 None => {
1471 // No `width` specified. There's no need to calculate the
1472 // length of the output in this case, just emit it.
1473 emit_without_padding(f)
1474 }
1475 Some(requested_w) => {
1476 // A `width` was specified. Calculate the actual width of
1477 // the output in order to calculate the required padding.
1478 // It consists of 4 parts:
1479 // 1. The prefix: is either "+" or "", so we can just use len().
1480 // 2. The postfix: can be "µs" so we have to count UTF8 characters.
1481 let mut actual_w = prefix.len() + postfix.chars().count();
1482 // 3. The integer part:
1483 if let Some(integer_part) = integer_part {
1484 if let Some(log) = integer_part.checked_ilog10() {
1485 // integer_part is > 0, so has length log10(x)+1
1486 actual_w += 1 + log as usize;
1487 } else {
1488 // integer_part is 0, so has length 1.
1489 actual_w += 1;
1490 }
1491 } else {
1492 // integer_part is u64::MAX + 1, so has length 20
1493 actual_w += 20;
1494 }
1495 // 4. The fractional part (if any):
1496 if end > 0 {
1497 let frac_part_w = f.precision().unwrap_or(pos);
1498 actual_w += 1 + frac_part_w;
1499 }
1500
1501 if requested_w <= actual_w {
1502 // Output is already longer than `width`, so don't pad.
1503 emit_without_padding(f)
1504 } else {
1505 // We need to add padding. Use the `Formatter::padding` helper function.
1506 let default_align = fmt::Alignment::Left;
1507 let post_padding =
1508 f.padding((requested_w - actual_w) as u16, default_align)?;
1509 emit_without_padding(f)?;
1510 post_padding.write(f)
1511 }
1512 }
1513 }
1514 }
1515
1516 // Print leading '+' sign if requested
1517 let prefix = if f.sign_plus() { "+" } else { "" };
1518
1519 if self.secs > 0 {
1520 fmt_decimal(f, self.secs, self.nanos.as_inner(), NANOS_PER_SEC / 10, prefix, "s")
1521 } else if self.nanos.as_inner() >= NANOS_PER_MILLI {
1522 fmt_decimal(
1523 f,
1524 (self.nanos.as_inner() / NANOS_PER_MILLI) as u64,
1525 self.nanos.as_inner() % NANOS_PER_MILLI,
1526 NANOS_PER_MILLI / 10,
1527 prefix,
1528 "ms",
1529 )
1530 } else if self.nanos.as_inner() >= NANOS_PER_MICRO {
1531 fmt_decimal(
1532 f,
1533 (self.nanos.as_inner() / NANOS_PER_MICRO) as u64,
1534 self.nanos.as_inner() % NANOS_PER_MICRO,
1535 NANOS_PER_MICRO / 10,
1536 prefix,
1537 "µs",
1538 )
1539 } else {
1540 fmt_decimal(f, self.nanos.as_inner() as u64, 0, 1, prefix, "ns")
1541 }
1542 }
1543}
1544
1545/// An error which can be returned when converting a floating-point value of seconds
1546/// into a [`Duration`].
1547///
1548/// This error is used as the error type for [`Duration::try_from_secs_f32`] and
1549/// [`Duration::try_from_secs_f64`].
1550///
1551/// # Example
1552///
1553/// ```
1554/// use std::time::Duration;
1555///
1556/// if let Err(e) = Duration::try_from_secs_f32(-1.0) {
1557/// println!("Failed conversion to Duration: {e}");
1558/// }
1559/// ```
1560#[derive(Debug, Clone, PartialEq, Eq)]
1561#[stable(feature = "duration_checked_float", since = "1.66.0")]
1562pub struct TryFromFloatSecsError {
1563 kind: TryFromFloatSecsErrorKind,
1564}
1565
1566#[stable(feature = "duration_checked_float", since = "1.66.0")]
1567impl fmt::Display for TryFromFloatSecsError {
1568 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1569 match self.kind {
1570 TryFromFloatSecsErrorKind::Negative => {
1571 "cannot convert float seconds to Duration: value is negative"
1572 }
1573 TryFromFloatSecsErrorKind::OverflowOrNan => {
1574 "cannot convert float seconds to Duration: value is either too big or NaN"
1575 }
1576 }
1577 .fmt(f)
1578 }
1579}
1580
1581#[derive(Debug, Clone, PartialEq, Eq)]
1582enum TryFromFloatSecsErrorKind {
1583 // Value is negative.
1584 Negative,
1585 // Value is either too big to be represented as `Duration` or `NaN`.
1586 OverflowOrNan,
1587}
1588
1589macro_rules! try_from_secs {
1590 (
1591 secs = $secs: expr,
1592 mantissa_bits = $mant_bits: literal,
1593 exponent_bits = $exp_bits: literal,
1594 offset = $offset: literal,
1595 bits_ty = $bits_ty:ty,
1596 double_ty = $double_ty:ty,
1597 ) => {{
1598 const MIN_EXP: i16 = 1 - (1i16 << $exp_bits) / 2;
1599 const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
1600 const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
1601
1602 if $secs < 0.0 {
1603 return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::Negative });
1604 }
1605
1606 let bits = $secs.to_bits();
1607 let mant = (bits & MANT_MASK) | (MANT_MASK + 1);
1608 let exp = ((bits >> $mant_bits) & EXP_MASK) as i16 + MIN_EXP;
1609
1610 let (secs, nanos) = if exp < -31 {
1611 // the input represents less than 1ns and can not be rounded to it
1612 (0u64, 0u32)
1613 } else if exp < 0 {
1614 // the input is less than 1 second
1615 let t = <$double_ty>::from(mant) << ($offset + exp);
1616 let nanos_offset = $mant_bits + $offset;
1617 let nanos_tmp = u128::from(NANOS_PER_SEC) * u128::from(t);
1618 let nanos = (nanos_tmp >> nanos_offset) as u32;
1619
1620 let rem_mask = (1 << nanos_offset) - 1;
1621 let rem_msb_mask = 1 << (nanos_offset - 1);
1622 let rem = nanos_tmp & rem_mask;
1623 let is_tie = rem == rem_msb_mask;
1624 let is_even = (nanos & 1) == 0;
1625 let rem_msb = nanos_tmp & rem_msb_mask == 0;
1626 let add_ns = !(rem_msb || (is_even && is_tie));
1627
1628 // f32 does not have enough precision to trigger the second branch
1629 // since it can not represent numbers between 0.999_999_940_395 and 1.0.
1630 let nanos = nanos + add_ns as u32;
1631 if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) { (0, nanos) } else { (1, 0) }
1632 } else if exp < $mant_bits {
1633 let secs = u64::from(mant >> ($mant_bits - exp));
1634 let t = <$double_ty>::from((mant << exp) & MANT_MASK);
1635 let nanos_offset = $mant_bits;
1636 let nanos_tmp = <$double_ty>::from(NANOS_PER_SEC) * t;
1637 let nanos = (nanos_tmp >> nanos_offset) as u32;
1638
1639 let rem_mask = (1 << nanos_offset) - 1;
1640 let rem_msb_mask = 1 << (nanos_offset - 1);
1641 let rem = nanos_tmp & rem_mask;
1642 let is_tie = rem == rem_msb_mask;
1643 let is_even = (nanos & 1) == 0;
1644 let rem_msb = nanos_tmp & rem_msb_mask == 0;
1645 let add_ns = !(rem_msb || (is_even && is_tie));
1646
1647 // f32 does not have enough precision to trigger the second branch.
1648 // For example, it can not represent numbers between 1.999_999_880...
1649 // and 2.0. Bigger values result in even smaller precision of the
1650 // fractional part.
1651 let nanos = nanos + add_ns as u32;
1652 if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) {
1653 (secs, nanos)
1654 } else {
1655 (secs + 1, 0)
1656 }
1657 } else if exp < 64 {
1658 // the input has no fractional part
1659 let secs = u64::from(mant) << (exp - $mant_bits);
1660 (secs, 0)
1661 } else {
1662 return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::OverflowOrNan });
1663 };
1664
1665 Ok(Duration::new(secs, nanos))
1666 }};
1667}
1668
1669impl Duration {
1670 /// The checked version of [`from_secs_f32`].
1671 ///
1672 /// [`from_secs_f32`]: Duration::from_secs_f32
1673 ///
1674 /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1675 ///
1676 /// # Examples
1677 /// ```
1678 /// use std::time::Duration;
1679 ///
1680 /// let res = Duration::try_from_secs_f32(0.0);
1681 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1682 /// let res = Duration::try_from_secs_f32(1e-20);
1683 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1684 /// let res = Duration::try_from_secs_f32(4.2e-7);
1685 /// assert_eq!(res, Ok(Duration::new(0, 420)));
1686 /// let res = Duration::try_from_secs_f32(2.7);
1687 /// assert_eq!(res, Ok(Duration::new(2, 700_000_048)));
1688 /// let res = Duration::try_from_secs_f32(3e10);
1689 /// assert_eq!(res, Ok(Duration::new(30_000_001_024, 0)));
1690 /// // subnormal float:
1691 /// let res = Duration::try_from_secs_f32(f32::from_bits(1));
1692 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1693 ///
1694 /// let res = Duration::try_from_secs_f32(-5.0);
1695 /// assert!(res.is_err());
1696 /// let res = Duration::try_from_secs_f32(f32::NAN);
1697 /// assert!(res.is_err());
1698 /// let res = Duration::try_from_secs_f32(2e19);
1699 /// assert!(res.is_err());
1700 ///
1701 /// // the conversion uses rounding with tie resolution to even
1702 /// let res = Duration::try_from_secs_f32(0.999e-9);
1703 /// assert_eq!(res, Ok(Duration::new(0, 1)));
1704 ///
1705 /// // this float represents exactly 976562.5e-9
1706 /// let val = f32::from_bits(0x3A80_0000);
1707 /// let res = Duration::try_from_secs_f32(val);
1708 /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1709 ///
1710 /// // this float represents exactly 2929687.5e-9
1711 /// let val = f32::from_bits(0x3B40_0000);
1712 /// let res = Duration::try_from_secs_f32(val);
1713 /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1714 ///
1715 /// // this float represents exactly 1.000_976_562_5
1716 /// let val = f32::from_bits(0x3F802000);
1717 /// let res = Duration::try_from_secs_f32(val);
1718 /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1719 ///
1720 /// // this float represents exactly 1.002_929_687_5
1721 /// let val = f32::from_bits(0x3F806000);
1722 /// let res = Duration::try_from_secs_f32(val);
1723 /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1724 /// ```
1725 #[stable(feature = "duration_checked_float", since = "1.66.0")]
1726 #[inline]
1727 #[ferrocene::annotation("`mantissa_bits` cannot be covered due to a coverage tooling bug")]
1728 pub fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError> {
1729 try_from_secs!(
1730 secs = secs,
1731 mantissa_bits = 23,
1732 exponent_bits = 8,
1733 offset = 41,
1734 bits_ty = u32,
1735 double_ty = u64,
1736 )
1737 }
1738
1739 /// The checked version of [`from_secs_f64`].
1740 ///
1741 /// [`from_secs_f64`]: Duration::from_secs_f64
1742 ///
1743 /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
1744 ///
1745 /// # Examples
1746 /// ```
1747 /// use std::time::Duration;
1748 ///
1749 /// let res = Duration::try_from_secs_f64(0.0);
1750 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1751 /// let res = Duration::try_from_secs_f64(1e-20);
1752 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1753 /// let res = Duration::try_from_secs_f64(4.2e-7);
1754 /// assert_eq!(res, Ok(Duration::new(0, 420)));
1755 /// let res = Duration::try_from_secs_f64(2.7);
1756 /// assert_eq!(res, Ok(Duration::new(2, 700_000_000)));
1757 /// let res = Duration::try_from_secs_f64(3e10);
1758 /// assert_eq!(res, Ok(Duration::new(30_000_000_000, 0)));
1759 /// // subnormal float
1760 /// let res = Duration::try_from_secs_f64(f64::from_bits(1));
1761 /// assert_eq!(res, Ok(Duration::new(0, 0)));
1762 ///
1763 /// let res = Duration::try_from_secs_f64(-5.0);
1764 /// assert!(res.is_err());
1765 /// let res = Duration::try_from_secs_f64(f64::NAN);
1766 /// assert!(res.is_err());
1767 /// let res = Duration::try_from_secs_f64(2e19);
1768 /// assert!(res.is_err());
1769 ///
1770 /// // the conversion uses rounding with tie resolution to even
1771 /// let res = Duration::try_from_secs_f64(0.999e-9);
1772 /// assert_eq!(res, Ok(Duration::new(0, 1)));
1773 /// let res = Duration::try_from_secs_f64(0.999_999_999_499);
1774 /// assert_eq!(res, Ok(Duration::new(0, 999_999_999)));
1775 /// let res = Duration::try_from_secs_f64(0.999_999_999_501);
1776 /// assert_eq!(res, Ok(Duration::new(1, 0)));
1777 /// let res = Duration::try_from_secs_f64(42.999_999_999_499);
1778 /// assert_eq!(res, Ok(Duration::new(42, 999_999_999)));
1779 /// let res = Duration::try_from_secs_f64(42.999_999_999_501);
1780 /// assert_eq!(res, Ok(Duration::new(43, 0)));
1781 ///
1782 /// // this float represents exactly 976562.5e-9
1783 /// let val = f64::from_bits(0x3F50_0000_0000_0000);
1784 /// let res = Duration::try_from_secs_f64(val);
1785 /// assert_eq!(res, Ok(Duration::new(0, 976_562)));
1786 ///
1787 /// // this float represents exactly 2929687.5e-9
1788 /// let val = f64::from_bits(0x3F68_0000_0000_0000);
1789 /// let res = Duration::try_from_secs_f64(val);
1790 /// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
1791 ///
1792 /// // this float represents exactly 1.000_976_562_5
1793 /// let val = f64::from_bits(0x3FF0_0400_0000_0000);
1794 /// let res = Duration::try_from_secs_f64(val);
1795 /// assert_eq!(res, Ok(Duration::new(1, 976_562)));
1796 ///
1797 /// // this float represents exactly 1.002_929_687_5
1798 /// let val = f64::from_bits(0x3_FF00_C000_0000_000);
1799 /// let res = Duration::try_from_secs_f64(val);
1800 /// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
1801 /// ```
1802 #[stable(feature = "duration_checked_float", since = "1.66.0")]
1803 #[inline]
1804 #[cfg(not(feature = "ferrocene_subset"))]
1805 pub fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError> {
1806 try_from_secs!(
1807 secs = secs,
1808 mantissa_bits = 52,
1809 exponent_bits = 11,
1810 offset = 44,
1811 bits_ty = u64,
1812 double_ty = u128,
1813 )
1814 }
1815}