Module:Human readable duration
Appearance
![]() | This Lua module is used on approximately 31,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module converts a duration into a form with units which leave the value easier to read. It should be invoked through the template {{human readable duration}}, and is documented there.
local p = {}
-- reproduced from Module:Convert
local scale = {
second = 1,
seconds = 1,
minute = 60,
minutes = 60,
hour = 3600,
hours = 3600,
day = 86400,
days = 86400,
month = 2629800,
months = 2629800,
year = 31557600,
years = 31557600
}
function p.main(frame)
local args = frame:getParent().args
local time = tonumber(args[1])
if time == nil then
return "<strong class='error'>Error: First argument must be a number</strong>"
end
if scale[args[2]] == nil then
return "<strong class='error'>Error: Second argument must be a valid time unit</strong>"
end
local timeseconds = tonumber(args[1]) * scale[args[2]]
if timeseconds < 60 then -- less than a minute
return timeseconds .. " seconds"
elseif timeseconds < 3585 then -- rounds to 59.5 minutes or less
return math.floor(timeseconds / scale.minute * 2 + 0.5) / 2 .. " minutes"
elseif timeseconds < 258300 and timeseconds ~= 86400 and timeseconds ~= 172800 then -- rounds to 71.5 hours or less, excluding 24 and 48 hours exactly
return math.floor(timeseconds / scale.hour * 2 + 0.5) / 2 .. " hours"
elseif timeseconds < 4341600 then -- rounds to 50 days or less
return math.floor(timeseconds / scale.day * 2 + 0.5) / 2 .. " days"
elseif timeseconds < 48651300 then -- rounds to 18 months or less (rounds to nearest integer instead of 0.5)
return math.floor(timeseconds / scale.month + 0.5) .. " months"
else -- anything over 18 months rounds to nearest 0.5 years
return math.floor(timeseconds / scale.year * 2 + 0.5) / 2 .. " years"
end
end
return p