Duration

Struct Duration 

Source
pub struct Duration { /* private fields */ }
Expand description

A Duration represents the difference between two time points. It has an associated Representation, which determines how the count of elapsed ticks is stored. The Period determines the integer (!) ratio of each tick to seconds. This may be used to convert between Durations of differing time units.

The accuracy of a Duration is one attosecond. This makes for a representable range of about 10 trillion years, or about 700 times the age of the universe; should be sufficient for most purposes. Note that this type is explicitly intended for calculations only: when storing large numbers of durations, it might be more efficient to use a more tailor-made representation.

Implementations§

Source§

impl Duration

Source

pub const fn attoseconds(count: i128) -> Self

Constructs a new Duration from a given number of attoseconds.

Source

pub const fn femtoseconds(count: i128) -> Self

Constructs a new Duration from a given number of femtoseconds.

Source

pub const fn picoseconds(count: i128) -> Self

Constructs a new Duration from a given number of picoseconds.

Source

pub const fn nanoseconds(count: i128) -> Self

Constructs a new Duration from a given number of nanoseconds.

Source

pub const fn microseconds(count: i128) -> Self

Constructs a new Duration from a given number of microseconds.

Source

pub const fn milliseconds(count: i128) -> Self

Constructs a new Duration from a given number of milliseconds.

Source

pub const fn seconds(count: i128) -> Self

Constructs a new Duration from a given number of seconds.

Source

pub const fn minutes(count: i128) -> Self

Constructs a new Duration from a given number of minutes.

Source

pub const fn hours(count: i128) -> Self

Constructs a new Duration from a given number of hours.

Source

pub const fn days(count: i128) -> Self

Constructs a new Duration from a given number of days.

Source

pub const fn weeks(count: i128) -> Self

Constructs a new Duration from a given number of weeks.

Source

pub const fn months(count: i128) -> Self

Constructs a new Duration from a given number of months. Expresses a month as 1/12 of an average Gregorian year.

Source

pub const fn years(count: i128) -> Self

Constructs a new Duration from a given number of years. Uses an average Gregorian year as duration.

Source

pub const fn count(&self) -> i128

Returns the raw number of time units contained in this Duration. It is advised not to use this function unless absolutely necessary, as it effectively throws away all time unit information and safety.

Source

pub fn fractional_digits( &self, precision: Option<usize>, base: u8, ) -> impl Iterator<Item = u8>

Returns an iterator over the fractional (sub-unit) digits of this duration. Useful as helper function when printing durations.

Source

pub fn decimal_digits( &self, precision: Option<usize>, ) -> impl Iterator<Item = u8>

Returns an iterator over the fractional (sub-unit) digits of this duration, expressed in decimal. Useful as helper function when printing durations.

Source

pub const fn round<Target>(self) -> Duration
where Target: UnitRatio + ?Sized,

Converts towards a different time unit, rounding towards the nearest whole unit.

Source

pub fn ceil<Target>(self) -> Duration
where Target: UnitRatio + ?Sized,

Converts towards a different time unit, rounding towards positive infinity if the unit is not entirely commensurate with the present unit.

Source

pub fn floor<Target>(self) -> Duration
where Target: UnitRatio + ?Sized,

Converts towards a different time unit, rounding towards negative infinity if the unit is not entirely commensurate with the present unit.

Source

pub const fn truncate<Target>(self) -> Duration
where Target: UnitRatio + ?Sized,

Converts towards a different time unit, rounding towards zero if the unit is not entirely commensurate with the present unit.

Source

pub fn factor_out<Unit>(self) -> (i128, Duration)
where Unit: UnitRatio + ?Sized,

Segments this Duration by factoring out the largest possible number of whole multiples of a given unit. Returns this whole number as well as the remainder.

An example would be factoring out the number of whole days from some elapsed time: then, self.factor_out() would return a tuple of the number of whole days and the fractional day part that remains.

Source

pub const fn div_round(self, other: i128) -> Self

Divides by an i128, rounding to the nearest result.

Source§

impl Duration

Source

pub fn abs(&self) -> Self

Source

pub fn abs_sub(&self, other: &Self) -> Self

Source

pub fn signum(&self) -> Self

Source

pub fn is_positive(&self) -> bool

Source

pub fn is_negative(&self) -> bool

Trait Implementations§

Source§

impl<Scale> Add<Duration> for TimePoint<Scale>
where Scale: ?Sized,

Source§

type Output = TimePoint<Scale>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Duration) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Duration

Source§

type Output = Duration

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Duration) -> Duration

Performs the + operation. Read more
Source§

impl<Scale> AddAssign<Duration> for TimePoint<Scale>
where Scale: ?Sized,

Source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
Source§

impl AddAssign for Duration

Source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
Source§

impl Bounded for Duration

Source§

fn min_value() -> Self

Returns the Duration value that is nearest to negative infinity.

Source§

fn max_value() -> Self

Returns the Duration value that is nearest to positive infinity.

Source§

impl Clone for Duration

Source§

fn clone(&self) -> Duration

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ConstZero for Duration

Source§

const ZERO: Self

The additive identity element of Self, 0.
Source§

impl Debug for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Duration
where Self: FromStr, <Self as FromStr>::Err: Display,

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div<T> for Duration
where T: Into<i128>,

Source§

fn div(self, rhs: T) -> Self::Output

A Duration may may be divided by unitless numbers to obtain a new Duration.

Source§

type Output = Duration

The resulting type after applying the / operator.
Source§

impl Div for Duration

Source§

type Output = i128

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Duration) -> Self::Output

Performs the / operation. Read more
Source§

impl From<Days> for Duration

Source§

fn from(value: Days) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Duration

Source§

fn from_str(string: &str) -> Result<Self, Self::Err>

Parses a Duration type based on some ISO 8601 duration string. However, we additionally impose that months may not be used as duration, to prevent confusion with minutes (and because their precise duration cannot be unambiguously defined). Furthermore, we do not support use of the time designator (‘T’) inside duration expressions. Finally, we support years, days, hours, minutes, and seconds with any number of digits.

For years, following the rest of this library, a duration of 31556952 seconds is used, which corresponds with the exact average duration of a Gregorian year.

Source§

type Err = DurationParsingError

The associated error which can be returned from parsing.
Source§

impl Hash for Duration

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<T> Mul<T> for Duration
where T: Into<i128>,

Source§

fn mul(self, rhs: T) -> Self::Output

A Duration may not be multiplied with another Duration (as that is undefined), but it may be multiplied with unitless numbers.

Source§

type Output = Duration

The resulting type after applying the * operator.
Source§

impl Neg for Duration

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn neg(self) -> Duration

Performs the unary - operation. Read more
Source§

impl Ord for Duration

Source§

fn cmp(&self, other: &Duration) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Duration

Source§

fn eq(&self, other: &Duration) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Duration

Source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Duration

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<Scale> Sub<Duration> for TimePoint<Scale>
where Scale: ?Sized,

Source§

type Output = TimePoint<Scale>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Duration) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Duration

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Duration) -> Duration

Performs the - operation. Read more
Source§

impl<Scale> SubAssign<Duration> for TimePoint<Scale>
where Scale: ?Sized,

Source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
Source§

impl SubAssign for Duration

Source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
Source§

impl Zero for Duration

Source§

fn zero() -> Self

Returns a Duration value that represents no time passed.

Source§

fn is_zero(&self) -> bool

Whether this Duration has any duration.

Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl Copy for Duration

Source§

impl Eq for Duration

Source§

impl StructuralPartialEq for Duration

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> LowerBounded for T
where T: Bounded,

Source§

fn min_value() -> T

Returns the smallest finite number this type can represent
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UpperBounded for T
where T: Bounded,

Source§

fn max_value() -> T

Returns the largest finite number this type can represent
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,