[−][src]Trait time::NumericalDuration
Create Duration
s 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 Duration
s.
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());
As floats can not be used to construct via anything other than
Duration::seconds_f32
and Duration::seconds_f64
, floats do not
implement NumericalDuration
.
5.0.seconds();
Required methods
fn nanoseconds(self) -> Duration
Create a Duration
from the number of nanoseconds.
fn microseconds(self) -> Duration
Create a Duration
from the number of microseconds.
fn milliseconds(self) -> Duration
Create a Duration
from the number of milliseconds.
fn seconds(self) -> Duration
Create a Duration
from the number of seconds.
fn minutes(self) -> Duration
Create a Duration
from the number of minutes.
fn hours(self) -> Duration
Create a Duration
from the number of hours.
fn days(self) -> Duration
Create a Duration
from the number of days.
fn weeks(self) -> Duration
Create a Duration
from the number of weeks.