pub struct Duration { /* private fields */ }
Expand description
A span of time with nanosecond precision.
Each Duration
is composed of a whole number of seconds and a fractional part represented in
nanoseconds.
This implementation allows for negative durations, unlike core::time::Duration
.
Implementations§
Source§impl Duration
impl Duration
Sourcepub const NANOSECOND: Self = _
pub const NANOSECOND: Self = _
Sourcepub const MICROSECOND: Self = _
pub const MICROSECOND: Self = _
Sourcepub const MILLISECOND: Self = _
pub const MILLISECOND: Self = _
Sourcepub const MIN: Self = _
pub const MIN: Self = _
The minimum possible duration. Adding any negative duration to this will cause an overflow.
Sourcepub const MAX: Self = _
pub const MAX: Self = _
The maximum possible duration. Adding any positive duration to this will cause an overflow.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Sourcepub const fn unsigned_abs(self) -> StdDuration
pub const fn unsigned_abs(self) -> StdDuration
Convert the existing Duration
to a std::time::Duration
and its sign. This returns a
std::time::Duration
and does not saturate the returned value (unlike Duration::abs
).
Sourcepub const fn new(seconds: i64, nanoseconds: i32) -> Self
pub const fn new(seconds: i64, nanoseconds: i32) -> Self
Create a new Duration
with the provided seconds and nanoseconds. If nanoseconds is at
least ±109, it will wrap to the number of seconds.
assert_eq!(Duration::new(1, 0), 1.seconds());
assert_eq!(Duration::new(-1, 0), (-1).seconds());
assert_eq!(Duration::new(1, 2_000_000_000), 3.seconds());
§Panics
This may panic if an overflow occurs.
Sourcepub const fn weeks(weeks: i64) -> Self
pub const fn weeks(weeks: i64) -> Self
Create a new Duration
with the given number of weeks. Equivalent to
Duration::seconds(weeks * 604_800)
.
§Panics
This may panic if an overflow occurs.
Sourcepub const fn days(days: i64) -> Self
pub const fn days(days: i64) -> Self
Create a new Duration
with the given number of days. Equivalent to
Duration::seconds(days * 86_400)
.
§Panics
This may panic if an overflow occurs.
Sourcepub const fn hours(hours: i64) -> Self
pub const fn hours(hours: i64) -> Self
Create a new Duration
with the given number of hours. Equivalent to
Duration::seconds(hours * 3_600)
.
§Panics
This may panic if an overflow occurs.
Sourcepub const fn minutes(minutes: i64) -> Self
pub const fn minutes(minutes: i64) -> Self
Create a new Duration
with the given number of minutes. Equivalent to
Duration::seconds(minutes * 60)
.
§Panics
This may panic if an overflow occurs.
Sourcepub fn seconds_f64(seconds: f64) -> Self
pub fn seconds_f64(seconds: f64) -> Self
Sourcepub fn seconds_f32(seconds: f32) -> Self
pub fn seconds_f32(seconds: f32) -> Self
Sourcepub fn saturating_seconds_f64(seconds: f64) -> Self
pub fn saturating_seconds_f64(seconds: f64) -> Self
Creates a new Duration
from the specified number of seconds
represented as f64
. Any values that are out of bounds are saturated at
the minimum or maximum respectively. NaN
gets turned into a Duration
of 0 seconds.
assert_eq!(Duration::saturating_seconds_f64(0.5), 0.5.seconds());
assert_eq!(Duration::saturating_seconds_f64(-0.5), (-0.5).seconds());
assert_eq!(
Duration::saturating_seconds_f64(f64::NAN),
Duration::new(0, 0),
);
assert_eq!(
Duration::saturating_seconds_f64(f64::NEG_INFINITY),
Duration::MIN,
);
assert_eq!(
Duration::saturating_seconds_f64(f64::INFINITY),
Duration::MAX,
);
Sourcepub fn saturating_seconds_f32(seconds: f32) -> Self
pub fn saturating_seconds_f32(seconds: f32) -> Self
Creates a new Duration
from the specified number of seconds
represented as f32
. Any values that are out of bounds are saturated at
the minimum or maximum respectively. NaN
gets turned into a Duration
of 0 seconds.
assert_eq!(Duration::saturating_seconds_f32(0.5), 0.5.seconds());
assert_eq!(Duration::saturating_seconds_f32(-0.5), (-0.5).seconds());
assert_eq!(
Duration::saturating_seconds_f32(f32::NAN),
Duration::new(0, 0),
);
assert_eq!(
Duration::saturating_seconds_f32(f32::NEG_INFINITY),
Duration::MIN,
);
assert_eq!(
Duration::saturating_seconds_f32(f32::INFINITY),
Duration::MAX,
);
Sourcepub fn checked_seconds_f64(seconds: f64) -> Option<Self>
pub fn checked_seconds_f64(seconds: f64) -> Option<Self>
Creates a new Duration
from the specified number of seconds
represented as f64
. Returns None
if the Duration
can’t be
represented.
assert_eq!(Duration::checked_seconds_f64(0.5), Some(0.5.seconds()));
assert_eq!(Duration::checked_seconds_f64(-0.5), Some((-0.5).seconds()));
assert_eq!(Duration::checked_seconds_f64(f64::NAN), None);
assert_eq!(Duration::checked_seconds_f64(f64::NEG_INFINITY), None);
assert_eq!(Duration::checked_seconds_f64(f64::INFINITY), None);
Sourcepub fn checked_seconds_f32(seconds: f32) -> Option<Self>
pub fn checked_seconds_f32(seconds: f32) -> Option<Self>
Creates a new Duration
from the specified number of seconds
represented as f32
. Returns None
if the Duration
can’t be
represented.
assert_eq!(Duration::checked_seconds_f32(0.5), Some(0.5.seconds()));
assert_eq!(Duration::checked_seconds_f32(-0.5), Some((-0.5).seconds()));
assert_eq!(Duration::checked_seconds_f32(f32::NAN), None);
assert_eq!(Duration::checked_seconds_f32(f32::NEG_INFINITY), None);
assert_eq!(Duration::checked_seconds_f32(f32::INFINITY), None);
Sourcepub const fn milliseconds(milliseconds: i64) -> Self
pub const fn milliseconds(milliseconds: i64) -> Self
Sourcepub const fn microseconds(microseconds: i64) -> Self
pub const fn microseconds(microseconds: i64) -> Self
Sourcepub const fn nanoseconds(nanoseconds: i64) -> Self
pub const fn nanoseconds(nanoseconds: i64) -> Self
Sourcepub const fn whole_weeks(self) -> i64
pub const fn whole_weeks(self) -> i64
Sourcepub const fn whole_days(self) -> i64
pub const fn whole_days(self) -> i64
Sourcepub const fn whole_hours(self) -> i64
pub const fn whole_hours(self) -> i64
Sourcepub const fn whole_minutes(self) -> i64
pub const fn whole_minutes(self) -> i64
Sourcepub const fn whole_seconds(self) -> i64
pub const fn whole_seconds(self) -> i64
Sourcepub fn as_seconds_f64(self) -> f64
pub fn as_seconds_f64(self) -> f64
Sourcepub fn as_seconds_f32(self) -> f32
pub fn as_seconds_f32(self) -> f32
Sourcepub const fn whole_milliseconds(self) -> i128
pub const fn whole_milliseconds(self) -> i128
Sourcepub const fn subsec_milliseconds(self) -> i16
pub const fn subsec_milliseconds(self) -> i16
Sourcepub const fn whole_microseconds(self) -> i128
pub const fn whole_microseconds(self) -> i128
Sourcepub const fn subsec_microseconds(self) -> i32
pub const fn subsec_microseconds(self) -> i32
Sourcepub const fn whole_nanoseconds(self) -> i128
pub const fn whole_nanoseconds(self) -> i128
Sourcepub const fn subsec_nanoseconds(self) -> i32
pub const fn subsec_nanoseconds(self) -> i32
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Sourcepub const fn checked_mul(self, rhs: i32) -> Option<Self>
pub const fn checked_mul(self, rhs: i32) -> Option<Self>
Computes self * rhs
, returning None
if an overflow occurred.
Sourcepub const fn checked_div(self, rhs: i32) -> Option<Self>
pub const fn checked_div(self, rhs: i32) -> Option<Self>
Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Computes self + rhs
, saturating if an overflow occurred.
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Computes self - rhs
, saturating if an overflow occurred.
Sourcepub const fn saturating_mul(self, rhs: i32) -> Self
pub const fn saturating_mul(self, rhs: i32) -> Self
Computes self * rhs
, saturating if an overflow occurred.
assert_eq!(5.seconds().saturating_mul(2), 10.seconds());
assert_eq!(5.seconds().saturating_mul(-2), (-10).seconds());
assert_eq!(5.seconds().saturating_mul(0), Duration::ZERO);
assert_eq!(Duration::MAX.saturating_mul(2), Duration::MAX);
assert_eq!(Duration::MIN.saturating_mul(2), Duration::MIN);
assert_eq!(Duration::MAX.saturating_mul(-2), Duration::MIN);
assert_eq!(Duration::MIN.saturating_mul(-2), Duration::MAX);
Trait Implementations§
Source§impl Add<Duration> for OffsetDateTime
impl Add<Duration> for OffsetDateTime
Source§impl Add<Duration> for PrimitiveDateTime
impl Add<Duration> for PrimitiveDateTime
Source§impl Add<Duration> for SystemTime
Available on crate feature std
only.
impl Add<Duration> for SystemTime
std
only.Source§impl Add<Duration> for Time
impl Add<Duration> for Time
Source§impl AddAssign<Duration> for Date
impl AddAssign<Duration> for Date
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read moreSource§impl AddAssign<Duration> for Duration
impl AddAssign<Duration> for Duration
Source§fn add_assign(&mut self, rhs: StdDuration)
fn add_assign(&mut self, rhs: StdDuration)
+=
operation. Read moreSource§impl AddAssign<Duration> for Duration
impl AddAssign<Duration> for Duration
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
§Panics
This may panic if the resulting addition cannot be represented.
Source§impl AddAssign<Duration> for Instant
Available on crate feature std
only.
impl AddAssign<Duration> for Instant
std
only.Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read moreSource§impl AddAssign<Duration> for OffsetDateTime
impl AddAssign<Duration> for OffsetDateTime
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
§Panics
This may panic if an overflow occurs.
Source§impl AddAssign<Duration> for PrimitiveDateTime
impl AddAssign<Duration> for PrimitiveDateTime
Source§fn add_assign(&mut self, duration: Duration)
fn add_assign(&mut self, duration: Duration)
§Panics
This may panic if an overflow occurs.
Source§impl AddAssign<Duration> for SystemTime
Available on crate feature std
only.
impl AddAssign<Duration> for SystemTime
std
only.Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read moreSource§impl AddAssign<Duration> for Time
impl AddAssign<Duration> for Time
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read moreSource§impl AddAssign for Duration
impl AddAssign for Duration
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl Display for Duration
The format returned by this implementation is not stable and must not be relied upon.
impl Display for Duration
The format returned by this implementation is not stable and must not be relied upon.
By default this produces an exact, full-precision printout of the duration.
For a concise, rounded printout instead, you can use the .N
format specifier:
For the purposes of this implementation, a day is exactly 24 hours and a minute is exactly 60 seconds.
Source§impl DivAssign<f32> for Duration
impl DivAssign<f32> for Duration
Source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read moreSource§impl DivAssign<f64> for Duration
impl DivAssign<f64> for Duration
Source§fn div_assign(&mut self, rhs: f64)
fn div_assign(&mut self, rhs: f64)
/=
operation. Read moreSource§impl DivAssign<i16> for Duration
impl DivAssign<i16> for Duration
Source§fn div_assign(&mut self, rhs: i16)
fn div_assign(&mut self, rhs: i16)
/=
operation. Read moreSource§impl DivAssign<i32> for Duration
impl DivAssign<i32> for Duration
Source§fn div_assign(&mut self, rhs: i32)
fn div_assign(&mut self, rhs: i32)
/=
operation. Read moreSource§impl DivAssign<i8> for Duration
impl DivAssign<i8> for Duration
Source§fn div_assign(&mut self, rhs: i8)
fn div_assign(&mut self, rhs: i8)
/=
operation. Read moreSource§impl DivAssign<u16> for Duration
impl DivAssign<u16> for Duration
Source§fn div_assign(&mut self, rhs: u16)
fn div_assign(&mut self, rhs: u16)
/=
operation. Read moreSource§impl DivAssign<u32> for Duration
impl DivAssign<u32> for Duration
Source§fn div_assign(&mut self, rhs: u32)
fn div_assign(&mut self, rhs: u32)
/=
operation. Read moreSource§impl DivAssign<u8> for Duration
impl DivAssign<u8> for Duration
Source§fn div_assign(&mut self, rhs: u8)
fn div_assign(&mut self, rhs: u8)
/=
operation. Read moreSource§impl MulAssign<f32> for Duration
impl MulAssign<f32> for Duration
Source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read moreSource§impl MulAssign<f64> for Duration
impl MulAssign<f64> for Duration
Source§fn mul_assign(&mut self, rhs: f64)
fn mul_assign(&mut self, rhs: f64)
*=
operation. Read moreSource§impl MulAssign<i16> for Duration
impl MulAssign<i16> for Duration
Source§fn mul_assign(&mut self, rhs: i16)
fn mul_assign(&mut self, rhs: i16)
*=
operation. Read moreSource§impl MulAssign<i32> for Duration
impl MulAssign<i32> for Duration
Source§fn mul_assign(&mut self, rhs: i32)
fn mul_assign(&mut self, rhs: i32)
*=
operation. Read moreSource§impl MulAssign<i8> for Duration
impl MulAssign<i8> for Duration
Source§fn mul_assign(&mut self, rhs: i8)
fn mul_assign(&mut self, rhs: i8)
*=
operation. Read moreSource§impl MulAssign<u16> for Duration
impl MulAssign<u16> for Duration
Source§fn mul_assign(&mut self, rhs: u16)
fn mul_assign(&mut self, rhs: u16)
*=
operation. Read moreSource§impl MulAssign<u32> for Duration
impl MulAssign<u32> for Duration
Source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
*=
operation. Read moreSource§impl MulAssign<u8> for Duration
impl MulAssign<u8> for Duration
Source§fn mul_assign(&mut self, rhs: u8)
fn mul_assign(&mut self, rhs: u8)
*=
operation. Read moreSource§impl Ord for Duration
impl Ord for Duration
Source§impl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
Source§impl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§impl Sub<Duration> for OffsetDateTime
impl Sub<Duration> for OffsetDateTime
Source§impl Sub<Duration> for PrimitiveDateTime
impl Sub<Duration> for PrimitiveDateTime
Source§impl Sub<Duration> for SystemTime
Available on crate feature std
only.
impl Sub<Duration> for SystemTime
std
only.Source§impl Sub<Duration> for Time
impl Sub<Duration> for Time
Source§impl SubAssign<Duration> for Date
impl SubAssign<Duration> for Date
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moreSource§impl SubAssign<Duration> for Duration
impl SubAssign<Duration> for Duration
Source§fn sub_assign(&mut self, rhs: StdDuration)
fn sub_assign(&mut self, rhs: StdDuration)
-=
operation. Read moreSource§impl SubAssign<Duration> for Duration
impl SubAssign<Duration> for Duration
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
§Panics
This may panic if the resulting subtraction can not be represented.
Source§impl SubAssign<Duration> for Instant
Available on crate feature std
only.
impl SubAssign<Duration> for Instant
std
only.Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moreSource§impl SubAssign<Duration> for OffsetDateTime
impl SubAssign<Duration> for OffsetDateTime
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
§Panics
This may panic if an overflow occurs.
Source§impl SubAssign<Duration> for PrimitiveDateTime
impl SubAssign<Duration> for PrimitiveDateTime
Source§fn sub_assign(&mut self, duration: Duration)
fn sub_assign(&mut self, duration: Duration)
§Panics
This may panic if an overflow occurs.
Source§impl SubAssign<Duration> for SystemTime
Available on crate feature std
only.
impl SubAssign<Duration> for SystemTime
std
only.Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moreSource§impl SubAssign<Duration> for Time
impl SubAssign<Duration> for Time
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moreSource§impl SubAssign for Duration
impl SubAssign for Duration
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more