Module:Countdown/sandbox: Difference between revisions
Appearance
Content deleted Content added
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
Line 12: | Line 12: | ||
function p.main(frame) |
function p.main(frame) |
||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
local timeInput = args['time'] or args[1] |
local timeInput = args['time'] or args[1] or error("No time string specified!") |
||
local offset = args['offset'] or args[2] |
local offset = args['offset'] or args[2] or '' |
||
local shorthand = yn(args['shorthand']) |
local shorthand = yn(args['shorthand']) |
||
local timestamp = tonumber(frame:preprocess("{{#time:U|" .. timeInput .. "|}}")) |
local timestamp = tonumber(frame:preprocess("{{#time:U|" .. timeInput .. "|}}")) |
Revision as of 19:51, 29 February 2024
![]() | This is the module sandbox page for Module:Countdown (diff). |
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 460 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 implements {{countdown}}. Please see the template page for documentation.
-- This module powers {{countdown}}.
local p = {}
-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local yn = require("Module:Yesno")
local floor = math.floor
local mod = math.fmod
function p.main(frame)
local args = getArgs(frame)
local timeInput = args['time'] or args[1] or error("No time string specified!")
local offset = args['offset'] or args[2] or ''
local shorthand = yn(args['shorthand'])
local timestamp = tonumber(frame:preprocess("{{#time:U|" .. timeInput .. "|}}"))
local currentTime = tonumber(frame:preprocess("{{#time:U}}"))
local time_days = floor((timestamp - currentTime) / 86400)
local time_hours = floor(mod((timestamp - currentTime) / 3600, 24))
local time_minutes = floor(mod((timestamp - currentTime) / 60, 60))
local time_seconds = floor(mod((timestamp - currentTime), 60))
local out = ''
if shorthand then
-- clip the first letter of each display
local secondDisplay, minuteDisplay, hourDisplay, dayDisplay
secondDisplay = mw.ustring.sub(mw.message:new("seconds", ""):plain(), 0, 1)
minuteDisplay = mw.ustring.sub(mw.message:new("minutes", ""):plain(), 0, 1)
hourDisplay = mw.ustring.sub(mw.message:new("hours", ""):plain(), 0, 1)
dayDisplay = mw.ustring.sub(mw.message:new("days", ""):plain(), 0, 1)
if time_days > 0 then
out = out .. time_days .. dayDisplay .. " "
end
if time_hours > 0 then
out = out .. time_hours .. hourDisplay .. " "
end
if time_minutes > 0 then
out = out .. time_minutes .. minuteDisplay .. " "
end
if time_seconds > 0 then
out = out .. time_seconds .. secondDisplay .. " "
end
else
if time_days > 0 then
out = out .. mw.message:new("days", time_days):plain() .. " "
end
if time_hours > 0 then
out = out .. mw.message:new("hours", time_hours):plain() .. " "
end
if time_minutes > 0 then
out = out .. mw.message:new("minutes", time_minutes):plain() .. " "
end
if time_seconds > 0 then
out = out .. mw.message:new("seconds", time_seconds):plain() .. " "
end
end
return '<span style="' .. args['style'] .. '">' .. out .. '</span> ' .. frame:preprocess("([{{fullurl:{{FULLPAGENAMEE}}|action=purge}} Update])")
end
return p