add duration to string function

This commit is contained in:
Fabian Schmidt 2024-12-10 11:46:27 +01:00
parent bfbdb6a20e
commit 8e297a3e84
2 changed files with 28 additions and 0 deletions

View File

@ -4,3 +4,4 @@ pub mod grid;
pub mod math;
pub mod number;
pub mod permutation;
pub mod time;

27
src/time.rs Normal file
View 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(" ")
}