Module:Calendar
From Neodyland Wiki
More actions
This documentation is transcluded from Module:Calendar/doc.
local p = {}
-- Convert "Julian day number" (jdn) to a calendar date
-- "gregorian" is a 1 for gregorian calendar and 0 for Julian
-- based on https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_day_number
function p.jdn2date(jdn, gregorian)
local f, e, g, h, year, month, day
f = jdn + 1401
if gregorian>0 then
f = f + math.floor((math.floor((4*jdn + 274277) / 146097) * 3) / 4) - 38
end
e = 4*f + 3
g = math.floor(math.fmod(e, 1461) / 4)
h = 5*g + 2
day = math.floor((math.fmod(h,153)) / 5) + 1
month = math.fmod (math.floor(h/153) + 2, 12) + 1
year = math.floor(e/1461) - y + math.floor((12 + 2 - month) / 12)
-- If year is less than 1, subtract one to convert from a zero based date system to the
-- common era system in which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.).
if year < 1 then
year = year - 1
end
return year, month, day
end
-- Convert calendar date to "Julian day number" (jdn)
-- "gregorian" is a 1 for gregorian calendar and 0 for Julian
-- based on https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_day_number
-- explanation based on http://www.cs.utsa.edu/~cs1063/projects/Spring2011/Project1/project1.html
function p.date2jdn(year, month, day, gregorian)
a = math.floor((14-month) / 12) -- will be 1 for January and February, and 0 for other months.
y = year + 4800 - a -- years since year –4800
m = month + 12*a - 3 -- month number where 10 for January, 11 for February, 0 for March, 1 for April
c = math.floor((153*m + 2)/5) -- number of days since March 1
d = 32045 -- offset so the result will be 0 for January 1, 4713 BCE
if gregorian>0 then
b = math.floor(y/4) - math.floor(y/100) + math.floor(y/400) -- number of leap years since y==0 (year –4800)
d = 32045 -- offset so the result will be 0 for January 1, 4713 BCE
else
b = math.floor(y/4) -- number of leap years since y==0 (year –4800)
d = 32083 -- offset so the result will be 0 for January 1, 4713 BCE
end
return day + c + 365*y + b - d
end
-- Convert a date from Gregorian to Julian calendar
function p.Gregorian2Julian(year, month, day)
return jdn2date(date2jdn(year, month, day, 1), 0)
end
-- Convert a date from Julian to Gregorian calendar
function p.Julian2Gregorian(year, month, day)
return jdn2date(date2jdn(year, month, day, 0), 1)
end
-- Return day of week based on gregorian date. Mon->1, Tue->2, ..., Sun->7
function p.DayOfWeek(year, month, day)
return math.fmod(date2jdn(year, month, day, 1), 7) + 1
end
return p;