Module:DateInterval

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 20:15, 1 May 2023 by Vish (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:DateInterval/doc

local p = {}

local function calculateDate(date, interval)
    local currentDate = os.time()
    local startDate = os.time{year=date.year, month=date.month, day=date.day, hour=13}

    local secondsElapsed = currentDate - startDate
    local intervalsElapsed = math.floor(secondsElapsed / (86400 * interval))

    local newDateTimestamp = startDate + (intervalsElapsed * 86400 * interval)
    local newDate = os.date("*t", newDateTimestamp)

    return newDate
end

function p.dateInterval(frame)
    local args = frame:getParent().args
    local date = args.date or ""
    local interval = tonumber(args.interval) or 7

    local year, month, day = date:match("(%d%d%d%d)%-(%d%d)%-(%d%d)")
    if not year or not month or not day then
        return "Error: Invalid date format. Please use YYYY-MM-DD."
    end

    local dateTable = {year = tonumber(year), month = tonumber(month), day = tonumber(day)}
    local resultDate = calculateDate(dateTable, interval)

    local formattedDate = os.date("%B %d, %Y", os.time(resultDate))
    local time = args.time or ""
    local timezone = args.timezone or ""

    return formattedDate .. (time ~= "" and ", " .. time or "")
end

return p