This is a library for human readable elapsed time string
Rust 1.6 or newer
Put this in your Cargo.toml
:
toml
[dependencies]
human-time="0"
```rust use std::{ thread::{self}, time::Duration, };
use human_time::ToHumanTimeString;
fn main() { println!( "costs {}", Duration::fromsecs(88401 * 2 * 8).tohumantimestring() ); println!( "costs {}", Duration::frommillis(8840003).tohumantimestring() ); println!("costs {}", Duration::frommillis(0).tohumantimestring()); println!( "costs {}", Duration::frommillis(8840003).tohumantimestringwithformat( |n, unit| { format!( "{n}{}", match unit { "d" => "days".toowned(), "h" => "hours".toowned(), "m" => "minutes".toowned(), "s" => "seconds".toowned(), "ms" => "ms".toowned(), other => other.tostring(), } ) }, |acc, item| format!("{}, {}", acc, item) ) );
foo();
}
fn foo() {
thread::sleep(Duration::from_millis(100));
}
Output
text
costs 16d,8h,53m,36s
costs 2h,27m,20s,3ms
costs 0ms
costs 2hours, 27minutes, 20seconds, 3ms
fn foo costs 1s,3ms
```