human-duration converts a std::time::Duration
to a human readable string.
Documentation | Github | Crates.io
use human_duration::human_duration;
let duration = std::time::Duration::new(120, 30_000_000);
assert_eq!(human_duration(&duration), "2m 0s 30ms");
let duration = std::time::Duration::new(9000, 0);
assert_eq!(human_duration(&duration), "2h 30m 0s 0ms");
Run
cargo add human_duration
or manually add it to your Cargo.toml
:
[dependencies]
human_duration = "0.1"
Then use the human_duration
function:
use human_duration::human_duration;
// somewhere in your code:
let duration = std::time::Duration::new(120, 0);
println!(human_duration(&duration));
Crate | Duration::new(0, 0) | Duration::new(0, 5_000_000) | Duration::new(5, 0) | Duration::new(34_536_000, 0) | Duration::new(86_400, 337_000_000) |
---|---|---|---|---|---|
human-duration | 0ms | 5ms | 5s 0ms | 1y 1mon 4d 7h 20m 0s 0ms | 1d 0h 0m 0s 337ms |
humantime | 0s | 5ms | 5s | 1year 1month 4days 46m 24s | 1day 337ms |
time-humanize | now | now | now | in a year | in a day |
MIT