about summary refs log tree commit diff stats
path: root/lib/Taurus/Seconds.rakumod
diff options
context:
space:
mode:
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) }
+    }
+}