Module:Countdown

-- This module powers.

local p = {}

-- Constants local lang = mw.language.getContentLanguage local getArgs = require('Module:Arguments').getArgs

local yesno = require('Module:Yesno')

local function formatMessage(secondsLeft, event, color, noweeks) local intervals = {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'} if noweeks then table.remove(intervals, 2) end local timeLeft = lang:formatDuration(secondsLeft, intervals) -- Find whether we are plural or not. local isOrAre if string.match(timeLeft, '^%d+') == '1' then isOrAre = 'is' else isOrAre = 'are' end -- Replace time to better format -- Color and bold the numbers, because it makes them look important. local h = string.match(timeLeft, '(%d+) hours?') or '00' local m = string.match(timeLeft, '(%d+) minutes?') or '00' local s = string.match(timeLeft, '(%d+) seconds?') or '00' if not h:match('%d%d') then h = '0' .. h end if not m:match('%d%d') then m = '0' .. m end if not s:match('%d%d') then s = '0' .. s end local timeStr = ('%s:%s:%s'):format(h, m, s)	timeLeft = string.gsub(timeLeft, ', %d+ hours?', '') timeLeft = string.gsub(timeLeft, ' and %d+ hours?', '') timeLeft = string.gsub(timeLeft, ', %d+ minutes?', '') timeLeft = string.gsub(timeLeft, ' and %d+ minutes?', '') timeLeft = string.gsub(timeLeft, ', %d+ seconds?', '') timeLeft = string.gsub(timeLeft, ' and %d+ seconds?', '') timeLeft = string.gsub(timeLeft, ' and', ',') if timeLeft ~= '' then timeLeft = timeLeft .. ' and ' end timeLeft = timeLeft .. timeStr timeLeft = string.gsub(timeLeft, '(%d+)', '%1 ') return string.format('There %s %s until %s.', isOrAre, timeLeft, event) end

function p.main(frame) local args = getArgs(frame) if not (args.year and args.month and args.day) then return ' Error: year, month, and day must be specified ' end local timeArgs = {year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second} for k,v in pairs(timeArgs) do		if not tonumber(v) then error('Argument ' .. k .. ' could not be parsed as a number: ' .. v)		end end local eventTime = os.time(timeArgs) local timeToStart = os.difftime(eventTime, os.time) -- (future time - current time) local text local noweeks = yesno(args.noweeks, false) if timeToStart > 0 then -- Event has not begun yet text = formatMessage(timeToStart, args.event or 'the event begins', args.color, noweeks) 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) or error('args.duration should be a number of seconds', 0)) end if timeToEnd > 0 then -- Event is in progress text = args.eventstart or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color, noweeks) else -- Event had a duration and has now ended text = args.eventend or ((lang:ucfirst(args.event or 'The event')) .. ' has ended.') end else -- Event had no duration and has begun text = args.eventstart or ((lang:ucfirst(args.event or 'The event')) .. ' has started.') end local refreshLink if yesno(args.refresh) then refreshLink = '' else refreshLink = mw.title.getCurrentTitle:fullUrl({ action = 'purge' }) refreshLink = string.format(' ([%s refresh]) ', refreshLink) end return text .. refreshLink end

return p