From 8e297a3e84e6fb86a7f4352f943e62e61c942292 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Tue, 10 Dec 2024 11:46:27 +0100 Subject: [PATCH] add duration to string function --- src/lib.rs | 1 + src/time.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/time.rs diff --git a/src/lib.rs b/src/lib.rs index 7e82089..1a93c50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,4 @@ pub mod grid; pub mod math; pub mod number; pub mod permutation; +pub mod time; diff --git a/src/time.rs b/src/time.rs new file mode 100644 index 0000000..c8c2255 --- /dev/null +++ b/src/time.rs @@ -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(" ") +}