Module:Archives/bots
| This Lua module is used on approximately 35,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._human_readable_duration(args)
local time = tonumber(args[1])
if time == nil or time < 0 then
return
"<strong class='error'>Human readable duration error: First argument must be a positive number ([[Template:Human readable duration|help]])</strong>"
end
local unit = string.lower(args[2] or "")
if scale[unit] == nil then
return
"<strong class='error'>Human readable duration error: Second argument must be a valid time unit ([[Template:Human readable duration|help]])</strong>"
end
local timeseconds = time * scale[unit]
if timeseconds < 59.75 then -- rounds to 59.5 seconds or less
local converted = math.floor(timeseconds * 2 + 0.5) / 2
return converted .. " second" .. (converted ~= 1 and "s" or "")
elseif timeseconds < 3585 then -- rounds to 59.5 minutes or less
local converted = math.floor(timeseconds / scale.minute * 2 + 0.5) / 2
return converted .. " minute" .. (converted ~= 1 and "s" or "")
elseif timeseconds < 258300 and timeseconds ~= 86400 and timeseconds ~= 172800 then -- rounds to 71.5 hours or less, excluding 24 and 48 hours exactly
local converted = math.floor(timeseconds / scale.hour * 2 + 0.5) / 2
return converted .. " hour" .. (converted ~= 1 and "s" or "")
elseif timeseconds < 4341600 then -- rounds to 50 days or less
local converted = math.floor(timeseconds / scale.day * 2 + 0.5) / 2
return converted .. " day" .. (converted ~= 1 and "s" or "")
elseif timeseconds < 48651300 then -- rounds to 18 months or less (rounds to nearest integer instead of 0.5)
local converted = math.floor(timeseconds / scale.month + 0.5)
return converted .. " month" .. (converted ~= 1 and "s" or "")
else -- anything over 18 months rounds to nearest 0.5 years
local converted = math.floor(timeseconds / scale.year * 2 + 0.5) / 2
return converted .. " year" .. (converted ~= 1 and "s" or "")
end
end
function p.human_readable_duration(frame)
local args = frame:getParent().args
return p._human_readable_duration(args)
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.