Jump to content


From Wikimania

Documentation for this module may be created at Module:Countdown/doc

-- This module powers {{countdown}}.

local p = {}

-- Text for "The event has begun" in other languages
local alreadyStarted = {
	['en'] = 'The event has begun',
	['pl'] = 'Wydarzenie się rozpoczęło',

-- Constants
local getArgs = require('Module:Arguments').getArgs

local function formatMessage(secondsLeft, color, lang)
	local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
	-- Color and bold the numbers, because it makes them look important.
	timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')
	return timeLeft

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>'

	-- Align the hour with ISO Y-M-D, so enforce 0 as default hour
	local timeArgs = {year=args.year, month=args.month, day=args.day, hour=args.hour or 0, 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)
	-- Using mw.language or mw.message will result in English no matter what's the page language
	local langCode = frame:callParserFunction('int:lang')
	local lang = mw.language.new(langCode)
	local eventTime = os.time(timeArgs)
	local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)
	local text
	if timeToStart > 0 then
		-- Event has not begun yet
		text = formatMessage(timeToStart, args.color, lang)
		-- Event has begun
		text = args.alreadystarted or alreadyStarted[langCode] or alreadyStarted['en']
	local refreshLink
	if args.refresh == 'no' or timeToStart <= 0 then
		refreshLink = ''
		local purgeMsg = mw.message.new('purge')
		local purgeText = purgeMsg:inLanguage(langCode):plain()
		refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
		refreshLink = string.format(' <small><span class="plainlinks">([%s %s])</span></small>', refreshLink, purgeText)
	return text .. refreshLink

return p