コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

利用者:Burthsceh/サンドボックス/モジュール:Checkdate

local ustr = mw.ustring local usub = ustr.sub local t = {

   ["####年#月"] = function (timestamp, default)
       return timestamp
   end,
   ["####年##月"] = function (timestamp, default)
       local n = tonumber(usub(timestamp, 6, 7))
       if 1 <= n and n <= 12 then
           return usub(timestamp, 1, 4) .. "年" .. tostring(n) .. "月"
       end
       return default
   end,
   ["####-#月"] = function (timestamp, default)
       return usub(timestamp, 1, 4) .. "年" .. usub(timestamp, 6, 6) .. "月"
   end,
   ["####-##月"] = false,
   ["####-#"] = false,
   ["####-#-#"] = false,
   ["####-#-##"] = false,
   ["####-##"] = false,
   ["####-##-#"] = false,
   ["####-##-##"] = false,
   ["#####"] = function (timestamp, default)
       return usub(timestamp, 1, 4) .. "年" .. usub(timestamp, 5, 5) .. "月"
   end,
   ["######"] = function (timestamp, default)
       local n = tonumber(usub(timestamp, 5, 6))
       if 1 <= n and n <= 12 then
           return usub(timestamp, 1, 4) .. "年" .. tostring(n) .. "月"
       end
       return default
   end,
   ["january ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年1月"
   end,
   ["february ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年2月"
   end,
   ["march ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年3月"
   end,
   ["april ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年4月"
   end,
   ["may ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年5月"
   end,
   ["june ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年6月"
   end,
   ["july ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年7月"
   end,
   ["august ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年8月"
   end,
   ["september ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年9月"
   end,
   ["october ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年10月"
   end,
   ["november ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年11月"
   end,
   ["december ####"] = function (timestamp, default)
       return usub(timestamp, -4) .. "年12月"
   end,
   ["january, ####"] = false,
   ["february, ####"] = false,
   ["march, ####"] = false,
   ["april, ####"] = false,
   ["may, ####"] = false,
   ["june, ####"] = false,
   ["july, ####"] = false,
   ["august, ####"] = false,
   ["september, ####"] = false,
   ["october, ####"] = false,
   ["november, ####"] = false,
   ["december, ####"] = false,

}


t["####-##月"] = t["####年##月"] t["####-#"] = t["####-#月"] t["####-#-#"] = t["####-#月"] t["####-#-##"] = t["####-#月"] t["####-##"] = t["####年##月"] t["####-##-#"] = t["####年##月"] t["####-##-##"] = t["####年##月"] t["january, ####"] = t["january ####"] t["february, ####"] = t["february ####"] t["march, ####"] = t["march ####"] t["april, ####"] = t["april ####"] t["may, ####"] = t["may ####"] t["june, ####"] = t["june ####"] t["july, ####"] = t["july ####"] t["august, ####"] = t["august ####"] t["september, ####"] = t["september ####"] t["october, ####"] = t["october ####"] t["november, ####"] = t["november ####"] t["december, ####"] = t["december ####"]


local function checkdate(timestamp, default)

   if ustr.match(timestamp, "#") then
       return default
   end
   local r = ustr.lower(ustr.gsub(timestamp, "[0-9]", "#"))
   local f = t[r]
   if f then
       return f(timestamp, default)
   else
       if ustr.match(r, "^####年#月") then
           return usub(timestamp, 1, 7)
       end
       if ustr.match(r, "^####[年%-]##月") then
           return t["####年##月"](timestamp, default)
       end
       if ustr.match(r, "^####%-#月") then
           return t["####-#月"](timestamp, default)
       end
   end
   return default

end


local trim = mw.text.trim local p = {

   checkdate = function (timestamp, default)
       return checkdate(timestamp, default or "")
   end,
   checkdatecurrent = function (frame)
       local timestamp = trim(frame.args[1] or "")
       local default = trim(frame.args.default or "")
       return checkdate(timestamp, default)
   end,
   checkdateparent = function (frame)
       local args = frame:getParent().args
       local timestamp = trim(args[1] or "")
       local default = trim(args.default or args.defaultdate or "")
       return checkdate(timestamp, default)
   end,

}

return p