Module:ErraCalendar: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
| (14 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
-- "datatype" = "date" | -- "datatype" = "date" | ||
-- "year" = int (default 0, range 0-inf) | -- "year" = int (default 0, range 0-inf) | ||
-- "month" = int (default 1, range 1-9) | -- "month" = int (default 1, range 1-9, range dynamically determined by contents of Module:ErraCalendar/Months) | ||
-- "date" = int (default 1, range 1-25) | -- "date" = int (default 1, range 1-25) | ||
-- "hour" = int (default 0, range 1-23) | -- "hour" = int (default 0, range 1-23) | ||
| Line 23: | Line 23: | ||
local h = {} | local h = {} | ||
MonthTable = | local MonthTable = mw.loadData("Module:ErraCalendar/Months") | ||
local CurrentDate = mw.loadData("Module:ErraCalendar/CurrentDate") | |||
local MonthTableLength = table.getn(MonthTable) -- We calculate this here in case it's changed within the module. | |||
function h.GetMonthFromInt( inInt ) | function h.GetMonthFromInt( inInt ) | ||
| Line 43: | Line 45: | ||
function h.ConstructDateTime(inyear, inmonth, indate) | function h.ConstructDateTime(inyear, inmonth, indate) | ||
inyear = tonumber(inyear) | |||
inmonth = tonumber(inmonth) | |||
indate = tonumber(indate) | |||
if inyear == nil then | if inyear == nil then | ||
inyear = 0 | inyear = 0 | ||
| Line 55: | Line 60: | ||
inmonth = 1 | inmonth = 1 | ||
end | end | ||
if inmonth > | if inmonth > MonthTableLength then | ||
inmonth = | inmonth = MonthTableLength | ||
end | end | ||
if indate == nil then | if indate == nil then | ||
| Line 74: | Line 79: | ||
local extrayears = 0 | local extrayears = 0 | ||
local newdate = date1["date"] | local newdate = date1["date"] - date2["date"] | ||
if newdate < 1 then | if newdate < 1 then | ||
| Line 91: | Line 96: | ||
end | end | ||
local newmonth = date1["month"] | local newmonth = date1["month"] - date2["month"] | ||
newmonth = newmonth + extramonths | newmonth = newmonth + extramonths | ||
| Line 97: | Line 102: | ||
while newdate < 1 do | while newdate < 1 do | ||
extrayears = extrayears - 1 | extrayears = extrayears - 1 | ||
newmonth = newmonth + | newmonth = newmonth + MonthTableLength | ||
if newmonth >= 1 then break end | if newmonth >= 1 then break end | ||
end | end | ||
end | end | ||
if newmonth > | if newmonth > MonthTableLength then | ||
while newmonth > | while newmonth > MonthTableLength do | ||
extrayears = extrayears + 1 | extrayears = extrayears + 1 | ||
newmonth = newmonth - | newmonth = newmonth - MonthTableLength | ||
if newmonth <= | if newmonth <= MonthTableLength then break end | ||
end | end | ||
end | end | ||
local newyear = date1["year"] | local newyear = date1["year"] - date2["year"] | ||
newyear = newyear + extrayears | newyear = newyear + extrayears | ||
return {datatype="timediff", year=newyear, month=newmonth, date=newdate} | return {datatype="timediff", year=newyear, month=newmonth, date=newdate} | ||
| Line 130: | Line 135: | ||
local date = nil | local date = nil | ||
if frame.args['date'] ~= nil then | if frame.args['date'] ~= nil then | ||
tonumber(frame.args['date']) | date = tonumber(frame.args['date']) | ||
end | end | ||
if date ~= nil then | if date ~= nil then | ||
return | return monthstr .. " " .. date .. ", " .. year | ||
end | end | ||
if month ~= nil then | if month ~= nil then | ||
return | if monthstr ~= 0 then | ||
return monthstr .. " " .. year | |||
end | |||
return year | |||
end | end | ||
if year ~= nil then | if year ~= nil then | ||
| Line 149: | Line 157: | ||
local difference = h.GetDifferenceBetweenTwoDates(dt1, dt2) | local difference = h.GetDifferenceBetweenTwoDates(dt1, dt2) | ||
if difference['year'] < 0 then | if difference['year'] < 0 then | ||
difference['year'] = difference['year'] / -1 | |||
end | |||
if difference['year'] == 1 then | |||
return difference['year'] .. " year old" | |||
end | end | ||
return difference['year'] .. " years old" | return difference['year'] .. " years old" | ||
end | |||
function p.PrintAgeFromToday( frame ) | |||
local dt1 = h.ConstructDateTime(frame.args['year1'], frame.args['month1'], frame.args['date1']) | |||
local difference = h.GetDifferenceBetweenTwoDates(dt1, CurrentDate) | |||
if difference['year'] < 0 then | |||
difference['year'] = difference['year'] / -1 | |||
end | |||
if difference['year'] == 1 then | |||
return difference['year'] .. " year old" | |||
end | |||
return difference['year'] .. " years old" | |||
end | |||
function p.PrintHowLongAgoFromToday( frame ) | |||
local dt1 = h.ConstructDateTime(frame.args['year'], frame.args['month'], frame.args['date']) | |||
local difference = h.GetDifferenceBetweenTwoDates(dt1, CurrentDate) | |||
if difference['year'] < 0 then | |||
difference['year'] = difference['year'] / -1 | |||
end | |||
if difference['year'] == 1 then | |||
return difference['year'] .. " year ago" | |||
end | |||
return difference['year'] .. " years ago" | |||
end | end | ||
return p | return p | ||
Latest revision as of 14:59, 27 March 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, range dynamically determined by contents of Module:ErraCalendar/Months)
-- "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 = {}
local MonthTable = mw.loadData("Module:ErraCalendar/Months")
local CurrentDate = mw.loadData("Module:ErraCalendar/CurrentDate")
local MonthTableLength = table.getn(MonthTable) -- We calculate this here in case it's changed within the module.
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 > MonthTableLength then
inmonth = MonthTableLength
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 + MonthTableLength
if newmonth >= 1 then break end
end
end
if newmonth > MonthTableLength then
while newmonth > MonthTableLength do
extrayears = extrayears + 1
newmonth = newmonth - MonthTableLength
if newmonth <= MonthTableLength 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
if monthstr ~= 0 then
return monthstr .. " " .. year
end
return 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
if difference['year'] == 1 then
return difference['year'] .. " year old"
end
return difference['year'] .. " years old"
end
function p.PrintAgeFromToday( frame )
local dt1 = h.ConstructDateTime(frame.args['year1'], frame.args['month1'], frame.args['date1'])
local difference = h.GetDifferenceBetweenTwoDates(dt1, CurrentDate)
if difference['year'] < 0 then
difference['year'] = difference['year'] / -1
end
if difference['year'] == 1 then
return difference['year'] .. " year old"
end
return difference['year'] .. " years old"
end
function p.PrintHowLongAgoFromToday( frame )
local dt1 = h.ConstructDateTime(frame.args['year'], frame.args['month'], frame.args['date'])
local difference = h.GetDifferenceBetweenTwoDates(dt1, CurrentDate)
if difference['year'] < 0 then
difference['year'] = difference['year'] / -1
end
if difference['year'] == 1 then
return difference['year'] .. " year ago"
end
return difference['year'] .. " years ago"
end
return p