模組:沙盒/cwek/test

local p={}
local getArgs

function isDate(inputStr)
	if inputStr==nil or inputStr=='' then 
		return false
	end
	
	local pass=true
	local model = {'(%d%d%d%d)年(%d%d?)月(%d%d?)日','(%d%d%d%d)/(%d%d?)/(%d%d?)','(%d%d%d%d)(%d%d)(%d%d)'}
	local year,month,day=nil,nil,nil
	for _,_model in ipairs( model ) do
		year,month,day=nil,nil,nil
    	for y,m,d in mw.ustring.gmatch(inputStr,model) do
		  year,month,day=y,m,d
		  break
    	end
		if year~=nil and month~=nil and day~=nil then
			year,month,day=tonumber(year),tonumber(month),tonumber(day)
			pass=true
			break
		else
			pass=false
		end
	end
	
	if pass then
		if year <=2001 then
			return false
		else
			if (1<=month and month<=12) and day >=1 then
				local m1={[1]=true,[3]=true,[5]=true,[7]=true,[8]=true,[10]=true,[12]=true}
				local m2={[4]=true,[6]=true,[9]=true,[11]=true}
				local leapyear=((year %4 ==0) and (year %100 ~=0 )) or (year%400==0)
				if (m1[month] and day<=31) or 
				   (m2[month] and day<=30) or 
				   (month==2 and ((leapyear and day<=29) or (day<=28 and not leapyear)) )
				then
					return 	year,month,day
				else 
					return false
				end
			else
				return false
			end
		end	
	else
		return false
	end
end

function p.main(frames)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {parentFirst=true})
	
	return isDate(args['1'])
end

return p