Struct FancyDuration

Source
pub struct FancyDuration<D: AsTimes + Clone>(pub D);
Expand description

A FancyDuration contains a duration of type that implements AsTimes. It is capable of that point at parsing strings as well as returning the duration value encapsulated. If included in a serde serializing or deserializing workflow, it will automatically construct the appropriate duration as a part of the process.

Support for [time] and [chrono] are available as a part of this library via feature flags.

A duration is “human-readable” when it follows the following format:

<count><timespec>...

This pattern repeats in an expected and prescribed order of precedence based on what duration is supplied. Certain durations are order-dependent (like months and minutes), but most are not; that said it should be desired to represent your durations in precedence order. If you express standard formatting, each unit is separated by whitespace, such as “2m 5s 30ms”, compact formatting removes the whitespace: “2m5s30ms”.

count is simply an integer value with no leading zero-padding. timespec is a one or two character identifier that specifies the unit of time the count represents. The following timespecs are supported, and more may be added in the future based on demand.

The order here is precedence-order. So to express this properly, one might say “5y2d30m” which means “5 years, 2 days and 30 minutes”, but “5y30m2d” means “5 years, 30 months, and 2 days”.

  • y: years
  • m: months (must appear before minutes)
  • w: weeks
  • d: days
  • h: hours
  • m: minutes (must appear after months)
  • s: seconds
  • ms: milliseconds
  • us: microseconds
  • ns: nanoseconds

Simplifications:

Some time units have been simplified:

  • Years is 365 days
  • Months is 30 days

These durations do not account for variations in the potential unit based on the current time. Perhaps in a future release.

Tuple Fields§

§0: D

Implementations§

Source§

impl<D> FancyDuration<D>
where D: AsTimes + Clone,

Source

pub fn new(d: D) -> Self

Construct a fancier duration!

Accept input of a Duration type that implements AsTimes. From here, strings containing human-friendly durations can be constructed, or the inner duration can be retrieved.

Source

pub fn duration(&self) -> D
where D: Clone,

Retrieve the inner duration.

Source

pub fn filter(&self, filter: &[DurationPart]) -> Self

Supply a filter of allowed time values, others will be zeroed out and the time recalculated as if they didn’t exist.

Source

pub fn truncate(&self, limit: usize) -> Self

Truncate to the most significant consecutive values. This will take a number like “1y 2m 3w 4d” and with a value of 2 reduce it to “1y 2m”. Since it works consecutively, minor values will also be dropped, such as “1h 2m 30us”, truncated to 3, would still produce “1h 2m” because “30us” is below the seconds value, which is more significant and would have been counted. “1h 2m 3s” would truncate to 3 with “1h 2m 3s”.

Source

pub fn parse(s: &str) -> Result<Self, Error>

Parse a string that contains a human-readable duration. See FancyDuration for more information on how times are represented.

Source

pub fn format(&self) -> String

Supply the standard formatted human-readable representation of the duration. This format contains whitespace.

Source

pub fn format_compact(&self) -> String

Supply the compact formatted human-readable representation of the duration. This format does not contain whitespace.

Source

pub fn parse_to_ns(s: &str) -> Result<(u64, u64), Error>

Parse a string in fancy duration format to a tuple of (seconds, nanoseconds). Nanoseconds is simply a subsecond count and does not contain the seconds represented as nanoseconds. If a parsing error occurs that will appear in the result.

Trait Implementations§

Source§

impl<D: Clone + AsTimes + Clone> Clone for FancyDuration<D>

Source§

fn clone(&self) -> FancyDuration<D>

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<D: Debug + AsTimes + Clone> Debug for FancyDuration<D>

Source§

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

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

impl<D> Display for FancyDuration<D>
where D: AsTimes + Clone,

Source§

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

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

impl<D> FromStr for FancyDuration<D>
where D: AsTimes + Clone,

Source§

type Err = Error

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

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

Parses a string s to return a value of this type. Read more
Source§

impl<D: PartialEq + AsTimes + Clone> PartialEq for FancyDuration<D>

Source§

fn eq(&self, other: &FancyDuration<D>) -> 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<D: AsTimes + Clone> StructuralPartialEq for FancyDuration<D>

Auto Trait Implementations§

§

impl<D> Freeze for FancyDuration<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for FancyDuration<D>
where D: RefUnwindSafe,

§

impl<D> Send for FancyDuration<D>
where D: Send,

§

impl<D> Sync for FancyDuration<D>
where D: Sync,

§

impl<D> Unpin for FancyDuration<D>
where D: Unpin,

§

impl<D> UnwindSafe for FancyDuration<D>
where D: UnwindSafe,

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.