Convert seconds to compound duration: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Combine into a single file for ease of testing) |
(Adds Clojure solution) |
||
Line 1,018: | Line 1,018: | ||
86400 sec is 1 d |
86400 sec is 1 d |
||
6000000 sec is 9 wk, 6 d, 10 hr, 40 min |
6000000 sec is 9 wk, 6 d, 10 hr, 40 min |
||
</pre> |
|||
=={{header|Clojure}}== |
|||
<lang clojure>(require '[clojure.string :as string]) |
|||
(def seconds-in-minute 60) |
|||
(def seconds-in-hour (* 60 seconds-in-minute)) |
|||
(def seconds-in-day (* 24 seconds-in-hour)) |
|||
(def seconds-in-week (* 7 seconds-in-day)) |
|||
(defn seconds->duration [seconds] |
|||
(let [weeks ((juxt quot rem) seconds seconds-in-week) |
|||
wk (first weeks) |
|||
days ((juxt quot rem) (last weeks) seconds-in-day) |
|||
d (first days) |
|||
hours ((juxt quot rem) (last days) seconds-in-hour) |
|||
hr (first hours) |
|||
min (quot (last hours) seconds-in-minute) |
|||
sec (rem (last hours) seconds-in-minute)] |
|||
(string/join ", " |
|||
(filter #(not (string/blank? %)) |
|||
(conj [] |
|||
(when (> wk 0) (str wk " wk")) |
|||
(when (> d 0) (str d " d")) |
|||
(when (> hr 0) (str hr " hr")) |
|||
(when (> min 0) (str min " min")) |
|||
(when (> sec 0) (str sec " sec"))))))) |
|||
(seconds->duration 7259) |
|||
(seconds->duration 86400) |
|||
(seconds->duration 6000000)</lang> |
|||
{{out}} |
|||
<pre> |
|||
"2 hr, 59 sec" |
|||
"1 d" |
|||
"9 wk, 6 d, 10 hr, 40 min" |
|||
</pre> |
</pre> |
||