Content deleted Content added
use require('strict') instead of require('Module:No globals') |
No edit summary |
||
Line 521:
return table.concat ({time_string, tz_tag, refresh_link});
end
--[[--------------------------< T Z _ O F F S E T >------------------------------------------------------------
Mimics templates {{Time/GMT offset}}, {{Time/EST offset}}, etc.
{{#invoke:Time/sandbox|tz_offset|<tz>}}
where <tz> is a timezone abbreviation known to Module:Time/data
]]
local function tz_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
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
cfg = data.cfg; -- get the configuration table
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 ('missing_tz'); -- abandon with error message
end
local timezone = args_t[1]:lower(); -- lowercase for indexing into tz data tables
if not data.tz_data[timezone] then -- timezone specifier not known
return error_msg ('unknown_tz', timezone); -- abandon with error message
end
-- tz = 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
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 utc_offset = get_utc_offset (); -- utc offset for specified timezone in seconds
local timestamp = utc_timestamp + utc_offset; -- make local time timestamp (in seconds)
local retval;
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 nil == dst_begin_ts or nil == dst_end_ts then -- if either of these are nil
return error_msg ('bad_dst'); -- abandon with error message
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?
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 utc_timestamp >= dst_begin_ts and utc_timestamp < dst_end_ts then -- is current date time daylight time?
retval = apply_dst_ajdust (tz.utc_offset); -- return dst-adjusted timezone-offset from utc
else
retval = 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 ('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 = retval:match ('^([%-%+]?)(%d%d?):(%d%d)')
if '' == sign then
sign = '+';
end
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
return retval .. ' + hours'
end
Line 527 ⟶ 608:
]]
return {
time = time tz_offset = tz_offset,
}
|