Skip to main content

Crate compact_reltime

Crate compact_reltime 

Source
Expand description

§compact-reltime

This is a small and simple libraray crate providing compact, human-readable, string formatting for approximated durations of time. The compact format represents a duration in terms of one or two distinct time units which are abbreviated by a single character. The precision of the string format is 1 second for short durations and generally decreases as the duration increases.

Duration strings generated by this crate have the following format:

xxuyyv

Where xx and yy are small integers, and u and v are single-character abbreviations of one of the following units of time:

UnitsAbbreviation
Secondss
Minutesm
Hoursh
Daysd
Weeksw
MonthsM
Yearsy

For example, a duration of 3,204,898 seconds would be formatted as 5w2d (5 weeks and 2 days), and a duration of 324 seconds would be formatted as 5m24s.

§Limitations

This crate does not support durations greater than 99y11M. This is to ensure the format is always less than 7 characters in length. It also does not presently support languages other than English or durations with sub-second precision.

§Usage

The simplest and most straightforward way to use this crate is to directly construct a Reltime value from a number of seconds, then display or render its string representation via Display:

use compact_reltime::Reltime;

let my_duration = Reltime::new(3204898).unwrap();
let my_string = my_duration.to_string(); // my_duration implments Display
assert_eq!(my_string, "5w2d");

Structs§

FormattedReltime
A formatted time duration
Reltime
A duration that formats with human-friendly units and precision via Display.
TooLong
Error type signaling that a Reltime which exceeds the supported duration was attempted to be initialized.

Enums§

Units
Possible units for a component of a relative time.

Constants§

MAX_DURATION
The maximum duration supported by the crate in seconds; 99 years and 11 months

Type Aliases§

ThreshSpec
A type for constructing threshold tables for Reltime::format_with_timetable. A value of this type, x means that a duration less than x.0 seconds will be docomposed into components with major units x.1 and minor units x.2, unless another ThreshSpec value with a lower .0 component is also greater than the duration.