Module:Calendar date/recurring

--[[ 

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('strict')

local p = {}

function p.calculate(frame)
	local args = frame.args

	local ONE_DAY = 86400 -- number of seconds in one day
	local year = tonumber(args.year)
	local month = tonumber(args.month)
	local weeknumber = tonumber(args.weeknumber)
	local dayofweek = tonumber(args.dayofweek)

	local date = os.time{year=year, month=month, day=1}
	local dateparts = os.date("*t", date)
	
	if weeknumber > 0 then
		-- find the first [dayofweek] of this month
		while dateparts["wday"] ~= dayofweek do
			date = date + ONE_DAY
			dateparts = os.date("*t", date)
		end

		-- add the correct number of weeks
		if weeknumber > 1 then
			date = date + ((weeknumber - 1) * (7 * ONE_DAY))
		end
	else
		-- find the first day of the next month
		while dateparts["month"] == month do
			date = date + ONE_DAY
			dateparts = os.date("*t", date)
		end

		-- go back one day to get the last day of the month we want
		date = date - ONE_DAY
		dateparts = os.date("*t", date)

		-- go backwards until we find the right day of week
		while dateparts["wday"] ~= dayofweek do
			date = date - ONE_DAY
			dateparts = os.date("*t", date)
		end
	end
	local result = os.date("%Y-%m-%d", date)
	return result

end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.