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