[][src]Trait time::ext::NumericalDuration

pub trait NumericalDuration {
    pub fn nanoseconds(self) -> Duration;
pub fn microseconds(self) -> Duration;
pub fn milliseconds(self) -> Duration;
pub fn seconds(self) -> Duration;
pub fn minutes(self) -> Duration;
pub fn hours(self) -> Duration;
pub fn days(self) -> Duration;
pub fn weeks(self) -> Duration; }

Create Durations from primitive and core numeric types.

This trait can be imported with use time::prelude::*.

Due to limitations in rustc, these methods are currently not const fn. See RFC 2632 for details.

Examples

Basic construction of Durations.

assert_eq!(5.nanoseconds(), Duration::nanoseconds(5));
assert_eq!(5.microseconds(), Duration::microseconds(5));
assert_eq!(5.milliseconds(), Duration::milliseconds(5));
assert_eq!(5.seconds(), Duration::seconds(5));
assert_eq!(5.minutes(), Duration::minutes(5));
assert_eq!(5.hours(), Duration::hours(5));
assert_eq!(5.days(), Duration::days(5));
assert_eq!(5.weeks(), Duration::weeks(5));

Signed integers work as well!

assert_eq!((-5).nanoseconds(), Duration::nanoseconds(-5));
assert_eq!((-5).microseconds(), Duration::microseconds(-5));
assert_eq!((-5).milliseconds(), Duration::milliseconds(-5));
assert_eq!((-5).seconds(), Duration::seconds(-5));
assert_eq!((-5).minutes(), Duration::minutes(-5));
assert_eq!((-5).hours(), Duration::hours(-5));
assert_eq!((-5).days(), Duration::days(-5));
assert_eq!((-5).weeks(), Duration::weeks(-5));

Just like any other Duration, they can be added, subtracted, etc.

assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());

When called on floating point values, any remainder of the floating point value will be truncated. Keep in mind that floating point numbers are inherently imprecise and have limited capacity.

Required methods

pub fn nanoseconds(self) -> Duration[src]

Create a Duration from the number of nanoseconds.

pub fn microseconds(self) -> Duration[src]

Create a Duration from the number of microseconds.

pub fn milliseconds(self) -> Duration[src]

Create a Duration from the number of milliseconds.

pub fn seconds(self) -> Duration[src]

Create a Duration from the number of seconds.

pub fn minutes(self) -> Duration[src]

Create a Duration from the number of minutes.

pub fn hours(self) -> Duration[src]

Create a Duration from the number of hours.

pub fn days(self) -> Duration[src]

Create a Duration from the number of days.

pub fn weeks(self) -> Duration[src]

Create a Duration from the number of weeks.

Loading content...

Implementations on Foreign Types

impl NumericalDuration for u8[src]

impl NumericalDuration for u16[src]

impl NumericalDuration for u32[src]

impl NumericalDuration for i8[src]

impl NumericalDuration for i16[src]

impl NumericalDuration for i32[src]

impl NumericalDuration for i64[src]

impl NumericalDuration for NonZeroU8[src]

impl NumericalDuration for NonZeroU16[src]

impl NumericalDuration for NonZeroU32[src]

impl NumericalDuration for NonZeroI8[src]

impl NumericalDuration for NonZeroI16[src]

impl NumericalDuration for NonZeroI32[src]

impl NumericalDuration for NonZeroI64[src]

impl NumericalDuration for f32[src]

impl NumericalDuration for f64[src]

Loading content...

Implementors

Loading content...