Module:DayCalc: Difference between revisions
Content added Content deleted
(Copied over from myndmess:Module:DayCalc) |
(Fixing up the get_next_tuesday logic and made it use an offset so that the date calculation uses Pacific Standard Time) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
function p.get_next_tuesday(frame) |
function p.get_next_tuesday(frame) |
||
local isodate = frame.args.isodate |
local isodate = frame.args.isodate |
||
local |
local pst_tzoffset = 0 - ( 8 * 60 * 60 ) |
||
local current_time = os.time() + pst_tzoffset |
|||
local current_day_of_week = tonumber(os.date("%w", current_time)) |
local current_day_of_week = tonumber(os.date("%w", current_time)) |
||
local days_until_tuesday = (9 - current_day_of_week) % 7 |
local days_until_tuesday = (9 - current_day_of_week) % 7 |
||
⚫ | |||
⚫ | |||
local retval="UNDEFINED" |
|||
⚫ | |||
⚫ | |||
if current_day_of_week == 2 then |
if isodate == "true" and current_day_of_week == 2 then |
||
retval = os.date("%Y-%m-%d", current_time) |
|||
elseif current_day_of_week == 2 then |
|||
retval = os.date("%B %d, %Y", current_time) |
|||
⚫ | |||
elseif isodate == true then |
|||
return os.date("%B %d, %Y", current_time) |
|||
⚫ | |||
⚫ | |||
else |
else |
||
retval = os.date("%B %d, %Y", next_tuesday_time) |
|||
⚫ | |||
else |
|||
return os.date("%B %d, %Y", next_tuesday_time) |
|||
end |
|||
end |
end |
||
return retval |
|||
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 |
local current_year = tonumber(os.date("%Y")) |
||
local |
local current_month = tonumber(os.date("%m")) |
||
local |
local current_datenum = tonumber(os.date("%e")) |
||
⚫ | |||
⚫ | |||
local first_tuesday_day |
|||
⚫ | |||
⚫ | |||
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 |
|||
⚫ | |||
if current_time > first_tuesday_time then |
|||
current_year = current_year + 1 |
|||
local next_year = current_year |
|||
⚫ | |||
⚫ | |||
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" |
return os.date("%Y-%m-%d") |
||
else |
else |
||
return os.date("%B %e, %Y" |
return os.date("%B %e, %Y") |
||
end |
end |
||
end |
|||
for day = 1, 7 do |
|||
⚫ | |||
local day_of_week = tonumber(os.date("%w", day_time)) |
|||
if day_of_week == 2 then -- Tuesday |
|||
first_tuesday_day = day |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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 65: | ||
return os.date("%B %e, %Y", first_tuesday_time) |
return os.date("%B %e, %Y", first_tuesday_time) |
||
end |
end |
||
⚫ | |||
return "Unable to calculate first Tuesday of the month." |
|||
end |
end |
||
end |
end |
Latest revision as of 06:43, 20 December 2023
Documentation for DayCalc
DayCalc may evolve into many day/date functions. Right now, it has a case of the Tuesdays.
Examples:
get_next_tuesday
- Returns the next Tuesday if today is not Tuesday, otherwise returns today's date.
- wikitext:
{{#invoke:DayCalc|get_next_tuesday}}
- result: July 02, 2024
- wikitext:
- Returns the next Tuesday if today is not Tuesday, otherwise returns today's date (ISO format)
- wikitext:
{{#invoke:DayCalc|get_next_tuesday|isodate=true}}
- result: July 02, 2024
- wikitext:
get_first_coming_tuesday_of_month
- Returns the first upcoming Tuesday which is the first Tuesday of a month.
- wikitext:
{{#invoke:DayCalc|get_first_coming_tuesday_of_month}}
- result: July 2, 2024
- wikitext:
- Returns the first upcoming Tuesday which is the first Tuesday of a month (ISO format)
- wikitext:
{{#invoke:DayCalc|get_first_coming_tuesday_of_month|isodate=true}}
- result: 2024-07-02
- wikitext:
-- Module:NextTuesday.lua
local p = {}
function p.get_next_tuesday(frame)
local isodate = frame.args.isodate
local pst_tzoffset = 0 - ( 8 * 60 * 60 )
local current_time = os.time() + pst_tzoffset
local current_day_of_week = tonumber(os.date("%w", current_time))
local days_until_tuesday = (9 - current_day_of_week) % 7
local next_tuesday_time = current_time + days_until_tuesday * 24 * 60 * 60
local retval="UNDEFINED"
if isodate == "true" and current_day_of_week == 2 then
retval = os.date("%Y-%m-%d", current_time)
elseif current_day_of_week == 2 then
retval = os.date("%B %d, %Y", current_time)
elseif isodate == true then
retval = os.date("%Y-%m-%d", next_tuesday_time)
else
retval = os.date("%B %d, %Y", next_tuesday_time)
end
return retval
end
function p.get_first_coming_tuesday_of_month(frame)
local isodate = frame.args.isodate
local current_year = tonumber(os.date("%Y"))
local current_month = tonumber(os.date("%m"))
local current_datenum = tonumber(os.date("%e"))
local day_of_week = tonumber(os.date("%w"))
local first_tuesday_day
if current_datenum > 7 then
current_month = current_month + 1
if current_month > 12 then
current_month = 1
current_year = current_year + 1
end
elseif current_datenum <= 7 and day_of_week == 2 then
if isodate == "true" then
return os.date("%Y-%m-%d")
else
return os.date("%B %e, %Y")
end
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
return os.date("%Y-%m-%d", first_tuesday_time)
else
return os.date("%B %e, %Y", first_tuesday_time)
end
else
return "Unable to calculate first Tuesday of the month."
end
end
return p