Expand description
htime
is a crate to provide convenient human-centric time formatting.
§Examples
Given a Duration
, generate a legible human output:
// One year minus 1 millisecond (demonstrates all the units).
let dur = std::time::Duration::from_millis(31_557_600_000 - 1);
// Prints the output as a nicely formatted string, with units in descending
// order of size. Specify the degree of unit precision desired as the second
// argument.
assert_eq!(
htime::pretty_print(&dur, "ms", ", "),
"11mo, 4w, 2d, 10h, 29m, 59s, 999ms",
)
Alternatively, you can just get back the list of value/unit strings, and arrange them yourself:
// Returns a list of the units, with no whitespace or separators.
assert_eq!(
htime::components(&dur, "ms"),
vec!["11mo", "4w", "2d", "10h", "29m", "59s", "999ms"],
)
Functions§
- components
- Convert a duration to a list of human time units, in decreasing order of size.
- pretty_
print - Convert a duration to a human readable string.