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
impl Duration
Sourcepub const fn attoseconds(count: i128) -> Self
pub const fn attoseconds(count: i128) -> Self
Constructs a new Duration from a given number of attoseconds.
Sourcepub const fn femtoseconds(count: i128) -> Self
pub const fn femtoseconds(count: i128) -> Self
Constructs a new Duration from a given number of femtoseconds.
Sourcepub const fn picoseconds(count: i128) -> Self
pub const fn picoseconds(count: i128) -> Self
Constructs a new Duration from a given number of picoseconds.
Sourcepub const fn nanoseconds(count: i128) -> Self
pub const fn nanoseconds(count: i128) -> Self
Constructs a new Duration from a given number of nanoseconds.
Sourcepub const fn microseconds(count: i128) -> Self
pub const fn microseconds(count: i128) -> Self
Constructs a new Duration from a given number of microseconds.
Sourcepub const fn milliseconds(count: i128) -> Self
pub const fn milliseconds(count: i128) -> Self
Constructs a new Duration from a given number of milliseconds.
Sourcepub const fn seconds(count: i128) -> Self
pub const fn seconds(count: i128) -> Self
Constructs a new Duration from a given number of seconds.
Sourcepub const fn minutes(count: i128) -> Self
pub const fn minutes(count: i128) -> Self
Constructs a new Duration from a given number of minutes.
Sourcepub const fn hours(count: i128) -> Self
pub const fn hours(count: i128) -> Self
Constructs a new Duration from a given number of hours.
Sourcepub const fn weeks(count: i128) -> Self
pub const fn weeks(count: i128) -> Self
Constructs a new Duration from a given number of weeks.
Sourcepub const fn months(count: i128) -> Self
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.
Sourcepub const fn years(count: i128) -> Self
pub const fn years(count: i128) -> Self
Constructs a new Duration from a given number of years. Uses an average Gregorian year as
duration.
Sourcepub const fn count(&self) -> i128
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.
Sourcepub fn fractional_digits(
&self,
precision: Option<usize>,
base: u8,
) -> impl Iterator<Item = u8>
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.
Sourcepub fn decimal_digits(
&self,
precision: Option<usize>,
) -> impl Iterator<Item = u8>
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.
Sourcepub const fn round<Target>(self) -> Duration
pub const fn round<Target>(self) -> Duration
Converts towards a different time unit, rounding towards the nearest whole unit.
Sourcepub fn ceil<Target>(self) -> Duration
pub fn ceil<Target>(self) -> Duration
Converts towards a different time unit, rounding towards positive infinity if the unit is not entirely commensurate with the present unit.
Sourcepub fn floor<Target>(self) -> Duration
pub fn floor<Target>(self) -> Duration
Converts towards a different time unit, rounding towards negative infinity if the unit is not entirely commensurate with the present unit.
Sourcepub const fn truncate<Target>(self) -> Duration
pub const fn truncate<Target>(self) -> Duration
Converts towards a different time unit, rounding towards zero if the unit is not entirely commensurate with the present unit.
Sourcepub fn factor_out<Unit>(self) -> (i128, Duration)
pub fn factor_out<Unit>(self) -> (i128, Duration)
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.
Trait Implementations§
Source§impl<Scale> AddAssign<Duration> for TimePoint<Scale>where
Scale: ?Sized,
impl<Scale> AddAssign<Duration> for TimePoint<Scale>where
Scale: ?Sized,
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read moreSource§impl AddAssign for Duration
impl AddAssign for Duration
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for Duration
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Duration
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl FromStr for Duration
impl FromStr for Duration
Source§fn from_str(string: &str) -> Result<Self, Self::Err>
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
type Err = DurationParsingError
Source§impl Ord for Duration
impl Ord for Duration
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§impl<Scale> SubAssign<Duration> for TimePoint<Scale>where
Scale: ?Sized,
impl<Scale> SubAssign<Duration> for TimePoint<Scale>where
Scale: ?Sized,
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-= operation. Read moreSource§impl SubAssign for Duration
impl SubAssign for Duration
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-= operation. Read more