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, {removeBlanks = false}) 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, dayfirst) local numberval if val then numberval = tonumber(mw.text.trim(val,"%s%t,")) end if (not val) or (type(val)=="table") or (mw.text.trim(val)=="") then numberval = 0 end if not numberval then if mw.text.trim(string.lower(val)) == "bc" then if (not self._year) then self._year = self._day self._day = nil end if self._year then self._year = 0 - self._year end else if self:_setmonth(val,dayfirst) and dayfirst and self._year and (not self._day) and (self._year > 0) and (self._year<31) then self._day = self._year self._year = nil self._fmt = "dmy" end end return end if self._month and (not self._day) and (numberval < 32) and (numberval > 0) then self._day = numberval return end if self._year and (not self._month) then self._month = numberval return end if (not self._year) then self._year = numberval return end end

function p:_dateformat(args) self._fmt = "mdy" self._abbr = false --default if args[1] and (not args[2]) then for _, val in pairs(mw.text.split(args[1],"[%s/-]")) do			self:_annonval(val, true) end else for key, val in pairs(args) do			if tonumber(key) then self:_annonval(val) end end end if args.format then self._fmt = args.format end if args.abbr then if args.abbr == "on" then self._abbr=true else self._abbr=false end end if (self._year==0) then --not valid. placeholder for no-year self._year = nil 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-%02d%02d", year, self._month or 1, self._day or 1, 0, 0) 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 if self._month then retval = retval .. " "		end 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, {removeBlanks = false}) 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