Skip to main content

Reltime

Struct Reltime 

Source
pub struct Reltime(/* private fields */);
Expand description

A duration that formats with human-friendly units and precision via Display.

This type represents a duration of time with 1 second resolution, and is primarily useful via its implementation of Display. The Display format is a short string representing the approximate duration in terms of one or two distinct units of time which are automatically selected based on the duration where the longer the duration, the larger the units selected to represent it are. In general the string format is lossy, it approximates the duration by rounding it down to the lowest increment of the minor units selected to represent it.

§Unit elision

In some cases, the quantity of the smallest unit selected to represent the duration is zero, and in these cases the 0-valued unit component is omitted. For example, consider a duration of 605,300 seconds, which is exactly 1 week 8 minutes and 20 seconds: the units selected to represent this duration are weeks and days, therefore the decomposition is 1w + 0d. Since the days component of this decomposition is zero, it is omitted from the formatted string and the final formatting for this duration is just 1w.

§Examples

use compact_reltime::Reltime;

let reltime = Reltime::new(135243).unwrap();
assert_eq!(reltime.to_string(), "37h34m");

Implementations§

Source§

impl Reltime

Source

pub fn new(seconds: u64) -> Result<Self, TooLong>

Create a new value from a duration of seconds seconds. Duration in seconds must be less than or equal to MAX_DURATION seconds

Source

pub fn seconds(self) -> u64

get the underlying duration in seconds

Source

pub fn format(self, f: impl FnOnce(u64) -> (Units, Units)) -> FormattedReltime

Format a duration using a custom decomposition

Duration is formatted as a sum of terms with units defined by f which takes the duration in seconds and returns the major and minor units to decompose it into.

Source

pub fn format_with_timetable(self, threshmap: &[ThreshSpec]) -> FormattedReltime

Create a formatted relative time representation of this duration using a custom time table to determine the units used for decomposition.

This method allows callers more fine-grained control over the units selected for decomposing the duration into its unit components. The two units used to decompose the duration are those in the element of threshmap whose first member is the lowest such among all those which have a first element higher than the this duration in seconds.

note: threshmap must uphold the following conditions otherwise the return value of this method is meaninless:

  1. x.1 >= x.2 for all x in threshmap
  2. threshmap[0].0, threshmap[1].0, threshmap[2].0… must be monotonically increasing

Trait Implementations§

Source§

impl Clone for Reltime

Source§

fn clone(&self) -> Reltime

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Reltime

Source§

impl Debug for Reltime

Source§

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

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

impl Display for Reltime

Format using human-friendly units and precision.

This implementation uses a built-in default time table to determine the units used to represent the duration. For direct control over how the duration is decomposed into units, see Reltime::format.

Source§

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

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

impl Eq for Reltime

Source§

impl From<Reltime> for Duration

Source§

fn from(r: Reltime) -> Self

Converts to this type from the input type.
Source§

impl Hash for Reltime

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 Ord for Reltime

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · 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 Reltime

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Reltime

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 StructuralPartialEq for Reltime

Source§

impl TryFrom<Duration> for Reltime

Source§

type Error = TooLong

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

fn try_from(value: Duration) -> Result<Self, Self::Error>

Performs the conversion.

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.