Difference between revisions of "Module:Countdown"

From blackwiki
Jump to navigation Jump to search
blackwiki>AntanO
m (mistake)
m (10 revisions imported)
 
(4 intermediate revisions by 3 users not shown)
Line 7: Line 7:
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
  
function formatMessage(secondsLeft, event, color, refreshLink)
+
local function formatMessage(secondsLeft, event, color)
    local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
+
local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
    -- Find whether we are plural or not.
+
-- Find whether we are plural or not.
    local isOrAre
+
local isOrAre
    if mw.ustring.match(timeLeft, '^%d+') == '1' then
+
if string.match(timeLeft, '^%d+') == '1' then
        isOrAre = 'is'
+
isOrAre = 'is'
    else
+
else
        isOrAre = 'are'
+
isOrAre = 'are'
    end
+
end
    -- Color and bold the numbers, because it makes them look important.
+
-- Color and bold the numbers, because it makes them look important.
    local timeLeft = mw.ustring.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')
+
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.
+
return string.format('There %s %s until %s.', isOrAre, timeLeft, event)
    return mw.ustring.format('There %s %s until %s.%s', isOrAre, timeLeft, event, refreshLink)
 
 
end
 
end
  
 
function p.main(frame)
 
function p.main(frame)
    local args = getArgs(frame)
+
local args = getArgs(frame)
  
    if not (args.year and args.month and args.day) then
+
if not (args.year and args.month and args.day) then
        return '<strong class="error">Error: year, month, and day must be specified</strong>'
+
return '<strong class="error">Error: year, month, and day must be specified</strong>'
    end
+
end
  
    local eventTime = os.time{year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second}
+
local timeArgs = {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)
+
for k,v in pairs(timeArgs) do
 
+
if not tonumber(v) then
    local refreshLink = mw.title.getCurrentTitle():fullUrl{action = 'purge'}
+
error('Argument ' .. k .. ' could not be parsed as a number: ' .. v)
    refreshLink = mw.ustring.format(' <small><span class="plainlinks">([%s refresh])</span></small>', refreshLink)
+
end
    if timeToStart > 0 then
+
end
        -- Event has not begun yet
+
local eventTime = os.time(timeArgs)
        return formatMessage(timeToStart, args.event or 'the event begins', args.color, refreshLink)
+
local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)
    elseif args.duration then
+
local text
        local timeToEnd
+
if timeToStart > 0 then
        if args['duration unit'] then
+
-- Event has not begun yet
            -- Duration is in unit other than seconds, use formatDate to add
+
text = formatMessage(timeToStart, args.event or 'the event begins', args.color)
            timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
+
elseif args.duration then
        else
+
local timeToEnd
            timeToEnd = timeToStart + tonumber(args.duration)
+
if args['duration unit'] then
        end
+
-- Duration is in unit other than seconds, use formatDate to add
        if timeToEnd > 0 then
+
timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
            -- Event is in progress
+
else
            return args.eventstart .. refreshLink or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color, refreshLink)
+
timeToEnd = timeToStart + (tonumber(args.duration) or error('args.duration should be a number of seconds', 0))
        else
+
end
            -- Event had a duration and has now ended
+
if timeToEnd > 0 then
            return ((args.eventend or ((lang:ucfirst(args.event) or 'The event') .. ' has ended.')) .. refreshLink)
+
-- Event is in progress
        end
+
text = args.eventstart or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color)
    else
+
else
        -- Event had no duration and has begun
+
-- Event had a duration and has now ended
        return ((args.eventstart or ((lang:ucfirst(args.event) or 'The event') .. ' has started.')) .. refreshLink)
+
text = args.eventend or ((lang:ucfirst(args.event or 'The event')) .. ' has ended.')
    end
+
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 args.refresh == 'no' then
 +
refreshLink = ''
 +
else
 +
refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
 +
refreshLink = string.format(' <small><span class="plainlinks">([%s refresh])</span></small>', refreshLink)
 +
end
 +
return text .. refreshLink
 
end
 
end
  
 
return p
 
return p

Latest revision as of 15:56, 26 September 2020

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 function formatMessage(secondsLeft, event, color)
	local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
	-- Find whether we are plural or not.
	local isOrAre
	if string.match(timeLeft, '^%d+') == '1' then
		isOrAre = 'is'
	else
		isOrAre = 'are'
	end
	-- 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 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 '<strong class="error">Error: year, month, and day must be specified</strong>'
	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
	if timeToStart > 0 then
		-- Event has not begun yet
		text = formatMessage(timeToStart, args.event or 'the event begins', args.color)
	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)
		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 args.refresh == 'no' then
		refreshLink = ''
	else
		refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
		refreshLink = string.format(' <small><span class="plainlinks">([%s refresh])</span></small>', refreshLink)
	end
	return text .. refreshLink
end

return p