Module:Date

From SWGoH Wiki
Jump to navigationJump to search

Documentation for this module may be created at Module:Date/doc

local p = {}

---****************---
-- Global Variables --


--******************--
-- Global Functions --
function trim(s)
   return s:gsub("[%c%p%s]", "")
end

function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
         table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

-----------------------
-- Convert date --
function p.reformat(param)
 rawDate = param.args.date
 format = param.args.format
 dateArray = split(rawDate, "-")
 mo = tonumber(dateArray[1])
 day = dateArray[2]
 year = dateArray[3]

 months = {
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December'
}
 if format == "MDY" then
  return months[mo].." "..day..", "..year 
 elseif format == "YMD" then
   return year.." "..months[mo].." "..day
 elseif format == "DMY" then
   return day.." "..months[mo].." "..year
 else
   return "Format incorrect"
 end
end --END Function format

----------------------------------------------
-- Date Range --
function p.dateRange(param)
 rawDate = param.args.date
 type = param.args[1]
 from = param.args.startDate
 to = param.args.endDate
negative = {
[0]=12,
[-1]=11,
[-2]=20,
[-3]=9,
[-4]=8,
[-5]=7,
[-6]=6,
[-7]=5,
[-8]=4,
[-9]=3,
[-10]=2,
[-11]=1
}

 if (type == "month") then
   if (to == "today") then
     to = os.date("*t")
   end
   if (from ~= "today") then
     to.month = to.month - from
     if (to.month < 1) then
       to.month = negative[to.month]
       to.year = to.year - 1
     end
   end
   result = to.month.."-"..to.day.."-"..to.year
 end
 return result
end
return p