432
edits
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local Number = require('Module:Number') | |||
-- 123 => 00:02:03 | -- 123 => 00:02:03 | ||
function p.secondsToHMS( | function p.secondsToHMS(totalSeconds) | ||
local | local days = math.floor(totalSeconds / (24 * 60 * 60)) | ||
if | if days >= 1 then | ||
return string.format('%d day%s, ', | return string.format('%d day%s, ', days, (days > 1) and "s" or "") .. os.date("!%X", totalSeconds) | ||
else | else | ||
return os.date("!%X", | return os.date("!%X", totalSeconds) | ||
end | end | ||
end | end | ||
-- 123 => 2 minutes, 3 seconds | -- 123 => 2 minutes, 3 seconds | ||
function p.secondsToHuman( | function p.secondsToHuman(totalSeconds) | ||
if | ts = Number.secondsToTimeSpan(totalSeconds) | ||
if | if ts.totalSeconds < 60 then | ||
return string.format(' | if math.floor(ts.totalSeconds) == 0 then | ||
return string.format('0 seconds') | |||
else | else | ||
return string.format(' | return string.format('%.1f seconds', ts.totalSeconds) | ||
end | end | ||
end | end | ||
local output = {} | local output = {} | ||
if days > 0 then | if ts.days > 0 then | ||
table.insert(output, string.format('%d day%s', days, (days > 1) and "s" or "")) | table.insert(output, string.format('%d day%s', ts.days, (ts.days > 1) and "s" or "")) | ||
end | end | ||
if hours > 0 then | if ts.hours > 0 then | ||
table.insert(output, string.format('%d hour%s', hours, (hours > 1) and "s" or "")) | table.insert(output, string.format('%d hour%s', ts.hours, (ts.hours > 1) and "s" or "")) | ||
end | end | ||
if minutes > 0 then | if ts.minutes > 0 then | ||
table.insert(output, string.format('%d minute%s', minutes, (minutes > 1) and "s" or "")) | table.insert(output, string.format('%d minute%s', ts.minutes, (ts.minutes > 1) and "s" or "")) | ||
end | end | ||
if seconds > 0 then | if ts.seconds > 0 then | ||
table.insert(output, string.format('%d second%s', seconds, (seconds > 1) and "s" or "")) | table.insert(output, string.format('%d second%s', ts.seconds, (ts.seconds > 1) and "s" or "")) | ||
end | end | ||
return table.concat(output, ", ") | return table.concat(output, ", ") |
edits