Module:ErraCalendar: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
(Undo revision 187 by LunarEcklipse (talk)) Tag: Undo |
||
| Line 127: | Line 127: | ||
month = tonumber(frame.args['month']) | month = tonumber(frame.args['month']) | ||
monthstr = h.GetMonthFromInt(month) | monthstr = h.GetMonthFromInt(month) | ||
if monthstr == nil then | |||
monthstr = month | |||
end | |||
end | end | ||
local date = nil | local date = nil | ||
Revision as of 22:46, 9 February 2023
Documentation for this module may be created at Module:ErraCalendar/doc
-- Date Table Structure
-- "datatype" = "date"
-- "year" = int (default 0, range 0-inf)
-- "month" = int (default 1, range 1-9)
-- "date" = int (default 1, range 1-25)
-- "hour" = int (default 0, range 1-23)
-- "minute" = int (default 0, range 0-59)
-- "second" = int (default 0, range 0-59)
--
-- Timedelta Table Structure
-- "datatype = "timedelta"
-- "year" = int
-- "month" = int
-- "date" = int
--
-- Timediff Table Structure
-- "datatype = "timediff"
-- "year" = int
-- "month" = int
-- "date" = int
local p = {}
local h = {}
MonthTable = {{name="Een", season="Rise"}, {name="Kar", season="Rise"}, {name="Mir", season="Rise"}, {name="Rort", season="Life"}, {name="Kir", season="Life"}, {name="Sur", season="Life"}, {name="Ith", season="Low"}, {name="Trint", season="Low"}, {name="Sorth", season="Low"}}
function h.GetMonthFromInt( inInt )
if MonthTable[inInt] == nil then
return nil
end
return MonthTable[inInt]["name"]
end
function h.GetSeasonFromMonthStr( inStr )
inStr = inStr:lower()
for _, v in pairs(MonthTable) do
if v["name"]:lower() == inStr then
return v["season"]
end
end
return nil
end
function h.ConstructDateTime(inyear, inmonth, indate)
inyear = tonumber(inyear)
inmonth = tonumber(inmonth)
indate = tonumber(indate)
if inyear == nil then
inyear = 0
end
if inyear < 0 then
inyear = 0
end
if inmonth == nil then
inmonth = 1
end
if inmonth < 1 then
inmonth = 1
end
if inmonth > 9 then
inmonth = 9
end
if indate == nil then
indate = 1
end
if indate < 1 then
indate = 1
end
if indate > 25 then
indate = 25
end
return {datatype="date", year=inyear, month=inmonth, date=indate}
end
function h.GetDifferenceBetweenTwoDates(date1, date2)
local extramonths = 0
local extrayears = 0
local newdate = date1["date"] - date2["date"]
if newdate < 1 then
while newdate < 1 do
extramonths = extramonths - 1
newdate = newdate + 25
if newdate >= 1 then break end
end
end
if newdate > 25 then
while newdate > 25 do
extramonths = extramonths + 1
newdate = newdate - 25
if newdate <= 25 then break end
end
end
local newmonth = date1["month"] - date2["month"]
newmonth = newmonth + extramonths
if newmonth < 1 then
while newdate < 1 do
extrayears = extrayears - 1
newmonth = newmonth + 9
if newmonth >= 1 then break end
end
end
if newmonth > 9 then
while newmonth > 9 do
extrayears = extrayears + 1
newmonth = newmonth - 9
if newmonth <= 9 then break end
end
end
local newyear = date1["year"] - date2["year"]
newyear = newyear + extrayears
return {datatype="timediff", year=newyear, month=newmonth, date=newdate}
end
function p.PrintDate( frame )
local year = nil
if frame.args['year'] ~= nil then
year = tonumber(frame.args['year'])
end
local month = nil
local monthstr = nil
if frame.args['month'] ~= nil then
month = tonumber(frame.args['month'])
monthstr = h.GetMonthFromInt(month)
if monthstr == nil then
monthstr = month
end
end
local date = nil
if frame.args['date'] ~= nil then
date = tonumber(frame.args['date'])
end
if date ~= nil then
return monthstr .. " " .. date .. ", " .. year
end
if month ~= nil then
return monthstr .. " " .. year
end
if year ~= nil then
return year
end
return ""
end
function p.PrintAge( frame )
local dt1 = h.ConstructDateTime(frame.args['year1'], frame.args['month1'], frame.args['date1'])
local dt2 = h.ConstructDateTime(frame.args['year2'], frame.args['month2'], frame.args['date2'])
local difference = h.GetDifferenceBetweenTwoDates(dt1, dt2)
if difference['year'] < 0 then
difference['year'] = difference['year'] / -1
end
return difference['year'] .. " years old"
end
return p