Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(→{{header|beeswax}}: beeswax example added) |
(Added Lua version) |
||
Line 768: | Line 768: | ||
86400: 1 d |
86400: 1 d |
||
6000000: 9 wk, 6 d, 10 hr, 40 min</lang> |
6000000: 9 wk, 6 d, 10 hr, 40 min</lang> |
||
=={{header|Lua}}== |
|||
<lang Lua>function duration (secs) |
|||
local units, dur = {"wk", "d", "hr", "min"}, "" |
|||
for i, v in ipairs({604800, 86400, 3600, 60}) do |
|||
if secs >= v then |
|||
dur = dur .. math.floor(secs / v) .. " " .. units[i] .. ", " |
|||
secs = secs % v |
|||
end |
|||
end |
|||
if secs == 0 then |
|||
return dur:sub(1, -3) |
|||
else |
|||
return dur .. secs .. " sec" |
|||
end |
|||
end |
|||
print(duration(7259)) |
|||
print(duration(86400)) |
|||
print(duration(6000000))</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |