Module:IxTime: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 3: | Line 3: | ||
local function getTemplateVar(varName) | local function getTemplateVar(varName) | ||
local frame = mw.getCurrentFrame() | 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 | end | ||
local EPOCH = getTemplateVar(' | local EPOCH = getTemplateVar('epoch') | ||
local MULTIPLIER = getTemplateVar(' | local MULTIPLIER = getTemplateVar('multiplier') | ||
function p.convertToIxTime(frame) | function p.convertToIxTime(frame) |
Revision as of 19:45, 3 August 2024
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