Content deleted Content added
No edit summary |
No edit summary |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 2:
local yesno = require('Module:Yesno')
local getArgs = require ('Module:Arguments').getArgs
local tz = {}; -- holds local copy of the specified timezone table from tz_data{}
▲local cfg = {}; -- for internationalization
Line 24 ⟶ 25:
]]
local function substitute (msg,
return
end
Line 32 ⟶ 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 (
return substitute (cfg.err_msg,
end
Line 353 ⟶ 354:
local is_dst_tz;
local tz_aliases = data.tz_aliases; -- get the aliases table
local tz_data = data.tz_data; -- get the tz data table
Line 379 ⟶ 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']});
end
Line 394 ⟶ 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 401 ⟶ 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])});
end
end
Line 408 ⟶ 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 414 ⟶ 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 434 ⟶ 432:
if nil == dst_begin_ts or nil == dst_end_ts then
return error_msg ({'Time', cfg.err_text['bad_dst']});
end
Line 453 ⟶ 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'],
else
tz_abbr = tz.abbr; -- dst not observed for this timezone
Line 503 ⟶ 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'],
end
Line 524 ⟶ 522:
--[[--------------------------< U T
implements {{UTC offset}}
Mimics templates {{Time/GMT offset}}, {{Time/EST offset}}, etc.▼
{{#invoke:Time|utc_offset|<tz>}} – for a stand-alone invoke
{{#invoke:Time|utc_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
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
return string.format ('%s:%s', tonumber (hours) + 1, minutes); -- return optional sign hh:mm string
end
▲ local data = mw.loadData ('Module:Time/data/sandbox'); -- load the data module
local args_t = getArgs (frame); -- fetch arguments; only {{{1}}}, timesone specifier is used
if not args_t[1] then -- no timezone specifier
return error_msg ({'UTC offset', cfg.err_text['missing_tz']});
end
local timezone = args_t[1]:lower(); -- lowercase for indexing into tz data tables
timezone = data.tz_aliases[timezone] or timezone; -- if <timezone> is an alias, map to its canonical value
if not data.tz_data[timezone] then -- timezone specifier not known
return error_msg ({'UTC offset', substitute (cfg.err_text['unknown_tz'], {timezone})});
end
tz = data.tz_aliases[timezone] and data.tz_data[data.tz_aliases[timezone]] or data.tz_data[timezone]; -- fetch a copy of this timezone's data; <tz> is a page-global table used by functions called from this function
local utc_timestamp = os.time (); -- get current server time (UTC) in seconds; used to determine when dst adjustment should be applied
local
▲ local retval;
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▼
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; -- ... dst is not observed (|dst=no) show time as standard time
return error_msg ('bad_dst'); -- abandon with error message▼
end▼
▲
▲ 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?▼
retval = tz.utc_offset; -- return timezone-offset from utc▼
else ▼
▲ retval = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
end
else -- northern hemisphere▼
▲ if invert then
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?
retval = 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
▲ retval = tz.utc_offset; -- return timezone-offset from utc
▲
▲ end
▲ 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
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
▲ retval = tz.utc_offset; -- return timezone-offset from utc
end
local sign, hours, minutes =
if '' == sign then
sign = '+';
Line 597 ⟶ 605:
if 0 ~= tonumber (minutes) then
return string.format ('%s%s %s %s%s minutes', sign, tonumber(hours), ('1' == hours) and 'hour' or 'hours', sign, tonumber(minutes));
else
return string.format ('%s%s %s', sign, tonumber(hours), ('1' == hours) and 'hour' or 'hours');
end
end
Line 610 ⟶ 617:
return {
time = time,
utc_offset = utc_offset,
}
|