Module:ErraCalendar

From Errapedia
Jump to navigation Jump to search

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