Module:Time/sandbox: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 25:
]]
 
local function substitute (msg, argsargs_t)
return argsargs_t and mw.message.newRawMessage (msg, argsargs_t):plain() or msg;
end
 
Line 33:
 
create an error message
<args_t> is a sequence where [1] is template name and [2] is substituted error message
 
]]
 
local function error_msg (msg, argargs_t)
return substitute (cfg.err_msg, substitute (cfg.err_text[msg], arg)args_t);
end
 
Line 377:
if not ((DF_cust_a and DF_cust_p) or -- DF_cust_a xor DF_cust_p
(not DF_cust_a and not DF_cust_p))then
return error_msg ({'Time', cfg.err_text['bad_df_pair']}); -- both are required
end
 
Line 392:
local s, t = mw.ustring.match (tz.utc_offset, '(±)(%d%d:%d%d)'); -- ± only valid for offset 00:00
if s and '00:00' ~= t then
return error_msg ({'Time', cfg.err_text['bad_sign']});
end
tz.df = 'iso';
Line 399:
tz = tz_aliases[args[1]] and tz_data[tz_aliases[args[1]]] or tz_data[args[1]]; -- make a local copy of the timezone table from tz_data{}
if not tz then
return error_msg ({'Time', substitute (cfg.err_text['unknown_tz'], args[1])}); -- if the timezone given isn't in module:time/data(/sandbox)
end
end
Line 406:
DF = DF:lower(); -- normalize to lower case
if not cfg.df_vals[DF] then
return error_msg ({'Time', substitute (cfg.err_text['bad_format'], DF)});
end
 
Line 412:
local test_time = get_test_time (args._TEST_TIME_);
if not test_time then
return error_msg ({'Time', cfg.err_text['test_time']});
end
 
Line 432:
 
if nil == dst_begin_ts or nil == dst_end_ts then
return error_msg ({'Time', cfg.err_text['bad_dst']});
end
Line 451:
end
elseif is_set (tz.dst_begins) or is_set (tz.dst_ends) or is_set (tz.dst_time) then -- if some but not all not all parts then emit error message
return error_msg ({'Time', substitute (cfg.err_text['bad_def'], argsargs_t[1]:upper())});
else
tz_abbr = tz.abbr; -- dst not observed for this timezone
Line 501:
 
if not is_set (tz.article) then -- if some but not all not all parts then emit error message
return error_msg ({'Time', substitute (cfg.err_text['bad_def'], argsargs_t[1]:upper())});
end
Line 522:
 
 
--[[--------------------------< U T ZC _ O F F S E T >------------------------------------------------------------
 
implements {{UTC offset}}
Mimics templates {{Time/GMT offset}}, {{Time/EST offset}}, etc.
 
Mimicsmimics templates {{Time/GMT offset}}, {{Time/EST offset}}, etc.
{{#invoke:Time/sandbox|tz_offset|<tz>}} – for a stand-alone invoke
 
{{#invoke:Time/sandbox|tz_offset}} – for an invoke in a template (<tz> is first positional parameter in the template call)
{{#invoke:Time/sandbox|tz_offsetutc_offset|<tz>}} – for a stand-alone invoke
{{#invoke:Time/sandbox|tz_offsetutc_offset}} – for an invoke in a template (<tz> is first positional parameter in the template call)
 
where <tz> is a timezone abbreviation known to Module:Time/data
 
returns a UTC offset string suitable for use with the {{#time:}} parser function:
{{#time:H:i | {{#invoke:Time|utc_offset|MST}} }}
{{#time:H:i | {{UTC_offset|MST}} }}
 
]]
 
local function tz_offsetutc_offset (frame)
local function apply_dst_ajdust (offset) -- local function to adjust standard time to daylight time; called when adjustment is needed
local hours, minutes = offset:match ('^(%-?%d%d):(%d%d)'); -- extract signed hours and minutes from specified offset
Line 544 ⟶ 548:
 
if not args_t[1] then -- no timezone specifier
return error_msg ({'UTC offset', cfg.err_text['missing_tz']}); -- abandon with error message
end
 
Line 551 ⟶ 555:
 
if not data.tz_data[timezone] then -- timezone specifier not known
return error_msg ({'UTC offset', substitute (cfg.err_text['unknown_tz'], {timezone})}); -- abandon with error message
end
 
Line 560 ⟶ 564:
local utc_offset;
 
local DST = first_set (cfg.aliases['dst'], args_t) or true; -- string 'always' or boolean
if is_set (tz.dst_begins) and is_set (tz.dst_ends) and is_set (tz.dst_time) then -- make sure we have all of the parts
if 'always' == DST then -- if needed to always display dst time
local dst_begin_ts, dst_end_ts, invert = make_dst_timestamps (timestamp); -- get begin and end dst timestamps and <invert> flag
utc_offset = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
 
elseif not yesno (DST) then -- for timezones that DO observe dst but for this ___location ...
if nil == dst_begin_ts or nil == dst_end_ts then -- if either of these are nil
utc_offset = tz.utc_offset; -- return... timezone-offsetdst is not observed (|dst=no) show time as fromstandard utctime
return error_msg ('bad_dst'); -- abandon with error message
else
end
if is_set (tz.dst_begins) and is_set (tz.dst_ends) and is_set (tz.dst_time) then -- make sure we have all of the parts
 
local dst_begin_ts, dst_end_ts, invert = make_dst_timestamps (timestamp); -- get begin and end dst timestamps and <invert> flag
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?
if nil == dst_begin_ts or nil == dst_end_ts then -- if either of these are nil
utc_offset = tz.utc_offset; -- return timezone-offset from utc
return error_msg ({'UTC offset', cfg.err_text['bad_dst']}); -- abandon with error message
else
utc_offset = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
end
else -- northern hemisphere
if invert then -- southern hemisphere; use beginning and ending of standard time in the comparison
if utc_timestamp >= dst_begin_ts and utc_timestamp < dst_end_ts then -- is current date time daylight time?
if utc_timestamp >= dst_end_ts and utc_timestamp < dst_begin_ts then -- is current date time standard time?
utc_offset = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
utc_offset = tz.utc_offset; -- return timezone-offset from utc
else
else
utc_offset = tz.utc_offset; -- return timezone-offset from utc
utc_offset = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
end
else else -- northern hemisphere
if utc_timestamp >= dst_begin_ts and utc_timestamp < dst_end_ts then -- is current date time daylight time?
utc_offset = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
else
utc_offset = tz.utc_offset; -- return timezone-offset from utc
end
end
elseif is_set (tz.dst_begins) or is_set (tz.dst_ends) or is_set (tz.dst_time) then -- if some but not all not all parts then emit error message
return error_msg ({'UTC offset', substitute (cfg.err_text['bad_def'], argsargs_t[1]:upper())});
else -- timezone does not use dst
utc_offset = tz.utc_offset; -- return timezone-offset from utc
end
 
elseif is_set (tz.dst_begins) or is_set (tz.dst_ends) or is_set (tz.dst_time) then -- if some but not all not all parts then emit error message
return error_msg ('bad_def', args[1]:upper());
 
else -- timezone does not use dst
utc_offset = tz.utc_offset; -- return timezone-offset from utc
end
Line 606 ⟶ 617:
return {
time = time,
utc_offset = utc_offset,
tz_offset = tz_offset,
}