Module:Date table sorting

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

local p = {}

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 = 0 return end if tonumber(raw) then self._month = tonumber(raw) return end for i, mon in pairs(monthAbr) do		if string.find(raw,mon) then self._month=i if string.find(raw,months[i]) then self._abbr=true else self._abbr=false end end end end

function p:_dateformat(args) self._fmt = args.format or "mdy" self._abbr = false --default local monthArg if args[2] and not args[3] then self:_setmonth(args[1]) self._day = tonumber(args[2]) else self._year = tonumber(args[1]) self:_setmonth(args[2]) self._day = tonumber(args[3]) if args[4] then --bc self._year = 1 - self._year 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 (self._month==nil) or (self._month < 0) or (self._month > 12) then return nil end if self.abbr then return self.monthsAbr[self._month] else return self.months[self._month] end end

function p:_dts(args) self:_dateformat(args) local retval = ' ' retval = retval .. string.format("%05d-%02d-%02d", self._year or 0, self._month or 0, self._day or 0) retval = retval .. ' '   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 if self._year > 0 then retval = retval .. self._year else retval = retval .. ( 1 - self._year ) .. "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