#macro #duration #human-friendly #parser

duration-human

Duration wrapper to interact with humans

7 releases

0.1.10 Dec 10, 2022
0.1.9 Dec 10, 2022

#186 in Date and time

Download history 113/week @ 2022-12-01 108/week @ 2022-12-08 33/week @ 2022-12-15 54/week @ 2022-12-22 41/week @ 2022-12-29 67/week @ 2023-01-05 98/week @ 2023-01-12 90/week @ 2023-01-19 141/week @ 2023-01-26 151/week @ 2023-02-02 192/week @ 2023-02-09 201/week @ 2023-02-16 145/week @ 2023-02-23 98/week @ 2023-03-02 80/week @ 2023-03-09 99/week @ 2023-03-16

448 downloads per month
Used in 3 crates

Custom license

32KB
644 lines

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 duration_human::DurationHuman;
let duration = DurationHuman::try_from("80h").unwrap();
assert_eq!(format!("{:#}", duration), "3 days 8h".to_string());
assert_eq!(format!("{}", duration), "80h".to_string());
let duration = DurationHuman::try_from("72h").unwrap();
assert_eq!(format!("{:#}", duration), "3 days".to_string());
assert_eq!(format!("{}", duration), "3 days".to_string());
let duration = DurationHuman::try_from("18446744073709551615ns").unwrap();
assert_eq!(format!("{:#}", duration), "5 centuries 84 years 11 months 1 week 6 days 23h 34min 33s 709ms 551μs 615ns".to_string());
// precision is nano second
let duration = DurationHuman::try_from("604800μs").unwrap();
assert_eq!(format!("{:#}", duration), "604ms 800μs".to_string());
assert_eq!(duration.to_string(), "604800μs".to_string());
let duration = DurationHuman::try_from("604800ms").unwrap();
assert_eq!(format!("{:#}", duration), "10min 4s 800ms".to_string());
assert_eq!(duration.to_string(), "604800ms".to_string());
let duration = DurationHuman::try_from("604800s").unwrap();
assert_eq!(format!("{:#}", duration), "1 week".to_string());
let duration = DurationHuman::try_from("604800s").unwrap();
assert_eq!(format!("{:#}", duration), "1 week".to_string());
assert_eq!(format!("{}", duration), "1 week".to_string());
let duration = DurationHuman::try_from("608430s").unwrap();
assert_eq!(format!("{:#}", duration), "1 week 1h 30s".to_string());
assert_eq!(format!("{}", duration), "608430s".to_string());

Adding to Instant

std::time::Instant supports Add, but we can't touch that here, so we turn it around:

use std::time::Instant;
use duration_human::{DurationHuman, DurationError};

let instant = Instant::now();
let duration = DurationHuman::try_from("420s")?;
let after = duration + instant;
let diff = DurationHuman::from(after - instant);
assert_eq!(format!("{}", diff), format!("7min"));

Dependencies

~2.6–3.5MB
~82K SLoC