time::ext

Trait NumericalDuration

Source
pub trait NumericalDuration: Sealed {
    // Required methods
    fn nanoseconds(self) -> Duration;
    fn microseconds(self) -> Duration;
    fn milliseconds(self) -> Duration;
    fn seconds(self) -> Duration;
    fn minutes(self) -> Duration;
    fn hours(self) -> Duration;
    fn days(self) -> Duration;
    fn weeks(self) -> Duration;
}
Expand description

Create Durations from numeric literals.

§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§

Source

fn nanoseconds(self) -> Duration

Create a Duration from the number of nanoseconds.

Source

fn microseconds(self) -> Duration

Create a Duration from the number of microseconds.

Source

fn milliseconds(self) -> Duration

Create a Duration from the number of milliseconds.

Source

fn seconds(self) -> Duration

Create a Duration from the number of seconds.

Source

fn minutes(self) -> Duration

Create a Duration from the number of minutes.

Source

fn hours(self) -> Duration

Create a Duration from the number of hours.

Source

fn days(self) -> Duration

Create a Duration from the number of days.

Source

fn weeks(self) -> Duration

Create a Duration from the number of weeks.

Implementations on Foreign Types§

Source§

impl NumericalDuration for f64

Source§

impl NumericalDuration for i64

Implementors§