diff options
author | Joris | 2022-02-12 16:57:19 +0100 |
---|---|---|
committer | Joris | 2022-02-12 16:57:19 +0100 |
commit | 3adf3f9697c4e2beb10e652947046d5fddda2ed4 (patch) | |
tree | 689e704cf4665cc4cd1949c06d4a7b0553d07501 /src/util | |
parent | a2ba205df12332a360d89991fe508f964d88c73f (diff) |
Say when the next card will be available
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/mod.rs | 1 | ||||
-rw-r--r-- | src/util/time.rs | 33 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/util/mod.rs b/src/util/mod.rs index c5504af..c866e61 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,4 @@ #[allow(dead_code)] pub mod event; pub mod serialization; +pub mod time; diff --git a/src/util/time.rs b/src/util/time.rs new file mode 100644 index 0000000..f88955d --- /dev/null +++ b/src/util/time.rs @@ -0,0 +1,33 @@ +use anyhow::Result; +use std::time::SystemTime; + +pub fn now() -> Result<u64> { + Ok(SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs()) +} + +/// Pretty print duration. +pub fn pp_duration(seconds: u64) -> String { + let minutes = (seconds as f64 / 60.).round(); + let hours = (minutes / 60.).round(); + let days = (hours / 24.).round(); + + if seconds < 60 { + plural(seconds, "seconde") + } else if minutes < 60. { + plural(minutes as u64, "minute") + } else if hours < 24. { + plural(hours as u64, "heure") + } else { + plural(days as u64, "jour") + } +} + +fn plural(n: u64, str: &str) -> String { + if n <= 1 { + format!("{} {}", n, str) + } else { + format!("{} {}s", n, str) + } +} |