Module:ErraCalendar

From Errapedia
Revision as of 20:55, 9 February 2023 by LunarEcklipse (talk | contribs)
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)
-- "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)
   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 = tonumber(frame.args['year'])
   local month = tonumber(frame.args['month'])
   local date = tonumber(frame.args['date'])
   local monthstr = h.GetMonthFromInt(month)
   return monstr .. " " .. date .. ", " .. year
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