Struct fancy_duration::FancyDuration
source · pub struct FancyDuration<D: AsTimes>(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: DImplementations§
source§impl<D> FancyDuration<D>where
D: AsTimes,
impl<D> FancyDuration<D>where
D: AsTimes,
sourcepub fn new(d: D) -> Self
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.
sourcepub fn parse(s: &str) -> Result<Self, Error>
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.
sourcepub fn format(&self) -> String
pub fn format(&self) -> String
Supply the standard formatted human-readable representation of the duration. This format contains whitespace.
sourcepub fn format_compact(&self) -> String
pub fn format_compact(&self) -> String
Supply the compact formatted human-readable representation of the duration. This format does not contain whitespace.
Trait Implementations§
source§impl<D: Clone + AsTimes> Clone for FancyDuration<D>
impl<D: Clone + AsTimes> Clone for FancyDuration<D>
source§fn clone(&self) -> FancyDuration<D>
fn clone(&self) -> FancyDuration<D>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<'de, T> Deserialize<'de> for FancyDuration<T>where
T: AsTimes,
impl<'de, T> Deserialize<'de> for FancyDuration<T>where
T: AsTimes,
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<D> Display for FancyDuration<D>where
D: AsTimes,
impl<D> Display for FancyDuration<D>where
D: AsTimes,
source§impl<D> FromStr for FancyDuration<D>where
D: AsTimes,
impl<D> FromStr for FancyDuration<D>where
D: AsTimes,
source§impl<D: PartialEq + AsTimes> PartialEq for FancyDuration<D>
impl<D: PartialEq + AsTimes> PartialEq for FancyDuration<D>
source§fn eq(&self, other: &FancyDuration<D>) -> bool
fn eq(&self, other: &FancyDuration<D>) -> bool
self and other values to be equal, and is used
by ==.