Module:Sandbox/Awesome Aasim/Countdown2: Difference between revisions

Content deleted Content added
testing with different texts
 
work on alternate countdown module
Line 1:
-- This module powers {{countdown2countdown}}.
 
local p = {}
Line 6:
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local yn = require("Module:Yesno")
local floor = math.floor
local mod = math.fmod
 
function p.main(frame, args)
local function formatMessage(secondsLeft, event, color, refreshLink)
local args = args or getArgs(frame)
local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
local timeInput = args['time'] or args[1] or error("No time string specified!")
-- Find whether we are plural or not.
local offset = args['offset'] or args[2] or ''
local isOrAre
local shorthand = yn(args['shorthand'], false)
if string.match(timeLeft, '^%d+') == '1' then
local timestamp = tonumber(frame:preprocess("{{#time:U|" .. timeInput .. ' ' .. offset .. "|}}"))
isOrAre = 'is'
local currentTime = tonumber(frame:preprocess("{{#time:U}}"))
local time_days = floor((timestamp - currentTime) / 86400)
local time_hours = floor(mod((timestamp - currentTime) / 3600, 24))
local time_minutes = floor(mod((timestamp - currentTime) / 60, 60))
local time_seconds = floor(mod((timestamp - currentTime), 60))
local isOrAreout = ''
if shorthand then
-- clip the first letter of each display
local secondDisplay, minuteDisplay, hourDisplay, dayDisplay
secondDisplay = mw.ustring.sub(mw.message.new("seconds", ""):plain(), 0, 1)
minuteDisplay = mw.ustring.sub(mw.message.new("minutes", ""):plain(), 0, 1)
hourDisplay = mw.ustring.sub(mw.message.new("hours", ""):plain(), 0, 1)
dayDisplay = mw.ustring.sub(mw.message.new("days", ""):plain(), 0, 1)
if timeToStarttime_days > 0 then
out = out .. time_days .. dayDisplay .. " "
end
if time_hours > 0 then
out = out .. time_hours .. hourDisplay .. " "
end
if time_minutes > 0 then
out = out .. time_minutes .. minuteDisplay .. " "
end
if time_seconds > 0 then
out = out .. time_seconds .. secondDisplay .. " "
end
else
if time_days > 0 then
isOrAre = 'are'
out = out .. mw.message.new("days", time_days):plain() .. " "
end
-- 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.
return string.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 = 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 nomination ends', 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 timeToEndtime_hours > 0 then
out = out .. mw.message.new("hours", time_hours):plain() .. " "
-- Event is in progress
end
return args.eventstart .. refreshLink or formatMessage(timeToEnd, (args.event or 'the event') .. ' ends', args.color, refreshLink)
if time_minutes > 0 then
else
out = out .. mw.message.new("minutes", time_minutes):plain() .. " "
-- Event had a duration and has now ended
end
return (args.eventend or ((lang:ucfirst(args.event or 'The nomination')) .. ' has ended.')) .. refreshLink
if time_seconds > 0 then
out = out .. mw.message.new("seconds", time_seconds):plain() .. " "
end
else
-- Event had no duration and has begun
return (args.eventstart or ((lang:ucfirst(args.event or 'The nomination')) .. ' has started.')) .. refreshLink
end
return frame:preprocess('<span style="' .. (args['style'] or '') .. '">' .. out .. '</span>' .. (mw.ustring.len(out) > 0 and ' [{{fullurl:{{FULLPAGENAMEE}}|action=purge}} Update])' or ''))
end