Module:IxTime: Difference between revisions

From IxWiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
local p = {}
local p = {}


local function getTemplateVar(varName)
-- Ensure this matches the JavaScript epoch
    local frame = mw.getCurrentFrame()
local EPOCH = os.time({year=2020, month=10, day=4, hour=0, minute=0, second=0})
     local parent = frame:getParent()
 
     local template = parent:expandTemplate{ title = 'IxTime' }
function p.calculateIxTime(inputTime)
     local pattern = '<!%-%- IxTime:' .. varName .. '=(%d+%.?%d*) %-%-%>'
     local currentTime = inputTime or os.time()
     return tonumber(string.match(template, pattern))
     local secondsSinceEpoch = currentTime - EPOCH
     local ixTime = math.floor(secondsSinceEpoch * 4.0 + EPOCH)
     return ixTime
end
end


local EPOCH = getTemplateVar('epoch')
function p.formatIxTime(ixTime)
local MULTIPLIER = getTemplateVar('multiplier')
    -- Format the time to match the JavaScript output
    return os.date("!%A, %B %d, %Y %I:%M:%S %p", ixTime)
end


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


     if not year or not month or not day then
     if not year or not month or not day then
Line 26: Line 30:


     local inputTime = os.time({year=year, month=month, day=day, hour=hour, minute=minute, second=second})
     local inputTime = os.time({year=year, month=month, day=day, hour=hour, minute=minute, second=second})
     local secondsSinceEpoch = inputTime - EPOCH
     local ixTime = p.calculateIxTime(inputTime)
     local ixTime = math.floor(secondsSinceEpoch * MULTIPLIER + EPOCH)
     return p.formatIxTime(ixTime) .. " IX"
end


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


return p
return p

Revision as of 20:50, 3 August 2024

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

local p = {}

-- Ensure this matches the JavaScript epoch
local EPOCH = os.time({year=2020, month=10, day=4, hour=0, minute=0, second=0})

function p.calculateIxTime(inputTime)
    local currentTime = inputTime or os.time()
    local secondsSinceEpoch = currentTime - EPOCH
    local ixTime = math.floor(secondsSinceEpoch * 4.0 + EPOCH)
    return ixTime
end

function p.formatIxTime(ixTime)
    -- Format the time to match the JavaScript output
    return os.date("!%A, %B %d, %Y %I:%M:%S %p", ixTime)
end

function p.convertToIxTime(frame)
    local args = frame.args
    local year = tonumber(args[1] or args.year)
    local month = tonumber(args[2] or args.month)
    local day = tonumber(args[3] or args.day)
    local hour = tonumber(args[4] or args.hour or "0")
    local minute = tonumber(args[5] or args.minute or "0")
    local second = tonumber(args[6] or args.second 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 ixTime = p.calculateIxTime(inputTime)
    return p.formatIxTime(ixTime) .. " IX"
end

function p.getCurrentIxTime(frame)
    local ixTime = p.calculateIxTime()
    return p.formatIxTime(ixTime) .. " IX"
end

return p