Database changes have finished applying - please report any issues you're (still) seeing to support@shoutwiki.com.

Module:Infobox/dates

From Eureka Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Infobox/dates/doc

 1 local getArgs = require('Module:Arguments').getArgs
 2 local p = {}
 3 
 4 function p.dates(frame)
 5 	local returnval;
 6 	local args = getArgs(frame);
 7 	
 8 	if table.getn(args) < 2 then
 9 		if args['1'] == nil and args['2'] == nil then
10 			return '';
11 		elseif args['1'] == nil then 
12 			return args['2'];
13 		elseif args['2'] == nil then 
14 			return args['1'];
15 		end
16 	end
17 	
18 	args['1'] = args['1']:gsub("&nbsp;"," ");
19 	args['2'] = args['2']:gsub("&nbsp;"," ");
20 	
21 	local dmy = false;
22 	local pr1, m1, d1, y1, su1 = string.match(args['1'], '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)');
23 	local pr2, m2, d2, y2, su2 = string.match(args['2'], '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)');
24 	if y1 == nil then
25 		dmy = true;
26 		pr1, d1, m1, y1, su1 = string.match(args['1'], '(.-)(%d%d?)%s(%a+)%s(%d+)(.*)');
27 		pr2, d2, m2, y2, su2 = string.match(args['2'], '(.-)(%d%d?)%s(%a+)%s(%d+)(.*)');
28 	end
29 	
30 	local dash = '&nbsp;– ';
31 	if y1 ~= nil and y2 ~= nil then
32 		su1 = su1 or '';
33 		su2 = su2 or '';
34 		
35 		local MONTHS = {January=1, February=2, March=3, April=4, May=5, June=6, July=7, August=8, September=9, October=10, November=11, December=12};
36 		local diff = os.time({year=y2,month=MONTHS[m2],day=d2,hour=0,min=0,sec=0})-os.time({year=y1,month=MONTHS[m1],day=d1,hour=0,min=0,sec=0});
37 		
38 		if diff < 0 then
39 			returnval = 'Invalid date range';
40 		else
41 			if y1 == y2 then
42 				if dmy == false then
43 					returnval = pr1..m1..' '..d1..su1..dash..pr2..m2..' '..d2..', '..y2..su2;
44 				else
45 					returnval = pr1..d1..' '..m1..su1..dash..pr2..d2..' '..m2..' '..y2..su2;
46 				end
47 			else
48 				if dmy == false then
49 					returnval = pr1..m1..' '..d1..', '..y1..su1..dash..pr2..m2..' '..d2..', '..y2..su2;
50 				else
51 					returnval = pr1..d1..' '..m1..' '..y1..su1..dash..pr2..d2..' '..m2..' '..y2..su2;
52 				end
53 			end
54 		end
55 	else
56 		returnval = args['1']..dash..args['2'];
57 	end
58 	
59 	return returnval;
60 end
61 
62 return p