Модуль:Tzdata

Материал из свободной русской энциклопедии «Традиция»
Перейти к навигации Перейти к поиску

Для документации этого модуля может быть создана страница Модуль:Tzdata/doc

local match, seconds_between, time, floor = mw.ustring.match, os.difftime, os.time, math.floor
local lang = mw.language.getContentLanguage()
local wiki_tz_offset, wiki_tz_name = lang:formatDate ('Z', nil, true), lang:formatDate ('e', nil, true)

local tzdata = mw.ext.externalData.getExternalData { source = 'tzdata' }

tzdata.__text = nil
tzdata[1].__text = nil

local function zone_time (zone)
	local months = {
		Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6,
		Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12
	}
	local year, month, day = zone.year, months [zone.month], zone.day
	local hour, min = (zone.time):match '(%d%d):(%d%d)'
	return time {
		year = zone.year,
		month = months [zone.month],
		day = zone.day,
		hour = hour,
		min = min
	}
end

-- Get time in local timezone:
local retrieved = (function ()
	for _, zone in ipairs (tzdata) do
		if zone.name == wiki_tz_name then
			return zone_time (zone)
		end
	end
end) ()
	
local zones, abbr_set, name_set = {}, {}, {}
for _, zone in ipairs (tzdata) do
	local now_zone = zone_time (zone)
	zone.year, zone.month, zone.day, zone.weekday, zone.time = nil, nil, nil, nil, nil
	local diff = seconds_between (now_zone, retrieved) + wiki_tz_offset
	zone.offset = diff
	local offset_min = (diff - diff % 60) / 60
	local diff_hour, diff_min =  (offset_min -  offset_min % 60) / 60,  offset_min % 60
	zone.diff = ('%1.2d:%2.2d'):format (diff_hour, diff_min)
	local name, abbr = zone.name, zone.abbr
	zones [name] = zone
	if not zones [abbr] then
		zones [abbr] = zone
	end
	abbr_set [abbr] = true
	name_set [name] = true
end

local function set2array (set)
	local array = {}
	for item, _ in pairs (set) do
		array [#array + 1] = item
	end
	return array
end
local abbrs, names = set2array (abbr_set), set2array (name_set)

local function choice (array)
	return '"' .. table.concat (array, '" / "') .. '"'
end

return {
	zones = zones,
	abbrs = abbrs,
	names = names,
	re = { abbrs = choice (abbrs), names = choice (names) }
}