Duration wrapper for humans
Wrapper for std::time::Duration to deal with durations from human readable form
DurationHuman
Parse and format for human interaction
Main goal is to declare a Duration from, as well as formatting into, a human readable string.
Parsing a string, adds all values with a time unit to the total duration, so parse("1min 2s 1min") results in a 122s duration.
Formatting as a string uses the unit for which an integral value can be represented, so a 122s duration will format as 122s, but a 86400s duration will format as 1day.
Formatting as pretty print includes all units that have a non-zero value, so 122s will pretty print as "2min 2s" and 90060 as "1 day 1h 1m"
# use DurationHuman;
let duration = try_from.unwrap;
assert_eq!;
assert_eq!;
let duration = try_from.unwrap;
assert_eq!;
assert_eq!;
let duration = try_from.unwrap;
assert_eq!;
// precision is nano second
let duration = try_from.unwrap;
assert_eq!;
assert_eq!;
let duration = try_from.unwrap;
assert_eq!;
assert_eq!;
let duration = try_from.unwrap;
assert_eq!;
let duration = try_from.unwrap;
assert_eq!;
assert_eq!;
let duration = try_from.unwrap;
assert_eq!;
assert_eq!;
Adding to Instant
std::time::Instant supports Add, but we can't touch that here, so we turn it around:
use Instant;
use ;
let instant = now;
let duration = try_from?;
let after = duration + instant;
let diff = from;
assert_eq!;