Module:DayCalc

From electowiki
Revision as of 04:18, 22 August 2023 by RobLa (talk | contribs) (Copied over from myndmess:Module:DayCalc)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for DayCalc

DayCalc may evolve into many day/date functions. Right now, it has a case of the Tuesdays.

Examples:

get_next_tuesday

  1. Returns the next Tuesday if today is not Tuesday, otherwise returns today's date.
    • wikitext: {{#invoke:DayCalc|get_next_tuesday}}
    • result: July 02, 2024
  2. 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: 2024-07-02

get_first_coming_tuesday_of_month

  1. 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
  2. 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

-- Module:NextTuesday.lua

local p = {}

function p.get_next_tuesday(frame)
    local isodate = frame.args.isodate
    local current_time = os.time()
    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 * 86400 -- 86400 seconds in a day
    
    if current_day_of_week == 2 then
        if isodate == "true" then
            return os.date("%Y-%m-%d", current_time)
        else
            return os.date("%B %d, %Y", current_time)
        end
    else
        if isodate == "true" then
            return os.date("%Y-%m-%d", next_tuesday_time)
        else
            return os.date("%B %d, %Y", next_tuesday_time)
        end
    end
end

function p.get_first_coming_tuesday_of_month(frame)
    local isodate = frame.args.isodate
    local current_time = os.time()
    local current_year = tonumber(os.date("%Y", current_time))
    local current_month = tonumber(os.date("%m", current_time))
    
    local first_day_of_month = os.time({ year = current_year, month = current_month, day = 1, hour = 0, min = 0, sec = 0 })
    local first_day_day_of_week = tonumber(os.date("%w", first_day_of_month))
    
    local days_until_first_tuesday = (9 - first_day_day_of_week) % 7
    local first_tuesday_time = first_day_of_month + days_until_first_tuesday * 86400
    
    if current_time > first_tuesday_time then
        local next_month = current_month + 1
        local next_year = current_year
        if next_month > 12 then
            next_month = 1
            next_year = next_year + 1
        end
        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)
        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)
        end
    else
        if isodate == "true" then
            return os.date("%Y-%m-%d", first_tuesday_time)
        else
            return os.date("%B %e, %Y", first_tuesday_time)
        end
    end
end

return p