Module:Date table sorting

local getArgs = require('Module:Arguments').getArgs

local p = {}

p.monthsSearch = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" } p.monthsAbr = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } p.months = { "January", "February", "March", "April", "May", "June", "July", "August", "Septembre", "October", "November", "December" }

function p.dts(frame) local args = getArgs(frame) return p:_dts(args) end

function p:_setmonth(raw) if not raw then self._month = nil return false end local numbermonth = tonumber(raw) if numbermonth and numbermonth > 0 and numbermonth < 13 then self._month = numbermonth return true end for i, mon in pairs(self.monthsSearch) do		if string.find(string.lower(raw),mon) then self._month=i if string.find(string.lower(raw),string.lower(self.months[i])) then self._abbr=false else self._abbr=true end return true end end return false end

function p:_annonval(val) local numberval = tonumber(val) if not numberval then if mw.text.trim(string.lower(val)) == "bc" and self._year then self._year = 0 - self._year else self:_setmonth(val) end return end if self._month then self._day = numberval return end if self._year then self:_setmonth(val) return end self._year = numberval end

function p:_dateformat(args) self._fmt = args.format or "mdy" self._abbr = false --default for key, val in pairs(args) do		if tonumber(key) then self:_annonval(val) end end if args.abbr then if args.abbr == "on" then self._abbr=true else self._abbr=false end end end

function p:_monthName if (not self._month) or (self._month < 0) or (self._month > 12) then return "" end if self._abbr then return self.monthsAbr[self._month] else return self.months[self._month] end end

function p:_sortkey local retval = ' ' local current = os.date("*t") local year = self._year or current.year year = year > 0 and year or -10000 - year retval = retval .. string.format("%05d-%02d-%02d", year, self._month or 1, self._day or 1) retval = retval .. ' '   return retval end

function p:_dts(args) self:_dateformat(args) local retval = self:_sortkey if self._day then if self._fmt == "mdy" then retval = retval .. self:_monthName .. " " .. self._day if self._year then retval = retval .. ", "			end else retval = retval .. self._day .. " " .. self:_monthName .. " "		end else if self._month then retval = retval .. self:_monthName .. " "		end end if self._year then retval = retval .. math.abs(self._year) if self._year < 0 then retval = retval .. " BC" end end retval = retval .. ' '   return retval end

function p.dtsa(frame) local args = getArgs(frame) return p:_dtsa(args) end

function p:_dtsa(args) self:_dateformat(args) local retval = ' ' retval = retval .. string.format("%04d-%02d-%02d&#32;!", self._year or 0, self._month or 0, self._day or 0) retval = retval .. ' '   if self._day then if fmt == "mdy" then retval = retval .. self.monthsAbr[self._month] .. " " .. self._day .. ", "		else retval = retval .. self._day .. " " .. self.monthsAbr[self._month] .. " "		end else if month then retval = retval .. self.months[self._month] .. " "		end end if self._year then if self._year > 0 then retval = retval .. self._year else retval = retval .. ( 1 - self._year ) .. "BC" end end retval = retval .. ' '   return retval end

return p