2018-02-10 21:20:42 +00:00
|
|
|
#![stable(feature = "duration_core", since = "1.25.0")]
|
2017-12-11 18:42:01 +00:00
|
|
|
|
|
|
|
//! Temporal quantification.
|
|
|
|
//!
|
2021-08-29 18:58:20 +00:00
|
|
|
//! # Examples:
|
|
|
|
//!
|
|
|
|
//! There are multiple ways to create a new [`Duration`]:
|
2017-12-11 18:42:01 +00:00
|
|
|
//!
|
|
|
|
//! ```
|
2021-08-29 18:58:20 +00:00
|
|
|
//! # use std::time::Duration;
|
|
|
|
//! let five_seconds = Duration::from_secs(5);
|
|
|
|
//! assert_eq!(five_seconds, Duration::from_millis(5_000));
|
|
|
|
//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
|
|
|
|
//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
|
2017-12-11 18:42:01 +00:00
|
|
|
//!
|
2021-08-29 18:58:20 +00:00
|
|
|
//! let ten_seconds = Duration::from_secs(10);
|
|
|
|
//! let seven_nanos = Duration::from_nanos(7);
|
|
|
|
//! let total = ten_seconds + seven_nanos;
|
|
|
|
//! assert_eq!(total, Duration::new(10, 7));
|
2017-12-11 18:42:01 +00:00
|
|
|
//! ```
|
2014-07-15 02:18:07 +00:00
|
|
|
|
2020-04-16 22:43:04 +00:00
|
|
|
use crate::fmt;
|
2019-04-15 02:23:21 +00:00
|
|
|
use crate::iter::Sum;
|
|
|
|
use crate::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
2015-04-28 18:40:04 +00:00
|
|
|
|
|
|
|
const NANOS_PER_SEC: u32 = 1_000_000_000;
|
|
|
|
const NANOS_PER_MILLI: u32 = 1_000_000;
|
2017-09-10 15:38:55 +00:00
|
|
|
const NANOS_PER_MICRO: u32 = 1_000;
|
2015-04-28 18:40:04 +00:00
|
|
|
const MILLIS_PER_SEC: u64 = 1_000;
|
2017-09-08 21:02:56 +00:00
|
|
|
const MICROS_PER_SEC: u64 = 1_000_000;
|
2015-04-28 18:40:04 +00:00
|
|
|
|
2022-09-14 10:43:02 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[rustc_layout_scalar_valid_range_start(0)]
|
|
|
|
#[rustc_layout_scalar_valid_range_end(999_999_999)]
|
|
|
|
struct Nanoseconds(u32);
|
|
|
|
|
|
|
|
impl Default for Nanoseconds {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
// SAFETY: 0 is within the valid range
|
|
|
|
unsafe { Nanoseconds(0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 13:03:36 +00:00
|
|
|
/// A `Duration` type to represent a span of time, typically used for system
|
2015-04-28 18:40:04 +00:00
|
|
|
/// timeouts.
|
|
|
|
///
|
2017-07-17 18:08:55 +00:00
|
|
|
/// Each `Duration` is composed of a whole number of seconds and a fractional part
|
2019-02-09 21:23:30 +00:00
|
|
|
/// represented in nanoseconds. If the underlying system does not support
|
2017-07-17 18:08:55 +00:00
|
|
|
/// nanosecond-level precision, APIs binding a system timeout will typically round up
|
|
|
|
/// the number of nanoseconds.
|
2015-04-28 18:40:04 +00:00
|
|
|
///
|
2020-08-09 21:26:42 +00:00
|
|
|
/// [`Duration`]s implement many common traits, including [`Add`], [`Sub`], and other
|
|
|
|
/// [`ops`] traits. It implements [`Default`] by returning a zero-length `Duration`.
|
2016-12-13 13:03:36 +00:00
|
|
|
///
|
2020-08-09 19:20:57 +00:00
|
|
|
/// [`ops`]: crate::ops
|
2015-04-28 18:40:04 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let five_seconds = Duration::new(5, 0);
|
|
|
|
/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
|
|
|
|
///
|
2015-07-06 06:20:00 +00:00
|
|
|
/// assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
|
|
|
|
/// assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
|
2015-04-28 18:40:04 +00:00
|
|
|
///
|
|
|
|
/// let ten_millis = Duration::from_millis(10);
|
|
|
|
/// ```
|
2021-01-28 08:51:31 +00:00
|
|
|
///
|
|
|
|
/// # Formatting `Duration` values
|
|
|
|
///
|
|
|
|
/// `Duration` intentionally does not have a `Display` impl, as there are a
|
|
|
|
/// variety of ways to format spans of time for human readability. `Duration`
|
|
|
|
/// provides a `Debug` impl that shows the full precision of the value.
|
|
|
|
///
|
|
|
|
/// The `Debug` output uses the non-ASCII "µs" suffix for microseconds. If your
|
|
|
|
/// program output may appear in contexts that cannot rely on full Unicode
|
|
|
|
/// compatibility, you may wish to format `Duration` objects yourself or use a
|
|
|
|
/// crate to do so.
|
2018-02-10 21:20:42 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2018-05-06 11:46:20 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
2021-07-15 21:51:34 +00:00
|
|
|
#[cfg_attr(not(test), rustc_diagnostic_item = "Duration")]
|
2014-07-15 02:18:07 +00:00
|
|
|
pub struct Duration {
|
2015-04-28 18:40:04 +00:00
|
|
|
secs: u64,
|
2022-09-14 10:43:02 +00:00
|
|
|
nanos: Nanoseconds, // Always 0 <= nanos < NANOS_PER_SEC
|
2014-07-15 02:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Duration {
|
2019-02-20 13:21:15 +00:00
|
|
|
/// The duration of one second.
|
2019-02-20 20:58:20 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-02-21 08:13:50 +00:00
|
|
|
/// #![feature(duration_constants)]
|
2019-02-20 20:58:20 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::SECOND, Duration::from_secs(1));
|
|
|
|
/// ```
|
2019-02-20 13:21:15 +00:00
|
|
|
#[unstable(feature = "duration_constants", issue = "57391")]
|
|
|
|
pub const SECOND: Duration = Duration::from_secs(1);
|
|
|
|
|
|
|
|
/// The duration of one millisecond.
|
2019-02-20 20:58:20 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-02-21 08:13:50 +00:00
|
|
|
/// #![feature(duration_constants)]
|
2019-02-20 20:58:20 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::MILLISECOND, Duration::from_millis(1));
|
|
|
|
/// ```
|
2019-02-20 13:21:15 +00:00
|
|
|
#[unstable(feature = "duration_constants", issue = "57391")]
|
|
|
|
pub const MILLISECOND: Duration = Duration::from_millis(1);
|
|
|
|
|
|
|
|
/// The duration of one microsecond.
|
2019-02-20 20:58:20 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-02-21 08:13:50 +00:00
|
|
|
/// #![feature(duration_constants)]
|
2019-02-20 20:58:20 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::MICROSECOND, Duration::from_micros(1));
|
|
|
|
/// ```
|
2019-02-20 13:21:15 +00:00
|
|
|
#[unstable(feature = "duration_constants", issue = "57391")]
|
|
|
|
pub const MICROSECOND: Duration = Duration::from_micros(1);
|
|
|
|
|
|
|
|
/// The duration of one nanosecond.
|
2019-02-20 20:58:20 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-02-21 08:13:50 +00:00
|
|
|
/// #![feature(duration_constants)]
|
2019-02-20 20:58:20 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::NANOSECOND, Duration::from_nanos(1));
|
|
|
|
/// ```
|
2019-02-20 13:21:15 +00:00
|
|
|
#[unstable(feature = "duration_constants", issue = "57391")]
|
|
|
|
pub const NANOSECOND: Duration = Duration::from_nanos(1);
|
|
|
|
|
2020-10-22 01:18:18 +00:00
|
|
|
/// A duration of zero time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::ZERO;
|
|
|
|
/// assert!(duration.is_zero());
|
|
|
|
/// assert_eq!(duration.as_nanos(), 0);
|
|
|
|
/// ```
|
2021-04-11 09:24:17 +00:00
|
|
|
#[stable(feature = "duration_zero", since = "1.53.0")]
|
2020-10-22 01:18:18 +00:00
|
|
|
pub const ZERO: Duration = Duration::from_nanos(0);
|
|
|
|
|
2020-08-30 17:31:34 +00:00
|
|
|
/// The maximum duration.
|
|
|
|
///
|
2021-04-25 17:28:23 +00:00
|
|
|
/// May vary by platform as necessary. Must be able to contain the difference between
|
|
|
|
/// two instances of [`Instant`] or two instances of [`SystemTime`].
|
|
|
|
/// This constraint gives it a value of about 584,942,417,355 years in practice,
|
|
|
|
/// which is currently used on all platforms.
|
2020-08-30 17:31:34 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::MAX, Duration::new(u64::MAX, 1_000_000_000 - 1));
|
|
|
|
/// ```
|
2021-04-25 17:28:23 +00:00
|
|
|
/// [`Instant`]: ../../std/time/struct.Instant.html
|
|
|
|
/// [`SystemTime`]: ../../std/time/struct.SystemTime.html
|
2021-04-12 04:37:46 +00:00
|
|
|
#[stable(feature = "duration_saturating_ops", since = "1.53.0")]
|
2020-08-30 17:31:34 +00:00
|
|
|
pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC - 1);
|
|
|
|
|
2017-07-17 18:08:55 +00:00
|
|
|
/// Creates a new `Duration` from the specified number of whole seconds and
|
|
|
|
/// additional nanoseconds.
|
2015-04-28 18:40:04 +00:00
|
|
|
///
|
2017-07-17 18:08:55 +00:00
|
|
|
/// If the number of nanoseconds is greater than 1 billion (the number of
|
|
|
|
/// nanoseconds in a second), then it will carry over into the seconds provided.
|
2016-04-18 10:31:16 +00:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This constructor will panic if the carry from the nanoseconds overflows
|
|
|
|
/// the seconds counter.
|
2016-12-13 13:03:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let five_seconds = Duration::new(5, 0);
|
|
|
|
/// ```
|
2015-07-06 06:20:00 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2016-04-03 03:41:37 +00:00
|
|
|
#[inline]
|
2021-10-10 06:44:26 +00:00
|
|
|
#[must_use]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn new(secs: u64, nanos: u32) -> Duration {
|
2024-01-24 11:10:07 +00:00
|
|
|
if nanos < NANOS_PER_SEC {
|
|
|
|
// SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
|
|
|
|
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
|
|
|
|
} else {
|
|
|
|
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
|
|
|
|
Some(secs) => secs,
|
|
|
|
None => panic!("overflow in Duration::new"),
|
|
|
|
};
|
|
|
|
let nanos = nanos % NANOS_PER_SEC;
|
|
|
|
// SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
|
|
|
|
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
|
|
|
|
}
|
2014-07-15 02:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:08:55 +00:00
|
|
|
/// Creates a new `Duration` from the specified number of whole seconds.
|
2016-12-13 13:03:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2017-05-03 03:55:40 +00:00
|
|
|
/// let duration = Duration::from_secs(5);
|
|
|
|
///
|
|
|
|
/// assert_eq!(5, duration.as_secs());
|
|
|
|
/// assert_eq!(0, duration.subsec_nanos());
|
2016-12-13 13:03:36 +00:00
|
|
|
/// ```
|
2015-07-06 06:20:00 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2021-10-10 22:22:40 +00:00
|
|
|
#[must_use]
|
2016-04-03 03:41:37 +00:00
|
|
|
#[inline]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2018-01-09 20:21:45 +00:00
|
|
|
pub const fn from_secs(secs: u64) -> Duration {
|
2022-09-14 10:43:02 +00:00
|
|
|
Duration::new(secs, 0)
|
2014-07-15 02:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 18:40:04 +00:00
|
|
|
/// Creates a new `Duration` from the specified number of milliseconds.
|
2016-12-13 13:03:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2017-05-03 03:56:29 +00:00
|
|
|
/// let duration = Duration::from_millis(2569);
|
|
|
|
///
|
|
|
|
/// assert_eq!(2, duration.as_secs());
|
2017-12-05 01:01:31 +00:00
|
|
|
/// assert_eq!(569_000_000, duration.subsec_nanos());
|
2016-12-13 13:03:36 +00:00
|
|
|
/// ```
|
2015-07-06 06:20:00 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2021-10-10 22:22:40 +00:00
|
|
|
#[must_use]
|
2016-04-03 03:41:37 +00:00
|
|
|
#[inline]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2018-01-09 20:21:45 +00:00
|
|
|
pub const fn from_millis(millis: u64) -> Duration {
|
2022-09-14 10:43:02 +00:00
|
|
|
Duration::new(millis / MILLIS_PER_SEC, ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI)
|
2017-09-09 22:27:39 +00:00
|
|
|
}
|
2017-09-08 21:02:56 +00:00
|
|
|
|
|
|
|
/// Creates a new `Duration` from the specified number of microseconds.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::from_micros(1_000_002);
|
|
|
|
///
|
|
|
|
/// assert_eq!(1, duration.as_secs());
|
|
|
|
/// assert_eq!(2000, duration.subsec_nanos());
|
|
|
|
/// ```
|
2018-04-17 04:43:48 +00:00
|
|
|
#[stable(feature = "duration_from_micros", since = "1.27.0")]
|
2021-10-10 22:22:40 +00:00
|
|
|
#[must_use]
|
2017-09-08 21:02:56 +00:00
|
|
|
#[inline]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2018-01-09 20:21:45 +00:00
|
|
|
pub const fn from_micros(micros: u64) -> Duration {
|
2022-09-14 10:43:02 +00:00
|
|
|
Duration::new(micros / MICROS_PER_SEC, ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO)
|
2014-07-15 02:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 01:01:31 +00:00
|
|
|
/// Creates a new `Duration` from the specified number of nanoseconds.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::from_nanos(1_000_000_123);
|
|
|
|
///
|
|
|
|
/// assert_eq!(1, duration.as_secs());
|
|
|
|
/// assert_eq!(123, duration.subsec_nanos());
|
|
|
|
/// ```
|
2018-04-17 04:56:20 +00:00
|
|
|
#[stable(feature = "duration_extras", since = "1.27.0")]
|
2021-10-10 22:22:40 +00:00
|
|
|
#[must_use]
|
2017-12-05 01:01:31 +00:00
|
|
|
#[inline]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2018-01-09 20:21:45 +00:00
|
|
|
pub const fn from_nanos(nanos: u64) -> Duration {
|
2022-09-14 10:43:02 +00:00
|
|
|
Duration::new(nanos / (NANOS_PER_SEC as u64), (nanos % (NANOS_PER_SEC as u64)) as u32)
|
2017-12-05 01:01:31 +00:00
|
|
|
}
|
|
|
|
|
core/time: Add Duration methods for zero
This patch adds two methods to `Duration`. The first, `Duration::zero`,
provides a `const` constructor for getting an zero-length duration. This
is also what `Default` provides (this was clarified in the docs), though
`default` is not `const`.
The second, `Duration::is_zero`, returns true if a `Duration` spans no
time (i.e., because its components are all zero). Previously, the way to
do this was either to compare both `as_secs` and `subsec_nanos` to 0, to
compare against `Duration::new(0, 0)`, or to use the `u128` method
`as_nanos`, none of which were particularly elegant.
2020-05-30 18:55:38 +00:00
|
|
|
/// Returns true if this `Duration` spans no time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2020-10-22 01:18:18 +00:00
|
|
|
/// assert!(Duration::ZERO.is_zero());
|
core/time: Add Duration methods for zero
This patch adds two methods to `Duration`. The first, `Duration::zero`,
provides a `const` constructor for getting an zero-length duration. This
is also what `Default` provides (this was clarified in the docs), though
`default` is not `const`.
The second, `Duration::is_zero`, returns true if a `Duration` spans no
time (i.e., because its components are all zero). Previously, the way to
do this was either to compare both `as_secs` and `subsec_nanos` to 0, to
compare against `Duration::new(0, 0)`, or to use the `u128` method
`as_nanos`, none of which were particularly elegant.
2020-05-30 18:55:38 +00:00
|
|
|
/// assert!(Duration::new(0, 0).is_zero());
|
|
|
|
/// assert!(Duration::from_nanos(0).is_zero());
|
|
|
|
/// assert!(Duration::from_secs(0).is_zero());
|
|
|
|
///
|
|
|
|
/// assert!(!Duration::new(1, 1).is_zero());
|
|
|
|
/// assert!(!Duration::from_nanos(1).is_zero());
|
|
|
|
/// assert!(!Duration::from_secs(1).is_zero());
|
|
|
|
/// ```
|
2021-10-10 01:27:13 +00:00
|
|
|
#[must_use]
|
2021-04-11 09:24:17 +00:00
|
|
|
#[stable(feature = "duration_zero", since = "1.53.0")]
|
2021-04-11 09:36:35 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_zero", since = "1.53.0")]
|
core/time: Add Duration methods for zero
This patch adds two methods to `Duration`. The first, `Duration::zero`,
provides a `const` constructor for getting an zero-length duration. This
is also what `Default` provides (this was clarified in the docs), though
`default` is not `const`.
The second, `Duration::is_zero`, returns true if a `Duration` spans no
time (i.e., because its components are all zero). Previously, the way to
do this was either to compare both `as_secs` and `subsec_nanos` to 0, to
compare against `Duration::new(0, 0)`, or to use the `u128` method
`as_nanos`, none of which were particularly elegant.
2020-05-30 18:55:38 +00:00
|
|
|
#[inline]
|
|
|
|
pub const fn is_zero(&self) -> bool {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.secs == 0 && self.nanos.0 == 0
|
core/time: Add Duration methods for zero
This patch adds two methods to `Duration`. The first, `Duration::zero`,
provides a `const` constructor for getting an zero-length duration. This
is also what `Default` provides (this was clarified in the docs), though
`default` is not `const`.
The second, `Duration::is_zero`, returns true if a `Duration` spans no
time (i.e., because its components are all zero). Previously, the way to
do this was either to compare both `as_secs` and `subsec_nanos` to 0, to
compare against `Duration::new(0, 0)`, or to use the `u128` method
`as_nanos`, none of which were particularly elegant.
2020-05-30 18:55:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:08:55 +00:00
|
|
|
/// Returns the number of _whole_ seconds contained by this `Duration`.
|
2015-04-28 18:40:04 +00:00
|
|
|
///
|
2017-07-17 18:08:55 +00:00
|
|
|
/// The returned value does not include the fractional (nanosecond) part of the
|
|
|
|
/// duration, which can be obtained using [`subsec_nanos`].
|
2016-12-13 13:03:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2017-05-03 03:58:39 +00:00
|
|
|
/// let duration = Duration::new(5, 730023852);
|
|
|
|
/// assert_eq!(duration.as_secs(), 5);
|
2016-12-13 13:03:36 +00:00
|
|
|
/// ```
|
2017-05-03 04:04:59 +00:00
|
|
|
///
|
2022-08-09 00:51:17 +00:00
|
|
|
/// To determine the total number of seconds represented by the `Duration`
|
|
|
|
/// including the fractional part, use [`as_secs_f64`] or [`as_secs_f32`]
|
2017-05-03 04:04:59 +00:00
|
|
|
///
|
2022-08-10 04:15:06 +00:00
|
|
|
/// [`as_secs_f64`]: Duration::as_secs_f64
|
|
|
|
/// [`as_secs_f32`]: Duration::as_secs_f32
|
2020-08-09 19:20:57 +00:00
|
|
|
/// [`subsec_nanos`]: Duration::subsec_nanos
|
2015-07-06 06:20:00 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2022-03-27 02:43:11 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2021-10-11 17:57:38 +00:00
|
|
|
#[must_use]
|
2016-04-03 03:41:37 +00:00
|
|
|
#[inline]
|
2018-05-08 20:47:50 +00:00
|
|
|
pub const fn as_secs(&self) -> u64 {
|
|
|
|
self.secs
|
|
|
|
}
|
2015-01-03 03:56:24 +00:00
|
|
|
|
2018-07-14 13:48:52 +00:00
|
|
|
/// Returns the fractional part of this `Duration`, in whole milliseconds.
|
2017-12-05 01:01:31 +00:00
|
|
|
///
|
|
|
|
/// This method does **not** return the length of the duration when
|
|
|
|
/// represented by milliseconds. The returned number always represents a
|
2018-11-27 02:59:49 +00:00
|
|
|
/// fractional portion of a second (i.e., it is less than one thousand).
|
2017-12-05 01:01:31 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::from_millis(5432);
|
|
|
|
/// assert_eq!(duration.as_secs(), 5);
|
2018-01-12 05:38:24 +00:00
|
|
|
/// assert_eq!(duration.subsec_millis(), 432);
|
2017-12-05 01:01:31 +00:00
|
|
|
/// ```
|
2018-04-17 04:56:20 +00:00
|
|
|
#[stable(feature = "duration_extras", since = "1.27.0")]
|
2022-03-27 02:43:11 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2021-10-14 22:54:55 +00:00
|
|
|
#[must_use]
|
2017-12-05 01:01:31 +00:00
|
|
|
#[inline]
|
2018-05-08 20:47:50 +00:00
|
|
|
pub const fn subsec_millis(&self) -> u32 {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.nanos.0 / NANOS_PER_MILLI
|
2018-05-08 20:47:50 +00:00
|
|
|
}
|
2017-12-05 01:01:31 +00:00
|
|
|
|
2018-07-14 13:48:52 +00:00
|
|
|
/// Returns the fractional part of this `Duration`, in whole microseconds.
|
2017-12-05 01:01:31 +00:00
|
|
|
///
|
|
|
|
/// This method does **not** return the length of the duration when
|
|
|
|
/// represented by microseconds. The returned number always represents a
|
2018-11-27 02:59:49 +00:00
|
|
|
/// fractional portion of a second (i.e., it is less than one million).
|
2017-12-05 01:01:31 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::from_micros(1_234_567);
|
|
|
|
/// assert_eq!(duration.as_secs(), 1);
|
2018-01-12 05:38:24 +00:00
|
|
|
/// assert_eq!(duration.subsec_micros(), 234_567);
|
2017-12-05 01:01:31 +00:00
|
|
|
/// ```
|
2018-04-17 04:56:20 +00:00
|
|
|
#[stable(feature = "duration_extras", since = "1.27.0")]
|
2022-03-27 02:43:11 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2021-10-14 22:54:55 +00:00
|
|
|
#[must_use]
|
2017-12-05 01:01:31 +00:00
|
|
|
#[inline]
|
2018-05-08 20:47:50 +00:00
|
|
|
pub const fn subsec_micros(&self) -> u32 {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.nanos.0 / NANOS_PER_MICRO
|
2018-05-08 20:47:50 +00:00
|
|
|
}
|
2017-12-05 01:01:31 +00:00
|
|
|
|
2017-07-17 18:08:55 +00:00
|
|
|
/// Returns the fractional part of this `Duration`, in nanoseconds.
|
2015-04-28 18:40:04 +00:00
|
|
|
///
|
|
|
|
/// This method does **not** return the length of the duration when
|
|
|
|
/// represented by nanoseconds. The returned number always represents a
|
2018-11-27 02:59:49 +00:00
|
|
|
/// fractional portion of a second (i.e., it is less than one billion).
|
2016-12-13 13:03:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::from_millis(5010);
|
2017-07-17 18:08:55 +00:00
|
|
|
/// assert_eq!(duration.as_secs(), 5);
|
|
|
|
/// assert_eq!(duration.subsec_nanos(), 10_000_000);
|
2016-12-13 13:03:36 +00:00
|
|
|
/// ```
|
2015-07-06 06:20:00 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2022-03-27 02:43:11 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
2021-10-14 22:54:55 +00:00
|
|
|
#[must_use]
|
2016-04-03 03:41:37 +00:00
|
|
|
#[inline]
|
2018-05-08 20:47:50 +00:00
|
|
|
pub const fn subsec_nanos(&self) -> u32 {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.nanos.0
|
2018-05-08 20:47:50 +00:00
|
|
|
}
|
2016-09-14 00:21:54 +00:00
|
|
|
|
2018-07-14 13:48:52 +00:00
|
|
|
/// Returns the total number of whole milliseconds contained by this `Duration`.
|
2018-05-19 19:18:07 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::new(5, 730023852);
|
|
|
|
/// assert_eq!(duration.as_millis(), 5730);
|
|
|
|
/// ```
|
2018-12-25 21:23:48 +00:00
|
|
|
#[stable(feature = "duration_as_u128", since = "1.33.0")]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
|
2021-10-11 17:57:38 +00:00
|
|
|
#[must_use]
|
2018-05-19 19:18:07 +00:00
|
|
|
#[inline]
|
2018-10-22 18:25:31 +00:00
|
|
|
pub const fn as_millis(&self) -> u128 {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.secs as u128 * MILLIS_PER_SEC as u128 + (self.nanos.0 / NANOS_PER_MILLI) as u128
|
2018-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 13:48:52 +00:00
|
|
|
/// Returns the total number of whole microseconds contained by this `Duration`.
|
2018-05-19 19:18:07 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::new(5, 730023852);
|
|
|
|
/// assert_eq!(duration.as_micros(), 5730023);
|
|
|
|
/// ```
|
2018-12-25 21:23:48 +00:00
|
|
|
#[stable(feature = "duration_as_u128", since = "1.33.0")]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
|
2021-10-11 17:57:38 +00:00
|
|
|
#[must_use]
|
2018-05-19 19:18:07 +00:00
|
|
|
#[inline]
|
2018-10-22 18:25:31 +00:00
|
|
|
pub const fn as_micros(&self) -> u128 {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.secs as u128 * MICROS_PER_SEC as u128 + (self.nanos.0 / NANOS_PER_MICRO) as u128
|
2018-05-19 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 19:39:28 +00:00
|
|
|
/// Returns the total number of nanoseconds contained by this `Duration`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let duration = Duration::new(5, 730023852);
|
|
|
|
/// assert_eq!(duration.as_nanos(), 5730023852);
|
|
|
|
/// ```
|
2018-12-25 21:23:48 +00:00
|
|
|
#[stable(feature = "duration_as_u128", since = "1.33.0")]
|
2019-12-18 17:00:59 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
|
2021-10-11 17:57:38 +00:00
|
|
|
#[must_use]
|
2018-04-22 19:39:28 +00:00
|
|
|
#[inline]
|
2018-10-22 18:25:31 +00:00
|
|
|
pub const fn as_nanos(&self) -> u128 {
|
2022-09-14 10:43:02 +00:00
|
|
|
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos.0 as u128
|
2018-04-22 19:39:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 18:33:44 +00:00
|
|
|
/// Computes the absolute difference between `self` and `other`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(duration_abs_diff)]
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::new(100, 0).abs_diff(Duration::new(80, 0)), Duration::new(20, 0));
|
|
|
|
/// assert_eq!(Duration::new(100, 400_000_000).abs_diff(Duration::new(110, 0)), Duration::new(9, 600_000_000));
|
|
|
|
/// ```
|
|
|
|
#[unstable(feature = "duration_abs_diff", issue = "117618")]
|
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
|
|
|
#[inline]
|
|
|
|
pub const fn abs_diff(self, other: Duration) -> Duration {
|
|
|
|
if let Some(res) = self.checked_sub(other) { res } else { other.checked_sub(self).unwrap() }
|
|
|
|
}
|
|
|
|
|
2016-12-13 13:03:36 +00:00
|
|
|
/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
|
2016-09-14 00:21:54 +00:00
|
|
|
/// if overflow occurred.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
2016-09-14 22:41:19 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2016-09-14 00:21:54 +00:00
|
|
|
/// assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
|
2020-03-27 21:43:28 +00:00
|
|
|
/// assert_eq!(Duration::new(1, 0).checked_add(Duration::new(u64::MAX, 0)), None);
|
2016-09-14 00:21:54 +00:00
|
|
|
/// ```
|
2017-01-25 23:37:20 +00:00
|
|
|
#[stable(feature = "duration_checked_ops", since = "1.16.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2016-09-14 00:21:54 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn checked_add(self, rhs: Duration) -> Option<Duration> {
|
2016-09-14 00:21:54 +00:00
|
|
|
if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
|
2022-09-14 10:43:02 +00:00
|
|
|
let mut nanos = self.nanos.0 + rhs.nanos.0;
|
2016-09-14 00:21:54 +00:00
|
|
|
if nanos >= NANOS_PER_SEC {
|
|
|
|
nanos -= NANOS_PER_SEC;
|
|
|
|
if let Some(new_secs) = secs.checked_add(1) {
|
|
|
|
secs = new_secs;
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_assert!(nanos < NANOS_PER_SEC);
|
2022-09-14 10:43:02 +00:00
|
|
|
Some(Duration::new(secs, nanos))
|
2016-09-14 00:21:54 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 17:31:34 +00:00
|
|
|
/// Saturating `Duration` addition. Computes `self + other`, returning [`Duration::MAX`]
|
|
|
|
/// if overflow occurred.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(duration_constants)]
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::new(0, 0).saturating_add(Duration::new(0, 1)), Duration::new(0, 1));
|
|
|
|
/// assert_eq!(Duration::new(1, 0).saturating_add(Duration::new(u64::MAX, 0)), Duration::MAX);
|
|
|
|
/// ```
|
2021-04-11 10:11:17 +00:00
|
|
|
#[stable(feature = "duration_saturating_ops", since = "1.53.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2020-08-30 17:31:34 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-08-30 17:31:34 +00:00
|
|
|
pub const fn saturating_add(self, rhs: Duration) -> Duration {
|
|
|
|
match self.checked_add(rhs) {
|
|
|
|
Some(res) => res,
|
|
|
|
None => Duration::MAX,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 13:03:36 +00:00
|
|
|
/// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
|
2017-12-21 01:39:01 +00:00
|
|
|
/// if the result would be negative or if overflow occurred.
|
2016-09-14 00:21:54 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
2016-09-14 22:41:19 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2016-09-14 00:21:54 +00:00
|
|
|
/// assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
|
|
|
|
/// assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);
|
|
|
|
/// ```
|
2017-01-25 23:37:20 +00:00
|
|
|
#[stable(feature = "duration_checked_ops", since = "1.16.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2016-09-14 00:21:54 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn checked_sub(self, rhs: Duration) -> Option<Duration> {
|
2016-09-14 00:21:54 +00:00
|
|
|
if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
|
2022-09-14 10:43:02 +00:00
|
|
|
let nanos = if self.nanos.0 >= rhs.nanos.0 {
|
|
|
|
self.nanos.0 - rhs.nanos.0
|
2021-05-03 00:06:17 +00:00
|
|
|
} else if let Some(sub_secs) = secs.checked_sub(1) {
|
|
|
|
secs = sub_secs;
|
2022-09-14 10:43:02 +00:00
|
|
|
self.nanos.0 + NANOS_PER_SEC - rhs.nanos.0
|
2016-09-14 00:21:54 +00:00
|
|
|
} else {
|
2021-05-03 00:06:17 +00:00
|
|
|
return None;
|
2016-09-14 00:21:54 +00:00
|
|
|
};
|
|
|
|
debug_assert!(nanos < NANOS_PER_SEC);
|
2022-09-14 10:43:02 +00:00
|
|
|
Some(Duration::new(secs, nanos))
|
2016-09-14 00:21:54 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 21:06:55 +00:00
|
|
|
/// Saturating `Duration` subtraction. Computes `self - other`, returning [`Duration::ZERO`]
|
2020-08-30 17:31:34 +00:00
|
|
|
/// if the result would be negative or if overflow occurred.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::new(0, 1).saturating_sub(Duration::new(0, 0)), Duration::new(0, 1));
|
2020-10-27 21:06:55 +00:00
|
|
|
/// assert_eq!(Duration::new(0, 0).saturating_sub(Duration::new(0, 1)), Duration::ZERO);
|
2020-08-30 17:31:34 +00:00
|
|
|
/// ```
|
2021-04-11 10:11:17 +00:00
|
|
|
#[stable(feature = "duration_saturating_ops", since = "1.53.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2020-08-30 17:31:34 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-08-30 17:31:34 +00:00
|
|
|
pub const fn saturating_sub(self, rhs: Duration) -> Duration {
|
|
|
|
match self.checked_sub(rhs) {
|
|
|
|
Some(res) => res,
|
2020-10-27 21:06:55 +00:00
|
|
|
None => Duration::ZERO,
|
2020-08-30 17:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 13:03:36 +00:00
|
|
|
/// Checked `Duration` multiplication. Computes `self * other`, returning
|
|
|
|
/// [`None`] if overflow occurred.
|
|
|
|
///
|
2016-09-14 00:21:54 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
2016-09-14 22:41:19 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2016-09-14 00:21:54 +00:00
|
|
|
/// assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
|
2020-03-27 21:43:28 +00:00
|
|
|
/// assert_eq!(Duration::new(u64::MAX - 1, 0).checked_mul(2), None);
|
2016-09-14 00:21:54 +00:00
|
|
|
/// ```
|
2017-01-25 23:37:20 +00:00
|
|
|
#[stable(feature = "duration_checked_ops", since = "1.16.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2016-09-14 00:21:54 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn checked_mul(self, rhs: u32) -> Option<Duration> {
|
2016-09-14 00:21:54 +00:00
|
|
|
// Multiply nanoseconds as u64, because it cannot overflow that way.
|
2022-09-14 10:43:02 +00:00
|
|
|
let total_nanos = self.nanos.0 as u64 * rhs as u64;
|
2016-09-14 00:21:54 +00:00
|
|
|
let extra_secs = total_nanos / (NANOS_PER_SEC as u64);
|
|
|
|
let nanos = (total_nanos % (NANOS_PER_SEC as u64)) as u32;
|
2020-05-21 19:22:47 +00:00
|
|
|
if let Some(s) = self.secs.checked_mul(rhs as u64) {
|
|
|
|
if let Some(secs) = s.checked_add(extra_secs) {
|
|
|
|
debug_assert!(nanos < NANOS_PER_SEC);
|
2022-09-14 10:43:02 +00:00
|
|
|
return Some(Duration::new(secs, nanos));
|
2020-05-21 19:22:47 +00:00
|
|
|
}
|
2016-09-14 00:21:54 +00:00
|
|
|
}
|
2020-05-21 19:22:47 +00:00
|
|
|
None
|
2016-09-14 00:21:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 17:31:34 +00:00
|
|
|
/// Saturating `Duration` multiplication. Computes `self * other`, returning
|
|
|
|
/// [`Duration::MAX`] if overflow occurred.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(duration_constants)]
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// assert_eq!(Duration::new(0, 500_000_001).saturating_mul(2), Duration::new(1, 2));
|
|
|
|
/// assert_eq!(Duration::new(u64::MAX - 1, 0).saturating_mul(2), Duration::MAX);
|
|
|
|
/// ```
|
2021-04-11 10:11:17 +00:00
|
|
|
#[stable(feature = "duration_saturating_ops", since = "1.53.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2020-08-30 17:31:34 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-08-30 17:31:34 +00:00
|
|
|
pub const fn saturating_mul(self, rhs: u32) -> Duration {
|
|
|
|
match self.checked_mul(rhs) {
|
|
|
|
Some(res) => res,
|
|
|
|
None => Duration::MAX,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 13:03:36 +00:00
|
|
|
/// Checked `Duration` division. Computes `self / other`, returning [`None`]
|
|
|
|
/// if `other == 0`.
|
|
|
|
///
|
2016-09-14 00:21:54 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
2016-09-14 22:41:19 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2016-09-14 00:21:54 +00:00
|
|
|
/// assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
|
|
|
|
/// assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
|
|
|
|
/// assert_eq!(Duration::new(2, 0).checked_div(0), None);
|
|
|
|
/// ```
|
2017-01-25 23:37:20 +00:00
|
|
|
#[stable(feature = "duration_checked_ops", since = "1.16.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2016-09-14 00:21:54 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn checked_div(self, rhs: u32) -> Option<Duration> {
|
2016-09-14 00:21:54 +00:00
|
|
|
if rhs != 0 {
|
2023-07-30 08:00:02 +00:00
|
|
|
let (secs, extra_secs) = (self.secs / (rhs as u64), self.secs % (rhs as u64));
|
|
|
|
let (mut nanos, extra_nanos) = (self.nanos.0 / rhs, self.nanos.0 % rhs);
|
|
|
|
nanos +=
|
|
|
|
((extra_secs * (NANOS_PER_SEC as u64) + extra_nanos as u64) / (rhs as u64)) as u32;
|
2016-09-14 00:21:54 +00:00
|
|
|
debug_assert!(nanos < NANOS_PER_SEC);
|
2022-09-14 10:43:02 +00:00
|
|
|
Some(Duration::new(secs, nanos))
|
2016-09-14 00:21:54 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 00:52:59 +00:00
|
|
|
|
2018-09-13 00:43:53 +00:00
|
|
|
/// Returns the number of seconds contained by this `Duration` as `f64`.
|
|
|
|
///
|
|
|
|
/// The returned value does include the fractional (nanosecond) part of the duration.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let dur = Duration::new(2, 700_000_000);
|
2019-03-11 14:53:22 +00:00
|
|
|
/// assert_eq!(dur.as_secs_f64(), 2.7);
|
2018-09-13 00:43:53 +00:00
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-11 17:57:38 +00:00
|
|
|
#[must_use]
|
2018-09-13 00:43:53 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn as_secs_f64(&self) -> f64 {
|
2022-09-14 10:43:02 +00:00
|
|
|
(self.secs as f64) + (self.nanos.0 as f64) / (NANOS_PER_SEC as f64)
|
2018-09-13 00:43:53 +00:00
|
|
|
}
|
2018-09-12 08:50:46 +00:00
|
|
|
|
2019-03-11 14:53:22 +00:00
|
|
|
/// Returns the number of seconds contained by this `Duration` as `f32`.
|
|
|
|
///
|
|
|
|
/// The returned value does include the fractional (nanosecond) part of the duration.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let dur = Duration::new(2, 700_000_000);
|
|
|
|
/// assert_eq!(dur.as_secs_f32(), 2.7);
|
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-11 17:57:38 +00:00
|
|
|
#[must_use]
|
2019-03-11 14:53:22 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn as_secs_f32(&self) -> f32 {
|
2022-09-14 10:43:02 +00:00
|
|
|
(self.secs as f32) + (self.nanos.0 as f32) / (NANOS_PER_SEC as f32)
|
2019-03-11 14:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `Duration` from the specified number of seconds represented
|
|
|
|
/// as `f64`.
|
2018-09-13 00:43:53 +00:00
|
|
|
///
|
2018-09-13 01:47:08 +00:00
|
|
|
/// # Panics
|
2022-01-26 15:14:25 +00:00
|
|
|
/// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
|
2018-09-13 01:47:08 +00:00
|
|
|
///
|
2018-09-13 00:43:53 +00:00
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::from_secs_f64(0.0);
|
|
|
|
/// assert_eq!(res, Duration::new(0, 0));
|
|
|
|
/// let res = Duration::from_secs_f64(1e-20);
|
|
|
|
/// assert_eq!(res, Duration::new(0, 0));
|
|
|
|
/// let res = Duration::from_secs_f64(4.2e-7);
|
|
|
|
/// assert_eq!(res, Duration::new(0, 420));
|
|
|
|
/// let res = Duration::from_secs_f64(2.7);
|
|
|
|
/// assert_eq!(res, Duration::new(2, 700_000_000));
|
|
|
|
/// let res = Duration::from_secs_f64(3e10);
|
|
|
|
/// assert_eq!(res, Duration::new(30_000_000_000, 0));
|
|
|
|
/// // subnormal float
|
|
|
|
/// let res = Duration::from_secs_f64(f64::from_bits(1));
|
|
|
|
/// assert_eq!(res, Duration::new(0, 0));
|
2022-04-14 17:44:09 +00:00
|
|
|
/// // conversion uses rounding
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::from_secs_f64(0.999e-9);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(res, Duration::new(0, 1));
|
2018-09-13 00:43:53 +00:00
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-10 22:22:40 +00:00
|
|
|
#[must_use]
|
2018-09-13 00:43:53 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn from_secs_f64(secs: f64) -> Duration {
|
2021-06-14 12:17:53 +00:00
|
|
|
match Duration::try_from_secs_f64(secs) {
|
|
|
|
Ok(v) => v,
|
2021-11-08 05:55:00 +00:00
|
|
|
Err(e) => panic!("{}", e.description()),
|
2018-09-13 00:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 00:52:59 +00:00
|
|
|
|
2019-03-11 14:53:22 +00:00
|
|
|
/// Creates a new `Duration` from the specified number of seconds represented
|
|
|
|
/// as `f32`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
2022-01-26 15:14:25 +00:00
|
|
|
/// This constructor will panic if `secs` is negative, overflows `Duration` or not finite.
|
2019-03-11 14:53:22 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::from_secs_f32(0.0);
|
|
|
|
/// assert_eq!(res, Duration::new(0, 0));
|
|
|
|
/// let res = Duration::from_secs_f32(1e-20);
|
|
|
|
/// assert_eq!(res, Duration::new(0, 0));
|
|
|
|
/// let res = Duration::from_secs_f32(4.2e-7);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(res, Duration::new(0, 420));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::from_secs_f32(2.7);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(res, Duration::new(2, 700_000_048));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::from_secs_f32(3e10);
|
|
|
|
/// assert_eq!(res, Duration::new(30_000_001_024, 0));
|
|
|
|
/// // subnormal float
|
|
|
|
/// let res = Duration::from_secs_f32(f32::from_bits(1));
|
|
|
|
/// assert_eq!(res, Duration::new(0, 0));
|
2022-04-14 17:44:09 +00:00
|
|
|
/// // conversion uses rounding
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::from_secs_f32(0.999e-9);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(res, Duration::new(0, 1));
|
2019-03-11 14:53:22 +00:00
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-10 22:22:40 +00:00
|
|
|
#[must_use]
|
2019-03-11 14:53:22 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn from_secs_f32(secs: f32) -> Duration {
|
2021-06-14 12:17:53 +00:00
|
|
|
match Duration::try_from_secs_f32(secs) {
|
|
|
|
Ok(v) => v,
|
2021-11-08 05:55:00 +00:00
|
|
|
Err(e) => panic!("{}", e.description()),
|
2019-03-11 14:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
/// Multiplies `Duration` by `f64`.
|
2018-09-12 08:50:46 +00:00
|
|
|
///
|
2018-09-13 01:47:08 +00:00
|
|
|
/// # Panics
|
2022-01-26 15:14:25 +00:00
|
|
|
/// This method will panic if result is negative, overflows `Duration` or not finite.
|
2018-09-13 01:47:08 +00:00
|
|
|
///
|
2018-09-12 08:50:46 +00:00
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2018-09-12 11:17:36 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2018-09-12 08:50:46 +00:00
|
|
|
/// let dur = Duration::new(2, 700_000_000);
|
|
|
|
/// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
|
|
|
|
/// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
|
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2018-09-12 08:50:46 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn mul_f64(self, rhs: f64) -> Duration {
|
2019-03-11 14:53:22 +00:00
|
|
|
Duration::from_secs_f64(rhs * self.as_secs_f64())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Multiplies `Duration` by `f32`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
2022-01-26 15:14:25 +00:00
|
|
|
/// This method will panic if result is negative, overflows `Duration` or not finite.
|
2019-03-11 14:53:22 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let dur = Duration::new(2, 700_000_000);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_641));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847800, 0));
|
2019-03-11 14:53:22 +00:00
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2019-03-11 14:53:22 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn mul_f32(self, rhs: f32) -> Duration {
|
2019-03-11 14:53:22 +00:00
|
|
|
Duration::from_secs_f32(rhs * self.as_secs_f32())
|
2018-09-12 08:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Divide `Duration` by `f64`.
|
|
|
|
///
|
2018-09-13 01:47:08 +00:00
|
|
|
/// # Panics
|
2022-01-26 15:14:25 +00:00
|
|
|
/// This method will panic if result is negative, overflows `Duration` or not finite.
|
2018-09-13 01:47:08 +00:00
|
|
|
///
|
2018-09-12 08:50:46 +00:00
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2018-09-12 11:17:36 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2018-09-12 08:50:46 +00:00
|
|
|
/// let dur = Duration::new(2, 700_000_000);
|
|
|
|
/// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_599));
|
2018-09-12 08:50:46 +00:00
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2018-09-12 08:50:46 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn div_f64(self, rhs: f64) -> Duration {
|
2019-03-11 14:53:22 +00:00
|
|
|
Duration::from_secs_f64(self.as_secs_f64() / rhs)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Divide `Duration` by `f32`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
2022-01-26 15:14:25 +00:00
|
|
|
/// This method will panic if result is negative, overflows `Duration` or not finite.
|
2019-03-11 14:53:22 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let dur = Duration::new(2, 700_000_000);
|
2019-03-11 16:57:53 +00:00
|
|
|
/// // note that due to rounding errors result is slightly
|
|
|
|
/// // different from 0.859_872_611
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_580));
|
|
|
|
/// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_599));
|
2019-03-11 14:53:22 +00:00
|
|
|
/// ```
|
2019-07-17 15:36:15 +00:00
|
|
|
#[stable(feature = "duration_float", since = "1.38.0")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2019-03-11 14:53:22 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn div_f32(self, rhs: f32) -> Duration {
|
2019-03-11 14:53:22 +00:00
|
|
|
Duration::from_secs_f32(self.as_secs_f32() / rhs)
|
2018-09-12 08:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Divide `Duration` by `Duration` and return `f64`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2019-07-30 16:41:03 +00:00
|
|
|
/// #![feature(div_duration)]
|
2018-09-12 11:17:36 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
2018-09-12 08:50:46 +00:00
|
|
|
/// let dur1 = Duration::new(2, 700_000_000);
|
|
|
|
/// let dur2 = Duration::new(5, 400_000_000);
|
2019-03-11 16:57:53 +00:00
|
|
|
/// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
|
2018-09-12 08:50:46 +00:00
|
|
|
/// ```
|
2019-07-30 15:00:17 +00:00
|
|
|
#[unstable(feature = "div_duration", issue = "63139")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2018-09-12 08:50:46 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn div_duration_f64(self, rhs: Duration) -> f64 {
|
2019-03-11 14:53:22 +00:00
|
|
|
self.as_secs_f64() / rhs.as_secs_f64()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Divide `Duration` by `Duration` and return `f32`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2019-07-30 16:41:03 +00:00
|
|
|
/// #![feature(div_duration)]
|
2019-03-11 14:53:22 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let dur1 = Duration::new(2, 700_000_000);
|
|
|
|
/// let dur2 = Duration::new(5, 400_000_000);
|
2019-03-11 16:57:53 +00:00
|
|
|
/// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
|
2019-03-11 14:53:22 +00:00
|
|
|
/// ```
|
2019-07-30 15:00:17 +00:00
|
|
|
#[unstable(feature = "div_duration", issue = "63139")]
|
2021-10-10 02:35:00 +00:00
|
|
|
#[must_use = "this returns the result of the operation, \
|
|
|
|
without modifying the original"]
|
2019-03-11 14:53:22 +00:00
|
|
|
#[inline]
|
2021-10-05 03:09:23 +00:00
|
|
|
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
|
2020-05-21 19:22:47 +00:00
|
|
|
pub const fn div_duration_f32(self, rhs: Duration) -> f32 {
|
2019-03-11 14:53:22 +00:00
|
|
|
self.as_secs_f32() / rhs.as_secs_f32()
|
2018-09-12 08:50:46 +00:00
|
|
|
}
|
2014-12-15 22:06:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2014-12-31 20:45:13 +00:00
|
|
|
impl Add for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2014-12-01 19:32:36 +00:00
|
|
|
fn add(self, rhs: Duration) -> Duration {
|
2016-09-14 00:58:45 +00:00
|
|
|
self.checked_add(rhs).expect("overflow when adding durations")
|
2014-12-01 19:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl AddAssign for Duration {
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2016-03-23 06:28:22 +00:00
|
|
|
fn add_assign(&mut self, rhs: Duration) {
|
|
|
|
*self = *self + rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2014-12-31 20:45:13 +00:00
|
|
|
impl Sub for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2014-12-01 19:32:36 +00:00
|
|
|
fn sub(self, rhs: Duration) -> Duration {
|
2016-09-14 00:58:45 +00:00
|
|
|
self.checked_sub(rhs).expect("overflow when subtracting durations")
|
2014-12-01 19:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl SubAssign for Duration {
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2016-03-23 06:28:22 +00:00
|
|
|
fn sub_assign(&mut self, rhs: Duration) {
|
|
|
|
*self = *self - rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2015-04-28 18:40:04 +00:00
|
|
|
impl Mul<u32> for Duration {
|
2014-12-31 20:45:13 +00:00
|
|
|
type Output = Duration;
|
|
|
|
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2015-04-28 18:40:04 +00:00
|
|
|
fn mul(self, rhs: u32) -> Duration {
|
2016-09-14 00:58:45 +00:00
|
|
|
self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
|
2014-12-01 19:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 15:40:33 +00:00
|
|
|
#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")]
|
2018-07-29 04:00:13 +00:00
|
|
|
impl Mul<Duration> for u32 {
|
|
|
|
type Output = Duration;
|
|
|
|
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2018-07-29 04:00:13 +00:00
|
|
|
fn mul(self, rhs: Duration) -> Duration {
|
2018-09-12 14:10:38 +00:00
|
|
|
rhs * self
|
2018-07-29 04:00:13 +00:00
|
|
|
}
|
2014-12-01 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl MulAssign<u32> for Duration {
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2016-03-23 06:28:22 +00:00
|
|
|
fn mul_assign(&mut self, rhs: u32) {
|
|
|
|
*self = *self * rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "duration", since = "1.3.0")]
|
2015-04-28 18:40:04 +00:00
|
|
|
impl Div<u32> for Duration {
|
2014-12-31 20:45:13 +00:00
|
|
|
type Output = Duration;
|
|
|
|
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2015-04-28 18:40:04 +00:00
|
|
|
fn div(self, rhs: u32) -> Duration {
|
2016-09-14 00:58:45 +00:00
|
|
|
self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
|
2014-07-15 02:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:28:22 +00:00
|
|
|
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
|
|
|
impl DivAssign<u32> for Duration {
|
2023-10-03 15:32:25 +00:00
|
|
|
#[inline]
|
2016-03-23 06:28:22 +00:00
|
|
|
fn div_assign(&mut self, rhs: u32) {
|
|
|
|
*self = *self / rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 18:19:19 +00:00
|
|
|
macro_rules! sum_durations {
|
|
|
|
($iter:expr) => {{
|
|
|
|
let mut total_secs: u64 = 0;
|
|
|
|
let mut total_nanos: u64 = 0;
|
|
|
|
|
|
|
|
for entry in $iter {
|
|
|
|
total_secs =
|
|
|
|
total_secs.checked_add(entry.secs).expect("overflow in iter::sum over durations");
|
2022-09-14 10:43:02 +00:00
|
|
|
total_nanos = match total_nanos.checked_add(entry.nanos.0 as u64) {
|
2018-06-16 18:19:19 +00:00
|
|
|
Some(n) => n,
|
|
|
|
None => {
|
|
|
|
total_secs = total_secs
|
|
|
|
.checked_add(total_nanos / NANOS_PER_SEC as u64)
|
|
|
|
.expect("overflow in iter::sum over durations");
|
2022-09-14 10:43:02 +00:00
|
|
|
(total_nanos % NANOS_PER_SEC as u64) + entry.nanos.0 as u64
|
2018-06-16 18:19:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
total_secs = total_secs
|
|
|
|
.checked_add(total_nanos / NANOS_PER_SEC as u64)
|
|
|
|
.expect("overflow in iter::sum over durations");
|
|
|
|
total_nanos = total_nanos % NANOS_PER_SEC as u64;
|
2022-09-14 10:43:02 +00:00
|
|
|
Duration::new(total_secs, total_nanos as u32)
|
2018-06-16 18:19:19 +00:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:22:47 +00:00
|
|
|
#[stable(feature = "duration_sum", since = "1.16.0")]
|
|
|
|
impl Sum for Duration {
|
|
|
|
fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
|
2018-06-16 18:19:19 +00:00
|
|
|
sum_durations!(iter)
|
2016-12-30 19:22:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "duration_sum", since = "1.16.0")]
|
|
|
|
impl<'a> Sum<&'a Duration> for Duration {
|
|
|
|
fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
|
2018-06-16 18:19:19 +00:00
|
|
|
sum_durations!(iter)
|
2016-12-30 19:22:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-06 11:46:20 +00:00
|
|
|
|
|
|
|
#[stable(feature = "duration_debug_impl", since = "1.27.0")]
|
|
|
|
impl fmt::Debug for Duration {
|
2019-04-18 23:37:12 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-05-06 11:46:20 +00:00
|
|
|
/// Formats a floating point number in decimal notation.
|
|
|
|
///
|
|
|
|
/// The number is given as the `integer_part` and a fractional part.
|
|
|
|
/// The value of the fractional part is `fractional_part / divisor`. So
|
|
|
|
/// `integer_part` = 3, `fractional_part` = 12 and `divisor` = 100
|
|
|
|
/// represents the number `3.012`. Trailing zeros are omitted.
|
|
|
|
///
|
|
|
|
/// `divisor` must not be above 100_000_000. It also should be a power
|
|
|
|
/// of 10, everything else doesn't make sense. `fractional_part` has
|
|
|
|
/// to be less than `10 * divisor`!
|
2021-09-16 01:08:41 +00:00
|
|
|
///
|
|
|
|
/// A prefix and postfix may be added. The whole thing is padded
|
|
|
|
/// to the formatter's `width`, if specified.
|
2018-05-06 11:46:20 +00:00
|
|
|
fn fmt_decimal(
|
2019-04-18 23:37:12 +00:00
|
|
|
f: &mut fmt::Formatter<'_>,
|
2022-09-29 17:50:10 +00:00
|
|
|
integer_part: u64,
|
2018-05-06 11:46:20 +00:00
|
|
|
mut fractional_part: u32,
|
|
|
|
mut divisor: u32,
|
2021-09-16 01:08:41 +00:00
|
|
|
prefix: &str,
|
|
|
|
postfix: &str,
|
2018-05-06 11:46:20 +00:00
|
|
|
) -> fmt::Result {
|
|
|
|
// Encode the fractional part into a temporary buffer. The buffer
|
|
|
|
// only need to hold 9 elements, because `fractional_part` has to
|
|
|
|
// be smaller than 10^9. The buffer is prefilled with '0' digits
|
|
|
|
// to simplify the code below.
|
|
|
|
let mut buf = [b'0'; 9];
|
|
|
|
|
|
|
|
// The next digit is written at this position
|
|
|
|
let mut pos = 0;
|
|
|
|
|
2018-05-16 13:08:39 +00:00
|
|
|
// We keep writing digits into the buffer while there are non-zero
|
|
|
|
// digits left and we haven't written enough digits yet.
|
|
|
|
while fractional_part > 0 && pos < f.precision().unwrap_or(9) {
|
2018-05-06 11:46:20 +00:00
|
|
|
// Write new digit into the buffer
|
|
|
|
buf[pos] = b'0' + (fractional_part / divisor) as u8;
|
|
|
|
|
|
|
|
fractional_part %= divisor;
|
|
|
|
divisor /= 10;
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
|
2018-05-16 12:46:37 +00:00
|
|
|
// If a precision < 9 was specified, there may be some non-zero
|
|
|
|
// digits left that weren't written into the buffer. In that case we
|
|
|
|
// need to perform rounding to match the semantics of printing
|
|
|
|
// normal floating point numbers. However, we only need to do work
|
|
|
|
// when rounding up. This happens if the first digit of the
|
|
|
|
// remaining ones is >= 5.
|
2022-09-29 17:50:10 +00:00
|
|
|
let integer_part = if fractional_part > 0 && fractional_part >= divisor * 5 {
|
2018-05-16 12:46:37 +00:00
|
|
|
// Round up the number contained in the buffer. We go through
|
|
|
|
// the buffer backwards and keep track of the carry.
|
|
|
|
let mut rev_pos = pos;
|
|
|
|
let mut carry = true;
|
|
|
|
while carry && rev_pos > 0 {
|
|
|
|
rev_pos -= 1;
|
|
|
|
|
|
|
|
// If the digit in the buffer is not '9', we just need to
|
|
|
|
// increment it and can stop then (since we don't have a
|
|
|
|
// carry anymore). Otherwise, we set it to '0' (overflow)
|
|
|
|
// and continue.
|
|
|
|
if buf[rev_pos] < b'9' {
|
|
|
|
buf[rev_pos] += 1;
|
|
|
|
carry = false;
|
|
|
|
} else {
|
|
|
|
buf[rev_pos] = b'0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have the carry bit set, that means that we set
|
|
|
|
// the whole buffer to '0's and need to increment the integer
|
|
|
|
// part.
|
|
|
|
if carry {
|
2022-09-29 17:50:10 +00:00
|
|
|
// If `integer_part == u64::MAX` and precision < 9, any
|
|
|
|
// carry of the overflow during rounding of the
|
|
|
|
// `fractional_part` into the `integer_part` will cause the
|
|
|
|
// `integer_part` itself to overflow. Avoid this by using an
|
|
|
|
// `Option<u64>`, with `None` representing `u64::MAX + 1`.
|
|
|
|
integer_part.checked_add(1)
|
|
|
|
} else {
|
|
|
|
Some(integer_part)
|
2018-05-16 12:46:37 +00:00
|
|
|
}
|
2022-09-29 17:50:10 +00:00
|
|
|
} else {
|
|
|
|
Some(integer_part)
|
|
|
|
};
|
2018-05-16 12:46:37 +00:00
|
|
|
|
2018-05-16 13:08:39 +00:00
|
|
|
// Determine the end of the buffer: if precision is set, we just
|
|
|
|
// use as many digits from the buffer (capped to 9). If it isn't
|
|
|
|
// set, we only use all digits up to the last non-zero one.
|
2019-04-15 02:23:21 +00:00
|
|
|
let end = f.precision().map(|p| crate::cmp::min(p, 9)).unwrap_or(pos);
|
2018-05-16 13:08:39 +00:00
|
|
|
|
2021-09-16 01:08:41 +00:00
|
|
|
// This closure emits the formatted duration without emitting any
|
|
|
|
// padding (padding is calculated below).
|
|
|
|
let emit_without_padding = |f: &mut fmt::Formatter<'_>| {
|
2022-09-29 17:50:10 +00:00
|
|
|
if let Some(integer_part) = integer_part {
|
|
|
|
write!(f, "{}{}", prefix, integer_part)?;
|
|
|
|
} else {
|
|
|
|
// u64::MAX + 1 == 18446744073709551616
|
|
|
|
write!(f, "{}18446744073709551616", prefix)?;
|
|
|
|
}
|
2021-09-16 01:08:41 +00:00
|
|
|
|
|
|
|
// Write the decimal point and the fractional part (if any).
|
|
|
|
if end > 0 {
|
|
|
|
// SAFETY: We are only writing ASCII digits into the buffer and
|
|
|
|
// it was initialized with '0's, so it contains valid UTF8.
|
|
|
|
let s = unsafe { crate::str::from_utf8_unchecked(&buf[..end]) };
|
|
|
|
|
|
|
|
// If the user request a precision > 9, we pad '0's at the end.
|
|
|
|
let w = f.precision().unwrap_or(pos);
|
|
|
|
write!(f, ".{:0<width$}", s, width = w)?;
|
|
|
|
}
|
2018-05-06 11:46:20 +00:00
|
|
|
|
2021-09-16 01:08:41 +00:00
|
|
|
write!(f, "{}", postfix)
|
|
|
|
};
|
|
|
|
|
|
|
|
match f.width() {
|
|
|
|
None => {
|
|
|
|
// No `width` specified. There's no need to calculate the
|
|
|
|
// length of the output in this case, just emit it.
|
|
|
|
emit_without_padding(f)
|
|
|
|
}
|
|
|
|
Some(requested_w) => {
|
|
|
|
// A `width` was specified. Calculate the actual width of
|
|
|
|
// the output in order to calculate the required padding.
|
|
|
|
// It consists of 4 parts:
|
|
|
|
// 1. The prefix: is either "+" or "", so we can just use len().
|
|
|
|
// 2. The postfix: can be "µs" so we have to count UTF8 characters.
|
|
|
|
let mut actual_w = prefix.len() + postfix.chars().count();
|
|
|
|
// 3. The integer part:
|
2022-09-29 17:50:10 +00:00
|
|
|
if let Some(integer_part) = integer_part {
|
|
|
|
if let Some(log) = integer_part.checked_ilog10() {
|
|
|
|
// integer_part is > 0, so has length log10(x)+1
|
|
|
|
actual_w += 1 + log as usize;
|
|
|
|
} else {
|
|
|
|
// integer_part is 0, so has length 1.
|
|
|
|
actual_w += 1;
|
|
|
|
}
|
2021-09-16 01:08:41 +00:00
|
|
|
} else {
|
2022-09-29 17:50:10 +00:00
|
|
|
// integer_part is u64::MAX + 1, so has length 20
|
|
|
|
actual_w += 20;
|
2021-09-16 01:08:41 +00:00
|
|
|
}
|
|
|
|
// 4. The fractional part (if any):
|
|
|
|
if end > 0 {
|
|
|
|
let frac_part_w = f.precision().unwrap_or(pos);
|
|
|
|
actual_w += 1 + frac_part_w;
|
|
|
|
}
|
|
|
|
|
|
|
|
if requested_w <= actual_w {
|
|
|
|
// Output is already longer than `width`, so don't pad.
|
|
|
|
emit_without_padding(f)
|
|
|
|
} else {
|
|
|
|
// We need to add padding. Use the `Formatter::padding` helper function.
|
2023-04-20 16:03:47 +00:00
|
|
|
let default_align = fmt::Alignment::Left;
|
2021-09-16 01:08:41 +00:00
|
|
|
let post_padding = f.padding(requested_w - actual_w, default_align)?;
|
|
|
|
emit_without_padding(f)?;
|
|
|
|
post_padding.write(f)
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 11:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print leading '+' sign if requested
|
2021-09-16 01:08:41 +00:00
|
|
|
let prefix = if f.sign_plus() { "+" } else { "" };
|
2018-05-06 11:46:20 +00:00
|
|
|
|
|
|
|
if self.secs > 0 {
|
2022-09-14 10:43:02 +00:00
|
|
|
fmt_decimal(f, self.secs, self.nanos.0, NANOS_PER_SEC / 10, prefix, "s")
|
|
|
|
} else if self.nanos.0 >= NANOS_PER_MILLI {
|
2020-12-10 05:37:39 +00:00
|
|
|
fmt_decimal(
|
|
|
|
f,
|
2022-09-14 10:43:02 +00:00
|
|
|
(self.nanos.0 / NANOS_PER_MILLI) as u64,
|
|
|
|
self.nanos.0 % NANOS_PER_MILLI,
|
2020-12-10 05:37:39 +00:00
|
|
|
NANOS_PER_MILLI / 10,
|
2021-09-16 01:08:41 +00:00
|
|
|
prefix,
|
|
|
|
"ms",
|
|
|
|
)
|
2022-09-14 10:43:02 +00:00
|
|
|
} else if self.nanos.0 >= NANOS_PER_MICRO {
|
2020-12-10 05:37:39 +00:00
|
|
|
fmt_decimal(
|
|
|
|
f,
|
2022-09-14 10:43:02 +00:00
|
|
|
(self.nanos.0 / NANOS_PER_MICRO) as u64,
|
|
|
|
self.nanos.0 % NANOS_PER_MICRO,
|
2020-12-10 05:37:39 +00:00
|
|
|
NANOS_PER_MICRO / 10,
|
2021-09-16 01:08:41 +00:00
|
|
|
prefix,
|
|
|
|
"µs",
|
|
|
|
)
|
2018-05-06 11:46:20 +00:00
|
|
|
} else {
|
2022-09-14 10:43:02 +00:00
|
|
|
fmt_decimal(f, self.nanos.0 as u64, 0, 1, prefix, "ns")
|
2018-05-06 11:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-14 12:16:13 +00:00
|
|
|
|
|
|
|
/// An error which can be returned when converting a floating-point value of seconds
|
|
|
|
/// into a [`Duration`].
|
|
|
|
///
|
|
|
|
/// This error is used as the error type for [`Duration::try_from_secs_f32`] and
|
|
|
|
/// [`Duration::try_from_secs_f64`].
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// if let Err(e) = Duration::try_from_secs_f32(-1.0) {
|
2022-02-12 19:16:17 +00:00
|
|
|
/// println!("Failed conversion to Duration: {e}");
|
2021-06-14 12:16:13 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-10-31 14:43:15 +00:00
|
|
|
#[stable(feature = "duration_checked_float", since = "1.66.0")]
|
2022-09-25 16:52:44 +00:00
|
|
|
pub struct TryFromFloatSecsError {
|
|
|
|
kind: TryFromFloatSecsErrorKind,
|
2021-06-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 16:52:44 +00:00
|
|
|
impl TryFromFloatSecsError {
|
2021-06-14 12:16:13 +00:00
|
|
|
const fn description(&self) -> &'static str {
|
|
|
|
match self.kind {
|
2022-09-25 16:52:44 +00:00
|
|
|
TryFromFloatSecsErrorKind::Negative => {
|
2022-01-26 15:14:25 +00:00
|
|
|
"can not convert float seconds to Duration: value is negative"
|
|
|
|
}
|
2022-09-25 16:52:44 +00:00
|
|
|
TryFromFloatSecsErrorKind::OverflowOrNan => {
|
2022-01-26 15:14:25 +00:00
|
|
|
"can not convert float seconds to Duration: value is either too big or NaN"
|
|
|
|
}
|
2021-06-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 14:43:15 +00:00
|
|
|
#[stable(feature = "duration_checked_float", since = "1.66.0")]
|
2022-09-25 16:52:44 +00:00
|
|
|
impl fmt::Display for TryFromFloatSecsError {
|
2021-06-14 12:16:13 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-01-26 15:14:25 +00:00
|
|
|
self.description().fmt(f)
|
2021-06-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-09-25 16:52:44 +00:00
|
|
|
enum TryFromFloatSecsErrorKind {
|
2021-10-16 08:25:18 +00:00
|
|
|
// Value is negative.
|
|
|
|
Negative,
|
2022-01-26 15:14:25 +00:00
|
|
|
// Value is either too big to be represented as `Duration` or `NaN`.
|
|
|
|
OverflowOrNan,
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_from_secs {
|
|
|
|
(
|
|
|
|
secs = $secs: expr,
|
|
|
|
mantissa_bits = $mant_bits: literal,
|
|
|
|
exponent_bits = $exp_bits: literal,
|
|
|
|
offset = $offset: literal,
|
|
|
|
bits_ty = $bits_ty:ty,
|
|
|
|
double_ty = $double_ty:ty,
|
|
|
|
) => {{
|
|
|
|
const MIN_EXP: i16 = 1 - (1i16 << $exp_bits) / 2;
|
|
|
|
const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
|
|
|
|
const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
|
|
|
|
|
2022-10-14 14:51:20 +00:00
|
|
|
if $secs < 0.0 {
|
2022-09-25 16:52:44 +00:00
|
|
|
return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::Negative });
|
2022-01-26 15:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let bits = $secs.to_bits();
|
|
|
|
let mant = (bits & MANT_MASK) | (MANT_MASK + 1);
|
|
|
|
let exp = ((bits >> $mant_bits) & EXP_MASK) as i16 + MIN_EXP;
|
|
|
|
|
2022-05-25 02:01:11 +00:00
|
|
|
let (secs, nanos) = if exp < -31 {
|
|
|
|
// the input represents less than 1ns and can not be rounded to it
|
2022-01-26 15:14:25 +00:00
|
|
|
(0u64, 0u32)
|
|
|
|
} else if exp < 0 {
|
|
|
|
// the input is less than 1 second
|
|
|
|
let t = <$double_ty>::from(mant) << ($offset + exp);
|
2022-04-14 17:44:09 +00:00
|
|
|
let nanos_offset = $mant_bits + $offset;
|
|
|
|
let nanos_tmp = u128::from(NANOS_PER_SEC) * u128::from(t);
|
|
|
|
let nanos = (nanos_tmp >> nanos_offset) as u32;
|
2022-05-25 02:01:11 +00:00
|
|
|
|
|
|
|
let rem_mask = (1 << nanos_offset) - 1;
|
|
|
|
let rem_msb_mask = 1 << (nanos_offset - 1);
|
|
|
|
let rem = nanos_tmp & rem_mask;
|
|
|
|
let is_tie = rem == rem_msb_mask;
|
|
|
|
let is_even = (nanos & 1) == 0;
|
|
|
|
let rem_msb = nanos_tmp & rem_msb_mask == 0;
|
|
|
|
let add_ns = !(rem_msb || (is_even && is_tie));
|
|
|
|
|
2022-08-18 02:13:37 +00:00
|
|
|
// f32 does not have enough precision to trigger the second branch
|
2022-05-27 01:59:01 +00:00
|
|
|
// since it can not represent numbers between 0.999_999_940_395 and 1.0.
|
2022-05-27 00:22:56 +00:00
|
|
|
let nanos = nanos + add_ns as u32;
|
2022-05-27 02:15:22 +00:00
|
|
|
if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) { (0, nanos) } else { (1, 0) }
|
2022-01-26 15:14:25 +00:00
|
|
|
} else if exp < $mant_bits {
|
2022-04-14 17:44:09 +00:00
|
|
|
let secs = u64::from(mant >> ($mant_bits - exp));
|
2022-01-26 15:14:25 +00:00
|
|
|
let t = <$double_ty>::from((mant << exp) & MANT_MASK);
|
2022-05-25 02:01:11 +00:00
|
|
|
let nanos_offset = $mant_bits;
|
2022-04-14 17:44:09 +00:00
|
|
|
let nanos_tmp = <$double_ty>::from(NANOS_PER_SEC) * t;
|
2022-05-25 02:01:11 +00:00
|
|
|
let nanos = (nanos_tmp >> nanos_offset) as u32;
|
|
|
|
|
|
|
|
let rem_mask = (1 << nanos_offset) - 1;
|
|
|
|
let rem_msb_mask = 1 << (nanos_offset - 1);
|
|
|
|
let rem = nanos_tmp & rem_mask;
|
|
|
|
let is_tie = rem == rem_msb_mask;
|
|
|
|
let is_even = (nanos & 1) == 0;
|
|
|
|
let rem_msb = nanos_tmp & rem_msb_mask == 0;
|
|
|
|
let add_ns = !(rem_msb || (is_even && is_tie));
|
|
|
|
|
2022-08-18 02:13:37 +00:00
|
|
|
// f32 does not have enough precision to trigger the second branch.
|
2022-05-27 01:59:01 +00:00
|
|
|
// For example, it can not represent numbers between 1.999_999_880...
|
2022-08-18 02:13:37 +00:00
|
|
|
// and 2.0. Bigger values result in even smaller precision of the
|
2022-05-27 01:59:01 +00:00
|
|
|
// fractional part.
|
2022-05-27 00:22:56 +00:00
|
|
|
let nanos = nanos + add_ns as u32;
|
2022-05-27 01:59:01 +00:00
|
|
|
if ($mant_bits == 23) || (nanos != NANOS_PER_SEC) {
|
|
|
|
(secs, nanos)
|
|
|
|
} else {
|
|
|
|
(secs + 1, 0)
|
|
|
|
}
|
2022-01-26 15:14:25 +00:00
|
|
|
} else if exp < 64 {
|
|
|
|
// the input has no fractional part
|
|
|
|
let secs = u64::from(mant) << (exp - $mant_bits);
|
|
|
|
(secs, 0)
|
|
|
|
} else {
|
2022-09-25 16:52:44 +00:00
|
|
|
return Err(TryFromFloatSecsError { kind: TryFromFloatSecsErrorKind::OverflowOrNan });
|
2022-01-26 15:14:25 +00:00
|
|
|
};
|
|
|
|
|
2022-09-14 10:43:02 +00:00
|
|
|
Ok(Duration::new(secs, nanos))
|
2022-01-26 15:14:25 +00:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Duration {
|
|
|
|
/// The checked version of [`from_secs_f32`].
|
|
|
|
///
|
|
|
|
/// [`from_secs_f32`]: Duration::from_secs_f32
|
|
|
|
///
|
|
|
|
/// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let res = Duration::try_from_secs_f32(0.0);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 0)));
|
|
|
|
/// let res = Duration::try_from_secs_f32(1e-20);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 0)));
|
|
|
|
/// let res = Duration::try_from_secs_f32(4.2e-7);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 420)));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::try_from_secs_f32(2.7);
|
2022-04-14 17:44:09 +00:00
|
|
|
/// assert_eq!(res, Ok(Duration::new(2, 700_000_048)));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// let res = Duration::try_from_secs_f32(3e10);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(30_000_001_024, 0)));
|
|
|
|
/// // subnormal float:
|
|
|
|
/// let res = Duration::try_from_secs_f32(f32::from_bits(1));
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 0)));
|
|
|
|
///
|
|
|
|
/// let res = Duration::try_from_secs_f32(-5.0);
|
|
|
|
/// assert!(res.is_err());
|
|
|
|
/// let res = Duration::try_from_secs_f32(f32::NAN);
|
|
|
|
/// assert!(res.is_err());
|
|
|
|
/// let res = Duration::try_from_secs_f32(2e19);
|
|
|
|
/// assert!(res.is_err());
|
2022-05-25 02:01:11 +00:00
|
|
|
///
|
2022-05-25 02:14:30 +00:00
|
|
|
/// // the conversion uses rounding with tie resolution to even
|
|
|
|
/// let res = Duration::try_from_secs_f32(0.999e-9);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 1)));
|
2022-05-25 02:01:11 +00:00
|
|
|
///
|
|
|
|
/// // this float represents exactly 976562.5e-9
|
|
|
|
/// let val = f32::from_bits(0x3A80_0000);
|
|
|
|
/// let res = Duration::try_from_secs_f32(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 976_562)));
|
|
|
|
///
|
|
|
|
/// // this float represents exactly 2929687.5e-9
|
|
|
|
/// let val = f32::from_bits(0x3B40_0000);
|
|
|
|
/// let res = Duration::try_from_secs_f32(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
|
|
|
|
///
|
|
|
|
/// // this float represents exactly 1.000_976_562_5
|
|
|
|
/// let val = f32::from_bits(0x3F802000);
|
|
|
|
/// let res = Duration::try_from_secs_f32(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(1, 976_562)));
|
|
|
|
///
|
|
|
|
/// // this float represents exactly 1.002_929_687_5
|
|
|
|
/// let val = f32::from_bits(0x3F806000);
|
|
|
|
/// let res = Duration::try_from_secs_f32(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// ```
|
2022-10-31 14:43:15 +00:00
|
|
|
#[stable(feature = "duration_checked_float", since = "1.66.0")]
|
2022-01-26 15:14:25 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn try_from_secs_f32(secs: f32) -> Result<Duration, TryFromFloatSecsError> {
|
2022-01-26 15:14:25 +00:00
|
|
|
try_from_secs!(
|
|
|
|
secs = secs,
|
|
|
|
mantissa_bits = 23,
|
|
|
|
exponent_bits = 8,
|
|
|
|
offset = 41,
|
|
|
|
bits_ty = u32,
|
|
|
|
double_ty = u64,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The checked version of [`from_secs_f64`].
|
|
|
|
///
|
|
|
|
/// [`from_secs_f64`]: Duration::from_secs_f64
|
|
|
|
///
|
|
|
|
/// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let res = Duration::try_from_secs_f64(0.0);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 0)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(1e-20);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 0)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(4.2e-7);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 420)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(2.7);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(2, 700_000_000)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(3e10);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(30_000_000_000, 0)));
|
|
|
|
/// // subnormal float
|
|
|
|
/// let res = Duration::try_from_secs_f64(f64::from_bits(1));
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 0)));
|
|
|
|
///
|
|
|
|
/// let res = Duration::try_from_secs_f64(-5.0);
|
|
|
|
/// assert!(res.is_err());
|
|
|
|
/// let res = Duration::try_from_secs_f64(f64::NAN);
|
|
|
|
/// assert!(res.is_err());
|
|
|
|
/// let res = Duration::try_from_secs_f64(2e19);
|
|
|
|
/// assert!(res.is_err());
|
2022-05-25 02:01:11 +00:00
|
|
|
///
|
2022-05-25 02:14:30 +00:00
|
|
|
/// // the conversion uses rounding with tie resolution to even
|
|
|
|
/// let res = Duration::try_from_secs_f64(0.999e-9);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 1)));
|
2022-05-27 01:59:01 +00:00
|
|
|
/// let res = Duration::try_from_secs_f64(0.999_999_999_499);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 999_999_999)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(0.999_999_999_501);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(1, 0)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(42.999_999_999_499);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(42, 999_999_999)));
|
|
|
|
/// let res = Duration::try_from_secs_f64(42.999_999_999_501);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(43, 0)));
|
2022-05-25 02:01:11 +00:00
|
|
|
///
|
|
|
|
/// // this float represents exactly 976562.5e-9
|
|
|
|
/// let val = f64::from_bits(0x3F50_0000_0000_0000);
|
|
|
|
/// let res = Duration::try_from_secs_f64(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 976_562)));
|
|
|
|
///
|
|
|
|
/// // this float represents exactly 2929687.5e-9
|
|
|
|
/// let val = f64::from_bits(0x3F68_0000_0000_0000);
|
|
|
|
/// let res = Duration::try_from_secs_f64(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(0, 2_929_688)));
|
|
|
|
///
|
|
|
|
/// // this float represents exactly 1.000_976_562_5
|
|
|
|
/// let val = f64::from_bits(0x3FF0_0400_0000_0000);
|
|
|
|
/// let res = Duration::try_from_secs_f64(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(1, 976_562)));
|
|
|
|
///
|
|
|
|
/// // this float represents exactly 1.002_929_687_5
|
|
|
|
/// let val = f64::from_bits(0x3_FF00_C000_0000_000);
|
|
|
|
/// let res = Duration::try_from_secs_f64(val);
|
|
|
|
/// assert_eq!(res, Ok(Duration::new(1, 2_929_688)));
|
2022-01-26 15:14:25 +00:00
|
|
|
/// ```
|
2022-10-31 14:43:15 +00:00
|
|
|
#[stable(feature = "duration_checked_float", since = "1.66.0")]
|
2022-01-26 15:14:25 +00:00
|
|
|
#[inline]
|
2023-04-16 07:20:26 +00:00
|
|
|
pub fn try_from_secs_f64(secs: f64) -> Result<Duration, TryFromFloatSecsError> {
|
2022-01-26 15:14:25 +00:00
|
|
|
try_from_secs!(
|
|
|
|
secs = secs,
|
|
|
|
mantissa_bits = 52,
|
|
|
|
exponent_bits = 11,
|
|
|
|
offset = 44,
|
|
|
|
bits_ty = u64,
|
|
|
|
double_ty = u128,
|
|
|
|
)
|
|
|
|
}
|
2021-06-14 12:16:13 +00:00
|
|
|
}
|