Module:IxTime: Difference between revisions
Jump to navigation
Jump to search
Created page with "local p = {} local EPOCH = os.time({year=2020, month=10, day=4, hour=0, minute=0, second=0}) 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 f..." |
mNo edit summary |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Ensure this matches the JavaScript epoch | |||
local EPOCH = os.time({year=2020, month=10, day=4, hour=0, minute=0, second=0}) | local EPOCH = os.time({year=2020, month=10, day=4, hour=0, minute=0, second=0}) | ||
function p.convertToIxTime(frame) | function p.convertToIxTime(inputTime) | ||
if type(inputTime) == "table" then | |||
inputTime = os.time(inputTime) | |||
end | |||
local secondsSinceEpoch = inputTime - EPOCH | |||
local ixSeconds = secondsSinceEpoch / 4.0 -- Divide by 4 to slow down time before epoch | |||
return EPOCH + ixSeconds | |||
end | |||
function p.formatIxTime(ixTime, includeTime) | |||
local ixDate = os.date("!*t", ixTime) | |||
local months = { | |||
"January", "February", "March", "April", "May", "June", | |||
"July", "August", "September", "October", "November", "December" | |||
} | |||
local weekdays = { | |||
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" | |||
} | |||
if includeTime then | |||
return string.format("%s, %s %d, %d %02d:%02d:%02d (ILT)", | |||
weekdays[ixDate.wday], | |||
months[ixDate.month], | |||
ixDate.day, | |||
ixDate.year, | |||
ixDate.hour, | |||
ixDate.min, | |||
ixDate.sec | |||
) | |||
else | |||
return string.format("%s, %s %d, %d (ILT)", | |||
weekdays[ixDate.wday], | |||
months[ixDate.month], | |||
ixDate.day, | |||
ixDate.year | |||
) | |||
end | |||
end | |||
function p.convertDateToIxTime(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 | local hour = tonumber(args[4] or args.hour or 0) | ||
local minute = tonumber(args[5] or | local minute = tonumber(args[5] or args.minute or 0) | ||
local second = tonumber(args[6] or | local second = tonumber(args[6] or args.second or 0) | ||
local includeTime = args[7] or args.includeTime | |||
if not year or not month or not day then | if not year or not month or not day then | ||
return "Error: Invalid date format" | return "Error: Invalid date format. Please provide at least year, month, and day." | ||
end | end | ||
local inputTime = | local inputTime = {year=year, month=month, day=day, hour=hour, min=minute, sec=second} | ||
local | local ixTime = p.convertToIxTime(inputTime) | ||
return p.formatIxTime(ixTime, includeTime == "yes") | |||
end | |||
local | function p.getCurrentIxTime(frame) | ||
return | local includeTime = frame.args[1] or frame.args.includeTime | ||
local currentTime = os.time() | |||
local ixTime = p.convertToIxTime(currentTime) | |||
return p.formatIxTime(ixTime, includeTime == "yes") | |||
end | end | ||
return p | return p |
Latest revision as of 20:33, 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.convertToIxTime(inputTime)
if type(inputTime) == "table" then
inputTime = os.time(inputTime)
end
local secondsSinceEpoch = inputTime - EPOCH
local ixSeconds = secondsSinceEpoch / 4.0 -- Divide by 4 to slow down time before epoch
return EPOCH + ixSeconds
end
function p.formatIxTime(ixTime, includeTime)
local ixDate = os.date("!*t", ixTime)
local months = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
}
local weekdays = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
}
if includeTime then
return string.format("%s, %s %d, %d %02d:%02d:%02d (ILT)",
weekdays[ixDate.wday],
months[ixDate.month],
ixDate.day,
ixDate.year,
ixDate.hour,
ixDate.min,
ixDate.sec
)
else
return string.format("%s, %s %d, %d (ILT)",
weekdays[ixDate.wday],
months[ixDate.month],
ixDate.day,
ixDate.year
)
end
end
function p.convertDateToIxTime(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)
local includeTime = args[7] or args.includeTime
if not year or not month or not day then
return "Error: Invalid date format. Please provide at least year, month, and day."
end
local inputTime = {year=year, month=month, day=day, hour=hour, min=minute, sec=second}
local ixTime = p.convertToIxTime(inputTime)
return p.formatIxTime(ixTime, includeTime == "yes")
end
function p.getCurrentIxTime(frame)
local includeTime = frame.args[1] or frame.args.includeTime
local currentTime = os.time()
local ixTime = p.convertToIxTime(currentTime)
return p.formatIxTime(ixTime, includeTime == "yes")
end
return p