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