Module:Countdown-ymd/sandbox
Appearance
![]() | This is the module sandbox page for Module:Countdown-ymd (diff). |
-- This module powers {{countdown}}.
require('Module:No globals')
local p = {}
-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
--[[--------------------------< G E T _ D A Y S _ I N _ M O N T H >--------------------------------------------
Returns the number of days in the month where month is a number 1–12 and year is four-digit Gregorian calendar.
Accounts for leap year.
]]
local function get_days_in_month (year, month)
local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
local month_length;
if (2 == month) then -- if February
month_length = 28; -- then 28 days unless
if (0 == (year%4) and (0 ~= (year%100) or 0 == (year%400))) then -- is year a leap year?
month_length = 29; -- if leap year then 29 days in February
end
else
month_length = days_in_month [month];
end
return month_length;
end
--[[--------------------------< D I F F _ T I M E >------------------------------------------------------------
s is a
]]
local function diff_time (a, b)
local diff = {}
diff.day = a.day - b.day;
if diff.day < 0 then
a.month = a.month - 1;
if a.month < 1 then -- borrow months from the year if necesssary
a.year = a.year - 1;
a.month = a.month + 12;
end
diff.day = diff.day + get_month_length (a.year, a.month);
end
diff.month = a.month - b.month;
if diff.month < 0 then -- borrows months from years
a.year = a.year - 1;
a.month = a.month + 12;
end
diff.year = a.year - b.year;
return diff;
end
--function formatMessage(secondsLeft, event, color, refreshLink)
local function formatMessage(secondsLeft, precision, event, color, refreshLink)
local timeLeft = lang:formatDuration(secondsLeft, precision)
-- 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 today = os.date ('!*t'); --fetch table of current date time parameters from the server
local event_time = {year=args.year, month=args.month, day=args.day, hour=args.hour or 0, min=args.minute, sec=args.second}
local time_til_start = diff_time (event_time, today) -- (future time - current time)
return table.concat (time_til_start, ', ');
end
return p