logo
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

Equivalent to 0.seconds().

assert_eq!(Duration::ZERO, 0.seconds());
Run

Equivalent to 1.nanoseconds().

assert_eq!(Duration::NANOSECOND, 1.nanoseconds());
Run

Equivalent to 1.microseconds().

assert_eq!(Duration::MICROSECOND, 1.microseconds());
Run

Equivalent to 1.milliseconds().

assert_eq!(Duration::MILLISECOND, 1.milliseconds());
Run

Equivalent to 1.seconds().

assert_eq!(Duration::SECOND, 1.seconds());
Run

Equivalent to 1.minutes().

assert_eq!(Duration::MINUTE, 1.minutes());
Run

Equivalent to 1.hours().

assert_eq!(Duration::HOUR, 1.hours());
Run

Equivalent to 1.days().

assert_eq!(Duration::DAY, 1.days());
Run

Equivalent to 1.weeks().

assert_eq!(Duration::WEEK, 1.weeks());
Run

The minimum possible duration. Adding any negative duration to this will cause an overflow.

The maximum possible duration. Adding any positive duration to this will cause an overflow.

Check if a duration is exactly zero.

assert!(0.seconds().is_zero());
assert!(!1.nanoseconds().is_zero());
Run

Check if a duration is negative.

assert!((-1).seconds().is_negative());
assert!(!0.seconds().is_negative());
assert!(!1.seconds().is_negative());
Run

Check if a duration is positive.

assert!(1.seconds().is_positive());
assert!(!0.seconds().is_positive());
assert!(!(-1).seconds().is_positive());
Run

Get the absolute value of the duration.

This method saturates the returned value if it would otherwise overflow.

assert_eq!(1.seconds().abs(), 1.seconds());
assert_eq!(0.seconds().abs(), 0.seconds());
assert_eq!((-1).seconds().abs(), 1.seconds());
Run

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());
Run

Create a new Duration with the given number of weeks. Equivalent to Duration::seconds(weeks * 604_800).

assert_eq!(Duration::weeks(1), 604_800.seconds());
Run

Create a new Duration with the given number of days. Equivalent to Duration::seconds(days * 86_400).

assert_eq!(Duration::days(1), 86_400.seconds());
Run

Create a new Duration with the given number of hours. Equivalent to Duration::seconds(hours * 3_600).

assert_eq!(Duration::hours(1), 3_600.seconds());
Run

Create a new Duration with the given number of minutes. Equivalent to Duration::seconds(minutes * 60).

assert_eq!(Duration::minutes(1), 60.seconds());
Run

Create a new Duration with the given number of seconds.

assert_eq!(Duration::seconds(1), 1_000.milliseconds());
Run

Creates a new Duration from the specified number of seconds represented as f64.

assert_eq!(Duration::seconds_f64(0.5), 0.5.seconds());
assert_eq!(Duration::seconds_f64(-0.5), -0.5.seconds());
Run

Creates a new Duration from the specified number of seconds represented as f32.

assert_eq!(Duration::seconds_f32(0.5), 0.5.seconds());
assert_eq!(Duration::seconds_f32(-0.5), (-0.5).seconds());
Run

Create a new Duration with the given number of milliseconds.

assert_eq!(Duration::milliseconds(1), 1_000.microseconds());
assert_eq!(Duration::milliseconds(-1), (-1_000).microseconds());
Run

Create a new Duration with the given number of microseconds.

assert_eq!(Duration::microseconds(1), 1_000.nanoseconds());
assert_eq!(Duration::microseconds(-1), (-1_000).nanoseconds());
Run

Create a new Duration with the given number of nanoseconds.

assert_eq!(Duration::nanoseconds(1), 1.microseconds() / 1_000);
assert_eq!(Duration::nanoseconds(-1), (-1).microseconds() / 1_000);
Run

Get the number of whole weeks in the duration.

assert_eq!(1.weeks().whole_weeks(), 1);
assert_eq!((-1).weeks().whole_weeks(), -1);
assert_eq!(6.days().whole_weeks(), 0);
assert_eq!((-6).days().whole_weeks(), 0);
Run

Get the number of whole days in the duration.

assert_eq!(1.days().whole_days(), 1);
assert_eq!((-1).days().whole_days(), -1);
assert_eq!(23.hours().whole_days(), 0);
assert_eq!((-23).hours().whole_days(), 0);
Run

Get the number of whole hours in the duration.

assert_eq!(1.hours().whole_hours(), 1);
assert_eq!((-1).hours().whole_hours(), -1);
assert_eq!(59.minutes().whole_hours(), 0);
assert_eq!((-59).minutes().whole_hours(), 0);
Run

Get the number of whole minutes in the duration.

assert_eq!(1.minutes().whole_minutes(), 1);
assert_eq!((-1).minutes().whole_minutes(), -1);
assert_eq!(59.seconds().whole_minutes(), 0);
assert_eq!((-59).seconds().whole_minutes(), 0);
Run

Get the number of whole seconds in the duration.

assert_eq!(1.seconds().whole_seconds(), 1);
assert_eq!((-1).seconds().whole_seconds(), -1);
assert_eq!(1.minutes().whole_seconds(), 60);
assert_eq!((-1).minutes().whole_seconds(), -60);
Run

Get the number of fractional seconds in the duration.

assert_eq!(1.5.seconds().as_seconds_f64(), 1.5);
assert_eq!((-1.5).seconds().as_seconds_f64(), -1.5);
Run

Get the number of fractional seconds in the duration.

assert_eq!(1.5.seconds().as_seconds_f32(), 1.5);
assert_eq!((-1.5).seconds().as_seconds_f32(), -1.5);
Run

Get the number of whole milliseconds in the duration.

assert_eq!(1.seconds().whole_milliseconds(), 1_000);
assert_eq!((-1).seconds().whole_milliseconds(), -1_000);
assert_eq!(1.milliseconds().whole_milliseconds(), 1);
assert_eq!((-1).milliseconds().whole_milliseconds(), -1);
Run

Get the number of milliseconds past the number of whole seconds.

Always in the range -1_000..1_000.

assert_eq!(1.4.seconds().subsec_milliseconds(), 400);
assert_eq!((-1.4).seconds().subsec_milliseconds(), -400);
Run

Get the number of whole microseconds in the duration.

assert_eq!(1.milliseconds().whole_microseconds(), 1_000);
assert_eq!((-1).milliseconds().whole_microseconds(), -1_000);
assert_eq!(1.microseconds().whole_microseconds(), 1);
assert_eq!((-1).microseconds().whole_microseconds(), -1);
Run

Get the number of microseconds past the number of whole seconds.

Always in the range -1_000_000..1_000_000.

assert_eq!(1.0004.seconds().subsec_microseconds(), 400);
assert_eq!((-1.0004).seconds().subsec_microseconds(), -400);
Run

Get the number of nanoseconds in the duration.

assert_eq!(1.microseconds().whole_nanoseconds(), 1_000);
assert_eq!((-1).microseconds().whole_nanoseconds(), -1_000);
assert_eq!(1.nanoseconds().whole_nanoseconds(), 1);
assert_eq!((-1).nanoseconds().whole_nanoseconds(), -1);
Run

Get the number of nanoseconds past the number of whole seconds.

The returned value will always be in the range -1_000_000_000..1_000_000_000.

assert_eq!(1.000_000_400.seconds().subsec_nanoseconds(), 400);
assert_eq!((-1.000_000_400).seconds().subsec_nanoseconds(), -400);
Run

Computes self + rhs, returning None if an overflow occurred.

assert_eq!(5.seconds().checked_add(5.seconds()), Some(10.seconds()));
assert_eq!(Duration::MAX.checked_add(1.nanoseconds()), None);
assert_eq!((-5).seconds().checked_add(5.seconds()), Some(0.seconds()));
Run

Computes self - rhs, returning None if an overflow occurred.

assert_eq!(5.seconds().checked_sub(5.seconds()), Some(Duration::ZERO));
assert_eq!(Duration::MIN.checked_sub(1.nanoseconds()), None);
assert_eq!(5.seconds().checked_sub(10.seconds()), Some((-5).seconds()));
Run

Computes self * rhs, returning None if an overflow occurred.

assert_eq!(5.seconds().checked_mul(2), Some(10.seconds()));
assert_eq!(5.seconds().checked_mul(-2), Some((-10).seconds()));
assert_eq!(5.seconds().checked_mul(0), Some(0.seconds()));
assert_eq!(Duration::MAX.checked_mul(2), None);
assert_eq!(Duration::MIN.checked_mul(2), None);
Run

Computes self / rhs, returning None if rhs == 0 or if the result would overflow.

assert_eq!(10.seconds().checked_div(2), Some(5.seconds()));
assert_eq!(10.seconds().checked_div(-2), Some((-5).seconds()));
assert_eq!(1.seconds().checked_div(0), None);
Run

Computes self + rhs, saturating if an overflow occurred.

assert_eq!(5.seconds().saturating_add(5.seconds()), 10.seconds());
assert_eq!(Duration::MAX.saturating_add(1.nanoseconds()), Duration::MAX);
assert_eq!(
    Duration::MIN.saturating_add((-1).nanoseconds()),
    Duration::MIN
);
assert_eq!((-5).seconds().saturating_add(5.seconds()), Duration::ZERO);
Run

Computes self - rhs, saturating if an overflow occurred.

assert_eq!(5.seconds().saturating_sub(5.seconds()), Duration::ZERO);
assert_eq!(Duration::MIN.saturating_sub(1.nanoseconds()), Duration::MIN);
assert_eq!(
    Duration::MAX.saturating_sub((-1).nanoseconds()),
    Duration::MAX
);
assert_eq!(5.seconds().saturating_sub(10.seconds()), (-5).seconds());
Run

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);
Run

Runs a closure, returning the duration of time it took to run. The return value of the closure is provided in the second part of the tuple.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Add the sub-day time of the Duration to the Time. Wraps on overflow.

assert_eq!(time!(12:00) + 2.hours(), time!(14:00));
assert_eq!(time!(0:00:01) + (-2).seconds(), time!(23:59:59));
Run

The resulting type after applying the + operator.

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The format returned by this implementation is not stable and must not be relied upon.

For the purposes of this implementation, a day is exactly 24 hours and a minute is exactly 60 seconds.

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Subtract the sub-day time of the Duration from the Time. Wraps on overflow.

assert_eq!(time!(14:00) - 2.hours(), time!(12:00));
assert_eq!(time!(23:59:59) - (-2).seconds(), time!(0:00:01));
Run

The resulting type after applying the - operator.

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.