diff options
| author | Fuwn <[email protected]> | 2021-05-02 17:20:29 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-02 17:20:29 +0000 |
| commit | 6f839a24e989353571c31870cae91c6aeabcb75b (patch) | |
| tree | 7030b6b145e2d75d98222d14a59f491c622cfa01 /src/utils | |
| parent | feat(global): begin implementing api (diff) | |
| download | whirl-6f839a24e989353571c31870cae91c6aeabcb75b.tar.xz whirl-6f839a24e989353571c31870cae91c6aeabcb75b.zip | |
feat(api): flush out statistics endpoint
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/mod.rs | 1 | ||||
| -rw-r--r-- | src/utils/system.rs | 38 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 98990d4..ce264a8 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,3 +2,4 @@ // SPDX-License-Identifier: GPL-3.0-only pub mod db; +pub mod system; diff --git a/src/utils/system.rs b/src/utils/system.rs new file mode 100644 index 0000000..7a823a0 --- /dev/null +++ b/src/utils/system.rs @@ -0,0 +1,38 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +const WEEK: usize = 60 * 60 * 60 * 60; +const DAY: usize = 60 * 60 * 60; +const HOUR: usize = 60 * 60; +const MIN: usize = 60; + +fn make_parts(t: usize, steps: &[usize], mut accum: Vec<usize>) -> Vec<usize> { + match steps.split_first() { + None => accum, + Some((s, steps)) => { + accum.push(t / *s); + make_parts(t % *s, steps, accum) + } + } +} + +pub fn seconds_to_hrtime(seconds: usize) -> String { + let word = ["week", "day", "hour", "min", "sec"]; + + make_parts(seconds, &[WEEK, DAY, HOUR, MIN, 1], Vec::new()) + .iter() + .enumerate() + .filter_map(|(i, s)| { + if s > &0 { + if s > &1 { + Some(format!("{} {}s", s, word[i])) + } else { + Some(format!("{} {}", s, word[i])) + } + } else { + None + } + }) + .collect::<Vec<String>>() + .join(", ") +} |