DurationBreakdown

Struct DurationBreakdown 

Source
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

Source

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

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

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");
Source

pub fn weeks(&self) -> u64

Gets the number of weeks in the breakdown.

Source

pub fn days(&self) -> u64

Gets the number of days in the breakdown.

Source

pub fn hours(&self) -> u64

Gets the number of hours in the breakdown.

Source

pub fn minutes(&self) -> u64

Gets the number of minutes in the breakdown.

Source

pub fn seconds(&self) -> u64

Gets the number of seconds in the breakdown.

Source

pub fn nanoseconds(&self) -> u64

Gets the number of nanoseconds in the breakdown.

Source

pub fn weeks_as_string(&self) -> String

A string describing the number of weeks in the breakdown. E.g. "14 weeks".

Source

pub fn days_as_string(&self) -> String

A string describing the number of days in the breakdown. E.g. "6 days".

Source

pub fn hours_as_string(&self) -> String

A string describing the number of hours in the breakdown. E.g. "1 hour".

Source

pub fn minutes_as_string(&self) -> String

A string describing the number of minutes in the breakdown. E.g. "53 minutes".

Source

pub fn seconds_as_string(&self) -> String

A string describing the number of seconds in the breakdown. E.g. "40 seconds".

Source

pub fn nanoseconds_as_string(&self) -> String

A string describing the number of nanoseconds in the breakdown. E.g. "1700 nanoseconds".

Source

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");
Source

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

Source§

fn clone(&self) -> DurationBreakdown

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 Debug for DurationBreakdown

Source§

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

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

impl Display for DurationBreakdown

Source§

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

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

impl From<Duration> for DurationBreakdown

Source§

fn from(duration: Duration) -> Self

Constructs a new duration breakdown, given an instance of std::time::Duration.

Source§

impl From<DurationBreakdown> for Duration

Source§

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.

Source§

impl PartialEq for DurationBreakdown

Source§

fn eq(&self, other: &DurationBreakdown) -> 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 Copy for DurationBreakdown

Source§

impl Eq for DurationBreakdown

Source§

impl StructuralPartialEq for DurationBreakdown

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> 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.