about summary refs log tree commit diff stats
path: root/lib/Taurus/Seconds.rakumod
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-08-16 00:26:23 +0530
committerAndinus <andinus@nand.sh>2021-08-16 00:26:23 +0530
commit34fff163576418fb7daae0a787deffa3dac42e39 (patch)
treec93cc7304f6b9a78dacb21915dcbfbb239669949 /lib/Taurus/Seconds.rakumod
parent15346a07a024fd42dbc597aea8900c2c2f035792 (diff)
downloadtaurus-34fff163576418fb7daae0a787deffa3dac42e39.tar.gz
Add seconds-to-str subroutine to format seconds, add Taurus::Seconds
Formatting seconds was moved to seconds-to-str module.
Diffstat (limited to 'lib/Taurus/Seconds.rakumod')
-rw-r--r--lib/Taurus/Seconds.rakumod11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/Taurus/Seconds.rakumod b/lib/Taurus/Seconds.rakumod
new file mode 100644
index 0000000..647f96d
--- /dev/null
+++ b/lib/Taurus/Seconds.rakumod
@@ -0,0 +1,11 @@
+unit module Taurus::Seconds;
+
+sub seconds-to-str(UInt $seconds --> Str) is export {
+    given $seconds {
+        when * < 60 { $seconds ~ "s" }
+        when * < 3600 { "%2dm %2ds".sprintf($_ div 60, $_ % 60) }
+        when * < 86400 { "%2dh %2dm %2ds".sprintf($_ div 3600, ($_ % 3600) div 60, $_ % 60) }
+        default { "%dd %2dh %2dm %2ds"
+                  .sprintf($_ div 86400, ($_ % 86400) div 3600, ($_ % 3600) div 60, $_ % 60) }
+    }
+}