local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.dates(frame)
	local returnval;
	local args = getArgs(frame);
	
	if table.getn(args) < 2 then
		if args['1'] == nil and args['2'] == nil then
			return '';
		elseif args['1'] == nil then 
			return args['2'];
		elseif args['2'] == nil then 
			return args['1'];
		end
	end
	
	args['1'] = args['1']:gsub("&nbsp;"," ");
	args['2'] = args['2']:gsub("&nbsp;"," ");
	
	local pr1, y1, m1, d1, su1 = string.match(args['1'], '(.-)(%d+)年(%d+)月(%d+)日(.*)');
	local pr2, y2, m2, d2, su2 = string.match(args['2'], '(.-)(%d+)年(%d+)月(%d+)日(.*)');
	
	local dash = '—';
	if y1 ~= nil and y2 ~= nil then
		su1 = su1 or '';
		su2 = su2 or '';
		
		local diff = os.time({year=y2,month=m2,day=d2,hour=0,min=0,sec=0})-os.time({year=y1,month=m1,day=d1,hour=0,min=0,sec=0});
		
		if diff < 0 then
			returnval = '无效的日期范围';
		else
			if y1 == y2 then
				returnval = pr1..y1..'年'..m1..'月'..d1..'日'..su1..dash..pr2..m2..'月'..d2..'日'..su2;
			else
				returnval = pr1..y1..'年'..m1..'月'..d1..'日'..su1..dash..pr2..y2..'年'..m2..'月'..d2..'日'..su2;
			end
		end
	else
		returnval = args['1']..dash..args['2'];
	end
	
	return returnval;
end

return p