add duration to string function
This commit is contained in:
parent
bfbdb6a20e
commit
8e297a3e84
@ -4,3 +4,4 @@ pub mod grid;
|
|||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod number;
|
pub mod number;
|
||||||
pub mod permutation;
|
pub mod permutation;
|
||||||
|
pub mod time;
|
||||||
|
27
src/time.rs
Normal file
27
src/time.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub fn get_elapsed_string(duration: Duration) -> String {
|
||||||
|
let total_microseconds = duration.as_micros();
|
||||||
|
let total_milliseconds = duration.as_millis();
|
||||||
|
let seconds = duration.as_secs() % 60;
|
||||||
|
let minutes = (duration.as_secs() / 60) % 60;
|
||||||
|
let milliseconds = total_milliseconds % 1000;
|
||||||
|
let microseconds = total_microseconds % 1_000;
|
||||||
|
|
||||||
|
let mut parts = vec![];
|
||||||
|
|
||||||
|
if minutes > 0 {
|
||||||
|
parts.push(format!("{}m", minutes));
|
||||||
|
}
|
||||||
|
if seconds > 0 {
|
||||||
|
parts.push(format!("{}s", seconds));
|
||||||
|
}
|
||||||
|
if milliseconds > 0 {
|
||||||
|
parts.push(format!("{}ms", milliseconds));
|
||||||
|
}
|
||||||
|
if microseconds > 0 {
|
||||||
|
parts.push(format!("{}µs", microseconds));
|
||||||
|
}
|
||||||
|
|
||||||
|
parts.join(" ")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user