Module:DayCalc: Difference between revisions

Content added Content deleted
(Copied over from myndmess:Module:DayCalc)
 
(Fixed algorithm so that the next "first Tuesday" will be September 5.)
Line 25: Line 25:
end
end
end
end



function p.get_first_coming_tuesday_of_month(frame)
function p.get_first_coming_tuesday_of_month(frame)
local isodate = frame.args.isodate
local isodate = frame.args.isodate
local current_time = os.time()
local current_year = tonumber(os.date("%Y"))
local current_year = tonumber(os.date("%Y", current_time))
local current_month = tonumber(os.date("%m"))
local current_month = tonumber(os.date("%m", current_time))
local current_datenum = tonumber(os.date("%e"))
local day_of_week = tonumber(os.date("%w"))

local first_day_of_month = os.time({ year = current_year, month = current_month, day = 1, hour = 0, min = 0, sec = 0 })
local first_tuesday_day
local first_day_day_of_week = tonumber(os.date("%w", first_day_of_month))

if current_datenum > 7 then
local days_until_first_tuesday = (9 - first_day_day_of_week) % 7
current_month = current_month + 1
local first_tuesday_time = first_day_of_month + days_until_first_tuesday * 86400
if current_month > 12 then
current_month = 1
if current_time > first_tuesday_time then
local next_month = current_month + 1
current_year = current_year + 1
local next_year = current_year
if next_month > 12 then
next_month = 1
next_year = next_year + 1
end
end
elseif current_datenum <= 7 and day_of_week == 2 then
if isodate == "true" then
if isodate == "true" then
return os.date("%Y-%m-%d", os.time({ year = next_year, month = next_month, day = 1, hour = 0, min = 0, sec = 0 }) + days_until_first_tuesday * 86400)
return os.date("%Y-%m-%d")
else
else
return os.date("%B %e, %Y", os.time({ year = next_year, month = next_month, day = 1, hour = 0, min = 0, sec = 0 }) + days_until_first_tuesday * 86400)
return os.date("%B %e, %Y")
end
end
else
end

for day = 1, 7 do
local day_time = os.time({ year = current_year, month = current_month, day = day, hour = 0, min = 0, sec = 0 })
local day_of_week = tonumber(os.date("%w", day_time))
if day_of_week == 2 then -- Tuesday
first_tuesday_day = day
break
end
end

if first_tuesday_day then
local first_tuesday_time = os.time({ year = current_year, month = current_month, day = first_tuesday_day, hour = 0, min = 0, sec = 0 })

if isodate == "true" then
if isodate == "true" then
return os.date("%Y-%m-%d", first_tuesday_time)
return os.date("%Y-%m-%d", first_tuesday_time)
Line 56: Line 67:
return os.date("%B %e, %Y", first_tuesday_time)
return os.date("%B %e, %Y", first_tuesday_time)
end
end
else
return "Unable to calculate first Tuesday of the month."
end
end
end
end