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
impl Reltime
Sourcepub fn new(seconds: u64) -> Result<Self, TooLong>
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
Sourcepub fn format(self, f: impl FnOnce(u64) -> (Units, Units)) -> FormattedReltime
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.
Sourcepub fn format_with_timetable(self, threshmap: &[ThreshSpec]) -> FormattedReltime
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:
x.1 >= x.2for allxinthreshmapthreshmap[0].0,threshmap[1].0,threshmap[2].0… must be monotonically increasing
Trait Implementations§
impl Copy for Reltime
Source§impl Display for Reltime
Format using human-friendly units and precision.
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.