Modul:GregorianSerialDate

Documentația acestui modul poate fi creată la Modul:GregorianSerialDate/doc

local p = {}
local getArgs = require('Modul:Arguments').getArgs

p.getGregorianSerialDate = function(year, month, day)
	local ret = 0
	ret = ret + ((year - 1) * 365)
	ret = ret + (((year - 1) - ((year - 1) % 4)) / 4) --add a day for every leap
	ret = ret - (((year - 1) - ((year - 1) % 100)) / 100) --subtract 100 year exception
	ret = ret + (((year - 1) - ((year - 1) % 400)) / 400) --readd 400 year exception
	
	-- days so far this year:
	if month < 9 then
		ret = ret + math.floor((month - 1) * 30.5 + 0.5)
	else
		ret = ret + math.floor((month - 1) * 30.5 + 1.4)
	end
	
	-- after February, account for leap day
	if month > 2 then
		if year % 4 == 0 and year % 100 ~= 0 then
			ret = ret - 1
		else
			ret = ret - 2
		end
		if year % 400 == 0 then
			ret = ret + 1
		end
	end
	ret = ret + day
	return ret
end

p.getGregorianSerialDateFromFrame = function(frame)
	local args = getArgs(frame)
	local crtDate = os.date("*t")
	local year = tonumber(args['year'] or crtDate.year)
	local month = tonumber(args['month'] or crtDate.month)
	local day = tonumber(args['day'] or crtDate.day)
	
	return tostring(p.getGregorianSerialDate(year, month, day))
end

return p