Module:IxTime

From IxWiki
Revision as of 20:45, 3 August 2024 by Heku (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

local function getTemplateVar(varName)
    local frame = mw.getCurrentFrame()
    local parent = frame:getParent()
    local template = parent:expandTemplate{ title = 'IxTime' }
    local pattern = '<!%-%- IxTime:' .. varName .. '=(%d+%.?%d*) %-%-%>'
    return tonumber(string.match(template, pattern))
end

local EPOCH = getTemplateVar('epoch')
local MULTIPLIER = getTemplateVar('multiplier')

function p.convertToIxTime(frame)
    local args = frame.args
    local year = tonumber(args[1])
    local month = tonumber(args[2])
    local day = tonumber(args[3])
    local hour = tonumber(args[4] or "0")
    local minute = tonumber(args[5] or "0")
    local second = tonumber(args[6] or "0")

    if not year or not month or not day then
        return "Error: Invalid date format"
    end

    local inputTime = os.time({year=year, month=month, day=day, hour=hour, minute=minute, second=second})
    local secondsSinceEpoch = inputTime - EPOCH
    local ixTime = math.floor(secondsSinceEpoch * MULTIPLIER + EPOCH)

    local ixDate = os.date("!%A, %B %d, %Y %H:%M:%S", ixTime)
    return ixDate .. " IX"
end

return p