pub struct DurationBreakdown { /* private fields */ }Expand description
A DurationBreakdown represents a duration of time that has been
broken up into several units (i.e. weeks, days, etc) in such a way
that the sum of each unit comprises the whole duration of time.
Implementations§
Source§impl DurationBreakdown
impl DurationBreakdown
Sourcepub fn from_parts(
weeks: u64,
days: u64,
hours: u64,
minutes: u64,
seconds: u64,
nanoseconds: u64,
) -> Self
pub fn from_parts( weeks: u64, days: u64, hours: u64, minutes: u64, seconds: u64, nanoseconds: u64, ) -> Self
Constructs a DurationBreakdown directly from the given component parts.
§Examples
let breakdown = DurationBreakdown::from_parts(
4, // weeks
2, // days
17, // hours
41, // minutes
18, // seconds
100, // nanoseconds
);
assert_eq!(breakdown.weeks(), 4);
assert_eq!(breakdown.days(), 2);
assert_eq!(breakdown.hours(), 17);
assert_eq!(breakdown.minutes(), 41);
assert_eq!(breakdown.seconds(), 18);
assert_eq!(breakdown.nanoseconds(), 100);Sourcepub fn with_precision(&self, precision: Precision) -> Self
pub fn with_precision(&self, precision: Precision) -> Self
Creates a copy of the given DurationBreakdown with a given precision.
Specifying a precision allows you to discard the pieces of the breakdown
which are below a certain granularity.
All units below the given precision are set to 0 in the breakdown (not rounded).
§Examples
let breakdown = DurationBreakdown::from_parts(14, 2, 16, 25, 55, 400);
assert_eq!(
breakdown.with_precision(Precision::Hours),
DurationBreakdown::from_parts(14, 2, 16, 0, 0, 0)
);Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Converts a DurationBreakdown into a standard form in which the value
of a given time component (week, day, etc) is no greater than the value
of a single unit of the time component one level up. For instance,
a DurationBreakdown with 68 as its minutes value and 3 as its
hours value would be normalized to 8 minutes and 4 hours.
§Examples
// 9 days, 1 hour, 50 minutes, 70 seconds (not normalized)
let mut breakdown = DurationBreakdown::from_parts(0, 9, 1, 50, 70, 0);
breakdown.normalize();
assert_eq!(
breakdown.as_string(),
"1 week, 2 days, 1 hour, 51 minutes, 10 seconds, and 0 nanoseconds");Sourcepub fn nanoseconds(&self) -> u64
pub fn nanoseconds(&self) -> u64
Gets the number of nanoseconds in the breakdown.
Sourcepub fn weeks_as_string(&self) -> String
pub fn weeks_as_string(&self) -> String
A string describing the number of weeks in the breakdown. E.g. "14 weeks".
Sourcepub fn days_as_string(&self) -> String
pub fn days_as_string(&self) -> String
A string describing the number of days in the breakdown. E.g. "6 days".
Sourcepub fn hours_as_string(&self) -> String
pub fn hours_as_string(&self) -> String
A string describing the number of hours in the breakdown. E.g. "1 hour".
Sourcepub fn minutes_as_string(&self) -> String
pub fn minutes_as_string(&self) -> String
A string describing the number of minutes in the breakdown. E.g. "53 minutes".
Sourcepub fn seconds_as_string(&self) -> String
pub fn seconds_as_string(&self) -> String
A string describing the number of seconds in the breakdown. E.g. "40 seconds".
Sourcepub fn nanoseconds_as_string(&self) -> String
pub fn nanoseconds_as_string(&self) -> String
A string describing the number of nanoseconds in the breakdown. E.g. "1700 nanoseconds".
Sourcepub fn as_string(&self) -> String
pub fn as_string(&self) -> String
A string describing the entire DurationBreakdown. All components
are included, even if their value is 0. See as_string_hide_zeros
for an alternate display of the breakdown.
Note that this function is used by the implementation of Display for
DurationBreakdown.
§Examples
let breakdown = DurationBreakdown::from_parts(0, 4, 0, 10, 48, 200);
assert_eq!(
breakdown.as_string(),
"0 weeks, 4 days, 0 hours, 10 minutes, 48 seconds, and 200 nanoseconds");Sourcepub fn as_string_hide_zeros(&self) -> String
pub fn as_string_hide_zeros(&self) -> String
A string describing the entire DurationBreakdown, but any components
that have a value of 0 are omitted from the description. See
as_string for a version of this function that includes 0-valued
components.
§Examples
let breakdown = DurationBreakdown::from_parts(0, 4, 0, 10, 48, 200);
assert_eq!(
breakdown.as_string_hide_zeros(),
"4 days, 10 minutes, 48 seconds, and 200 nanoseconds");Trait Implementations§
Source§impl Clone for DurationBreakdown
impl Clone for DurationBreakdown
Source§fn clone(&self) -> DurationBreakdown
fn clone(&self) -> DurationBreakdown
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DurationBreakdown
impl Debug for DurationBreakdown
Source§impl Display for DurationBreakdown
impl Display for DurationBreakdown
Source§impl From<Duration> for DurationBreakdown
impl From<Duration> for DurationBreakdown
Source§impl From<DurationBreakdown> for Duration
impl From<DurationBreakdown> for Duration
Source§fn from(db: DurationBreakdown) -> Self
fn from(db: DurationBreakdown) -> Self
Constructs a new std::time::Duration, given a DurationBreakdown.
§Panics
This will panic if the DurationBreakdown’s nanoseconds value is
greater than u32::MAX.