Difference between revisions of "Module:Calendar date/recurring"
Jump to navigation
Jump to search
blackwiki>Howcheng (←Created page with '--[[ Calculates the Gregorian date of a recurring holiday that varies year-to-year, but follows the rule "Nth [day of week] of [month]" "month" = month numb...') |
blackwiki>Howcheng |
||
| Line 19: | Line 19: | ||
local ONE_DAY = 86400 -- number of seconds in one day | local ONE_DAY = 86400 -- number of seconds in one day | ||
| − | local date = os.time{year=args.year, month=args.month, day=1} | + | local date = os.time{year=tonumber(args.year), month=tonumber(args.month), day=1} |
local dateparts = os.date("*t", date) | local dateparts = os.date("*t", date) | ||
-- find the first [dayofweek] of this month | -- find the first [dayofweek] of this month | ||
| − | while not dateparts["wday"] == | + | local dayofweek = tonumber(args.dayofweek) |
| + | while not dateparts["wday"] == dayofweek do | ||
date = date + ONE_DAY | date = date + ONE_DAY | ||
dateparts = os.date("*t", date) | dateparts = os.date("*t", date) | ||
| Line 29: | Line 30: | ||
-- add the correct number of weeks | -- add the correct number of weeks | ||
| + | local weeknumber = tonumber(args.weeknumber) | ||
if weeknumber > 1 then | if weeknumber > 1 then | ||
date = date + ((weeknumber - 1) * (7 * ONE_DAY)) | date = date + ((weeknumber - 1) * (7 * ONE_DAY)) | ||
Revision as of 23:13, 26 March 2019
Documentation for this module may be created at Module:Calendar date/recurring/doc
--[[
Calculates the Gregorian date of a recurring holiday that varies year-to-year, but follows the rule "Nth [day of week] of [month]"
"month" = month number (1 to 12)
"weeknumber" = number of week (1 to 4, or -1 to mean "last")
"dayofweek" = number that represents the day of the week, where 1 = Sunday and 7 = Saturday
"year" = Gregorian calendar year
]]
require('Module:No globals')
local p = {}
function p.calculate(frame)
local pframe = frame:getParent()
local args = pframe.args
local ONE_DAY = 86400 -- number of seconds in one day
local date = os.time{year=tonumber(args.year), month=tonumber(args.month), day=1}
local dateparts = os.date("*t", date)
-- find the first [dayofweek] of this month
local dayofweek = tonumber(args.dayofweek)
while not dateparts["wday"] == dayofweek do
date = date + ONE_DAY
dateparts = os.date("*t", date)
end
-- add the correct number of weeks
local weeknumber = tonumber(args.weeknumber)
if weeknumber > 1 then
date = date + ((weeknumber - 1) * (7 * ONE_DAY))
end
local result = os.time("%Y-%m-%d", date)
return result
end
return p