Module:Countdown: Difference between revisions
Appearance
Content deleted Content added
Theopolisme (talk | contribs) Use Module:Arguments |
Theopolisme (talk | contribs) Modifications |
||
Line 7: | Line 7: | ||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
function formatMessage(secondsLeft, event, color) |
function formatMessage(secondsLeft, event, color, refreshLink) |
||
local timeLeft = lang:formatDuration(secondsLeft) |
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 |
||
Line 19: | Line 19: | ||
local timeLeft = mw.ustring.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>') |
local timeLeft = mw.ustring.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>') |
||
-- Make the refresh link and join it all together. |
-- Make the refresh link and join it all together. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 28: | Line 26: | ||
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>' |
|||
end |
end |
||
Line 34: | Line 32: | ||
local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time) |
local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time) |
||
⚫ | |||
⚫ | |||
if timeToStart > 0 then |
if timeToStart > 0 then |
||
-- Event has not begun yet |
-- Event has not begun yet |
||
return formatMessage(timeToStart, args.event or 'the event begins', args.color) |
return formatMessage(timeToStart, args.event or 'the event begins', args.color, refreshLink) |
||
elseif args.duration then |
elseif args.duration then |
||
local timeToEnd |
local timeToEnd |
||
if args['duration unit'] then |
if args['duration unit'] then |
||
-- Duration is in unit other than seconds, use |
-- Duration is in unit other than seconds, use formatDate to add |
||
timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit'])) |
timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit'])) |
||
else |
else |
||
Line 47: | Line 47: | ||
if timeToEnd > 0 then |
if timeToEnd > 0 then |
||
-- Event is in progress |
-- Event is in progress |
||
return args.eventstart or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color) |
return args.eventstart .. refreshLink or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color, refreshLink) |
||
else |
else |
||
-- Event had a duration and has now ended |
-- Event had a duration and has now ended |
||
return args.eventend or ((lang:ucfirst(args.event) or 'The event') .. ' has ended.') |
return ((args.eventend or ((lang:ucfirst(args.event) or 'The event') .. ' has ended.')) .. refreshLink) |
||
end |
end |
||
else |
else |
||
-- Event had no duration and has begun |
-- Event had no duration and has begun |
||
return args.eventstart or ((lang:ucfirst(args.event) or 'The event') .. ' has started.') |
return ((args.eventstart or ((lang:ucfirst(args.event) or 'The event') .. ' has started.')) .. refreshLink) |
||
end |
end |
||
end |
end |
Revision as of 09:03, 14 December 2013
![]() | 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
function formatMessage(secondsLeft, event, color, refreshLink)
local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
-- Find whether we are plural or not.
local isOrAre
if mw.ustring.match(timeLeft, '^%d+') == '1' then
isOrAre = 'is'
else
isOrAre = 'are'
end
-- 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>')
-- Make the refresh link and join it all together.
return mw.ustring.format('There %s %s until %s.%s', isOrAre, 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 = mw.ustring.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.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