Module:Countdown
Module documentation
[create]
-- This module powers {{countdown}}.
local p = {}
-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local function formatMessage(secondsLeft, event, color, refreshLink)
local absSecondLeft = math.abs( secondsLeft )
local timeLeft = lang:formatDuration(absSecondLeft, {'years', 'weeks', 'days', 'hours', 'minutes'})
local isSingleSubject = string.match(timeLeft, '^%d+') == '1'
-- Color and bold the numbers, because it makes them look important.
local timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')
-- Make the refresh link and join it all together.
local msg
if secondsLeft > 0 then
-- the event is in the future
-- TODO: internationalization
msg = isSingleSubject
and 'There is %s until %s.%s'
or 'There are %s until %s.%s'
else
-- the event is in the past
-- TODO: internationalization
msg = isSingleSubject
and 'It has been %s passed after %s.%s'
or 'It has been %s passed after %s.%s'
end
return string.format( msg, timeLeft, event, refreshLink )
end
function p.main(frame)
local args = getArgs(frame)
if not (args.year and args.month and args.day) then
return '<strong class="error">Error: year, month, and day must be specified</strong>'
end
local eventTime = os.time({year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second})
local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)
local refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
refreshLink = string.format(' <small><span class="plainlinks">([%s refresh])</span></small>', refreshLink)
if timeToStart > 0 then
-- Event has not begun yet
return formatMessage(timeToStart, args.event or 'the event begins', args.color, refreshLink)
elseif args.countup == 'yes' then
return formatMessage(timeToStart, args.event or 'started', args.color, refreshLink)
elseif args.duration then
local timeToEnd
if args['duration unit'] then
-- Duration is in unit other than seconds, use formatDate to add
timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
else
timeToEnd = timeToStart + tonumber(args.duration)
end
if timeToEnd > 0 then
-- Event is in progress
return args.eventstart .. refreshLink or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color, refreshLink)
else
-- Event had a duration and has now ended
return (args.eventend or ((lang:ucfirst(args.event or 'The event')) .. ' has ended.')) .. refreshLink
end
else
-- Event had no duration and has begun
return (args.eventstart or ((lang:ucfirst(args.event or 'The event')) .. ' has started.')) .. refreshLink
end
end
return p