Module:Time/sandbox: Difference between revisions

Content deleted Content added
No edit summary
Undid revision 853960208 by BrandonXLF (talk)
Line 828:
end
df = args.df or args[2] or tz[args[1]].df or ''; -- template |df= overrides typical df from tz properties TODO: error check these values?
if is_set (df) then
df = df:lower(); -- lower case because we will compare to lower case values later
end
 
hf = df:match ('%l%ly(12)'); -- hf == '12' selects 12-hour AM/PM display; other values ignored and 24 hour clock time displayed
if is_set (hf) then
df = df:match ('(%l%ly)12'); -- extract df
end
 
if is_set (args._TEST_TIME_) then -- typically used to test the code at a specific utc time
local test_time = get_test_time (args._TEST_TIME_);
if not test_time then
return '<span style="font-size:100%" class="error">{{time}} – malformed or incomplete _TEST_TIME_ ([[Template:Time#Error messages|help]])</span>';
end
 
-- utc_timestamp = os.time(get_test_time (args._TEST_TIME_));
utc_timestamp = os.time(test_time);
else
utc_timestamp = os.time (); -- get current server time (UTC)
end
utc_offset = get_utc_offset (args[1]); -- utc offset for specified timezone
utc_offset_2 = get_utc_offset_2 (args[1]);
utc_offset_3 = get_utc_offset_3 (args[1]);
timestamp = utc_timestamp + utc_offset; -- make local time timestamp
 
if 'no' == args.dst then -- for timezones that DO observe dst but for this ___location ...
return string.format ('%s:%s', utc_offset_2, utc_offset_3);
tz_abbr = tz[args[1]].abbr; -- ... dst is not observed (|dst=no) show time as standard time
else
if is_set (tz[args[1]].dst_begins) and is_set (tz[args[1]].dst_ends) and is_set (tz[args[1]].dst_time) then -- make sure we have all of the parts
dst_begin_ts, dst_end_ts, invert = make_dst_timestamps (timestamp, args[1]); -- get begin and end dst timestamps and invert flag
 
if nil == dst_begin_ts or nil == dst_end_ts then
return '<span style="font-size:100%" class="error">{{time}} – error calculating dst timestamps ([[Template:Time#Error messages|help]])</span>';
end
if invert then -- southern hemisphere; use beginning and ending of standard time in the comparison
if utc_timestamp >= dst_end_ts and utc_timestamp < dst_begin_ts then -- is current date time standard time?
tz_abbr = tz[args[1]].abbr; -- standard time abbreviation
else
timestamp = timestamp + 3600; -- add an hour for
tz_abbr = tz[args[1]].dst_abbr; -- dst abbreviation
end
else -- northern hemisphere
if utc_timestamp >= dst_begin_ts and utc_timestamp < dst_end_ts then -- all timestamps are UTC
timestamp = timestamp + 3600; -- add an hour
tz_abbr = tz[args[1]].dst_abbr;
else
tz_abbr = tz[args[1]].abbr;
end
end
elseif is_set (tz[args[1]].dst_begins) or is_set (tz[args[1]].dst_ends) or is_set (tz[args[1]].dst_time) then -- if some but not all not all parts then emit error message
return '<span style="font-size:100%" class="error">{{time}} – incomplete definition for ' .. args[1]:upper() .. ' ([[Template:Time#Error messages|help]])</span>';
else
tz_abbr = tz[args[1]].abbr; -- dst not observed for this timezone
end
end
if 'y' == df or 'dmy' == df then -- format the output (|df=y is legacy from original template)
if '12' == hf then
tz_string = mw.text.trim (os.date ('%l:%M %p, %e %B %Y', timestamp)); -- dmy, 12 hour am/pm
else
tz_string = os.date ('%R, %e %B %Y', timestamp); -- dmy
end
elseif 'iso' == df then
tz_string = os.date ('%FT%R', timestamp); -- iso
else
if '12' == hf then
tz_string = mw.text.trim (os.date ('%l:%M %p, %B %e, %Y', timestamp)); -- mdy (legacy default)
else
tz_string = os.date ('%R, %B %e, %Y', timestamp); -- mdy (legacy default)
end
end
if not is_set (tz[args[1]].article) then -- if some but not all not all parts then emit error message
return '<span style="font-size:100%" class="error">{{time}} – incomplete definition for ' .. args[1]:upper() .. ' ([[Template:Time#Error messages|help]])</span>';
end
 
local refreshLink = mw.title.getCurrentTitle():fullUrl{action = 'purge'} -- create a refresh link
return string.format ('%s : %s', utc_offset_2, utc_offset_3);
 
end