pub struct Duration(/* private fields */);
Expand description
A Duration
type to represent a span of time, typically used for system
timeouts.
Each Duration
is composed of a whole number of seconds and a fractional part
represented in nanoseconds. If the underlying system does not support
nanosecond-level precision, APIs binding a system timeout will typically round up
the number of nanoseconds.
Duration
s implement many common traits, including Add
, Sub
, and other
ops
traits.
§Examples
use easytime::Duration;
let five_seconds = Duration::new(5, 0);
let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
assert_eq!(five_seconds_and_five_nanos.as_secs(), Some(5));
assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), Some(5));
let ten_millis = Duration::from_millis(10);
Implementations§
source§impl Duration
impl Duration
sourcepub const ZERO: Self = _
pub const ZERO: Self = _
A duration of zero time.
§Examples
use easytime::Duration;
let duration = Duration::ZERO;
assert!(duration.is_zero());
assert_eq!(duration.as_nanos(), Some(0));
sourcepub const MAX: Self = _
pub const MAX: Self = _
The maximum duration.
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.
§Examples
use easytime::Duration;
assert_eq!(Duration::MAX, Duration::new(u64::MAX, 1_000_000_000 - 1));
sourcepub const fn new(secs: u64, nanos: u32) -> Self
pub const fn new(secs: u64, nanos: u32) -> Self
Creates a new Duration
from the specified number of whole seconds and
additional nanoseconds.
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.
§Examples
use easytime::Duration;
let five_seconds = Duration::new(5, 0);
sourcepub const fn from_secs(secs: u64) -> Self
pub const fn from_secs(secs: u64) -> Self
Creates a new Duration
from the specified number of whole seconds.
§Examples
use easytime::Duration;
let duration = Duration::from_secs(5);
assert_eq!(Some(5), duration.as_secs());
assert_eq!(Some(0), duration.subsec_nanos());
sourcepub const fn from_millis(millis: u64) -> Self
pub const fn from_millis(millis: u64) -> Self
Creates a new Duration
from the specified number of milliseconds.
§Examples
use easytime::Duration;
let duration = Duration::from_millis(2_569);
assert_eq!(Some(2), duration.as_secs());
assert_eq!(Some(569_000_000), duration.subsec_nanos());
sourcepub const fn from_micros(micros: u64) -> Self
pub const fn from_micros(micros: u64) -> Self
Creates a new Duration
from the specified number of microseconds.
§Examples
use easytime::Duration;
let duration = Duration::from_micros(1_000_002);
assert_eq!(Some(1), duration.as_secs());
assert_eq!(Some(2000), duration.subsec_nanos());
sourcepub const fn from_nanos(nanos: u64) -> Self
pub const fn from_nanos(nanos: u64) -> Self
Creates a new Duration
from the specified number of nanoseconds.
§Examples
use easytime::Duration;
let duration = Duration::from_nanos(1_000_000_123);
assert_eq!(Some(1), duration.as_secs());
assert_eq!(Some(123), duration.subsec_nanos());
sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if this Duration
spans no time.
§Examples
use easytime::Duration;
assert!(Duration::ZERO.is_zero());
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());
sourcepub const fn as_secs(&self) -> Option<u64>
pub const fn as_secs(&self) -> Option<u64>
Returns the number of whole seconds contained by this Duration
.
The returned value does not include the fractional (nanosecond) part of the
duration, which can be obtained using subsec_nanos
.
§Examples
use easytime::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_secs(), Some(5));
sourcepub const fn subsec_millis(&self) -> Option<u32>
pub const fn subsec_millis(&self) -> Option<u32>
Returns the fractional part of this Duration
, in whole milliseconds.
This method does not return the length of the duration when represented by milliseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one thousand).
§Examples
use easytime::Duration;
let duration = Duration::from_millis(5_432);
assert_eq!(duration.as_secs(), Some(5));
assert_eq!(duration.subsec_millis(), Some(432));
sourcepub const fn subsec_micros(&self) -> Option<u32>
pub const fn subsec_micros(&self) -> Option<u32>
Returns the fractional part of this Duration
, in whole microseconds.
This method does not return the length of the duration when represented by microseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one million).
§Examples
use easytime::Duration;
let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), Some(1));
assert_eq!(duration.subsec_micros(), Some(234_567));
sourcepub const fn subsec_nanos(&self) -> Option<u32>
pub const fn subsec_nanos(&self) -> Option<u32>
Returns the fractional part of this Duration
, in nanoseconds.
This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one billion).
§Examples
use easytime::Duration;
let duration = Duration::from_millis(5_010);
assert_eq!(duration.as_secs(), Some(5));
assert_eq!(duration.subsec_nanos(), Some(10_000_000));
sourcepub const fn as_millis(&self) -> Option<u128>
pub const fn as_millis(&self) -> Option<u128>
Returns the total number of whole milliseconds contained by this Duration
.
§Examples
use easytime::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_millis(), Some(5_730));
sourcepub const fn as_micros(&self) -> Option<u128>
pub const fn as_micros(&self) -> Option<u128>
Returns the total number of whole microseconds contained by this Duration
.
§Examples
use easytime::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_micros(), Some(5_730_023));
sourcepub const fn as_nanos(&self) -> Option<u128>
pub const fn as_nanos(&self) -> Option<u128>
Returns the total number of nanoseconds contained by this Duration
.
§Examples
use easytime::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_nanos(), Some(5_730_023_852));
sourcepub fn as_secs_f64(&self) -> Option<f64>
pub fn as_secs_f64(&self) -> Option<f64>
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 easytime::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f64(), Some(2.7));
sourcepub fn as_secs_f32(&self) -> Option<f32>
pub fn as_secs_f32(&self) -> Option<f32>
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 easytime::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f32(), Some(2.7));
sourcepub fn from_secs_f64(secs: f64) -> Self
pub fn from_secs_f64(secs: f64) -> Self
Creates a new Duration
from the specified number of seconds represented
as f64
.
§Examples
use easytime::Duration;
let dur = Duration::from_secs_f64(2.7);
assert_eq!(dur, Duration::new(2, 700_000_000));
sourcepub fn from_secs_f32(secs: f32) -> Duration
pub fn from_secs_f32(secs: f32) -> Duration
Creates a new Duration
from the specified number of seconds represented
as f32
.
§Examples
use easytime::Duration;
let dur = Duration::from_secs_f32(2.7);
assert_eq!(dur, Duration::new(2, 700_000_000));
sourcepub fn mul_f64(self, rhs: f64) -> Duration
pub fn mul_f64(self, rhs: f64) -> Duration
Multiplies Duration
by f64
.
§Examples
use easytime::Duration;
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));
sourcepub fn mul_f32(self, rhs: f32) -> Duration
pub fn mul_f32(self, rhs: f32) -> Duration
Multiplies Duration
by f32
.
§Examples
use easytime::Duration;
let dur = Duration::new(2, 700_000_000);
// note that due to rounding errors result is slightly different
// from 8.478 and 847800.0
assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_640));
assert_eq!(dur.mul_f32(3.14e5), Duration::new(847799, 969_120_256));
sourcepub fn div_f64(self, rhs: f64) -> Duration
pub fn div_f64(self, rhs: f64) -> Duration
Divide Duration
by f64
.
§Examples
use easytime::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
// note that truncation is used, not rounding
assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));
sourcepub fn div_f32(self, rhs: f32) -> Duration
pub fn div_f32(self, rhs: f32) -> Duration
Divide Duration
by f32
.
§Examples
use easytime::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
// note that truncation is used, not rounding
assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));
sourcepub const fn is_some(&self) -> bool
pub const fn is_some(&self) -> bool
Returns true
if into_inner
returns Some
.
§Examples
use easytime::Duration;
let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert!((one_sec - zero).is_some());
assert!(!(zero - one_sec).is_some());
sourcepub const fn is_none(&self) -> bool
pub const fn is_none(&self) -> bool
Returns true
if into_inner
returns None
.
§Examples
use easytime::Duration;
let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert!(!(one_sec - zero).is_none());
assert!((zero - one_sec).is_none());
sourcepub const fn into_inner(self) -> Option<Duration>
pub const fn into_inner(self) -> Option<Duration>
Returns the contained std::time::Duration
or None
.
§Examples
use easytime::Duration;
let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert_eq!((one_sec - zero).into_inner(), Some(std::time::Duration::from_secs(1)));
assert_eq!((zero - one_sec).into_inner(), None);
sourcepub const fn unwrap_or(self, default: Duration) -> Duration
pub const fn unwrap_or(self, default: Duration) -> Duration
Returns the contained std::time::Duration
or a default.
dur.unwrap_or(default)
is equivalent to dur.into_inner().unwrap_or(default)
.
§Examples
use easytime::Duration;
let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert_eq!(
(one_sec - zero).unwrap_or(std::time::Duration::from_secs(2)),
std::time::Duration::from_secs(1)
);
assert_eq!(
(zero - one_sec).unwrap_or(std::time::Duration::from_secs(2)),
std::time::Duration::from_secs(2)
);
sourcepub fn unwrap_or_else<F>(self, default: F) -> Duration
pub fn unwrap_or_else<F>(self, default: F) -> Duration
Returns the contained std::time::Duration
or computes it from a closure.
dur.unwrap_or_else(default)
is equivalent to dur.into_inner().unwrap_or_else(default)
.
§Examples
use easytime::Duration;
let zero = Duration::new(0, 0);
let one_sec = Duration::new(1, 0);
assert_eq!(
(one_sec - zero).unwrap_or_else(|| std::time::Duration::from_secs(2)),
std::time::Duration::from_secs(1)
);
assert_eq!(
(zero - one_sec).unwrap_or_else(|| std::time::Duration::from_secs(2)),
std::time::Duration::from_secs(2)
);
Trait Implementations§
source§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)
+=
operation. Read moresource§impl AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
source§fn add_assign(&mut self, other: Duration)
fn add_assign(&mut self, other: 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 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 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 Ord for Duration
impl Ord for Duration
source§impl PartialEq<Duration> for Duration
impl PartialEq<Duration> for Duration
source§impl PartialEq<Duration> for Duration
impl PartialEq<Duration> for Duration
source§impl PartialEq for Duration
impl PartialEq for Duration
source§impl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd for Duration
impl PartialOrd for Duration
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. 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)
-=
operation. Read moresource§impl SubAssign<Duration> for Instant
impl SubAssign<Duration> for Instant
source§fn sub_assign(&mut self, other: Duration)
fn sub_assign(&mut self, other: 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