Модуль:Time

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация

Модуль предоставляет функции для работы со временем.

Использование[править код]

В шаблонах-карточках подключается через Шаблон:Время

Функции[править код]

  • time.fromTemplate(frame) — вызов модуля из шаблона через #invoke
  • time.fromWikidata(entity) — вызов из другого модуля с данными из Wikidata
  • time.fromSeconds(seconds) — вызов из другого модуля, на вход - секунды
  • time.stringToSeconds(string) — преобразование строки в секунды
  • time.formatText — время в формате 1 час 23 мин для длительности по-умолчанию
  • time.formatMinutes — время в формате 183 мин для карточки в фильмах
  • time.formatShort — время в формате 00:00 для треклиста
  • time.formatLong — время в формате 0:00:00 для общей длительности в треклисте
local p = {}

-- вызов из шаблона
function p.fromTemplate(frame)
	local args = frame:getParent().args
	local p1 = mw.text.trim(args[1] or '')
	local p2 = mw.text.trim(args[2] or '')
	local p3 = mw.text.trim(args[3] or '')
	local format = mw.text.trim(args['format'] or '')

	local seconds

	if p1 ~= '' then
		seconds = p.stringToSeconds(p1)
		if seconds == nil then return p1 end
	else
		return ''
	end

	if p2 ~= '' then
		seconds = 0
		seconds = seconds + tonumber(p1) * 60
		seconds = seconds + tonumber(p2)
	end

	if p3 ~= '' then
		seconds = 0
		seconds = seconds + tonumber(p1) * 3600
		seconds = seconds + tonumber(p2) * 60
		seconds = seconds + tonumber(p3)
	end

	return p.formatToString(seconds, format)
end

-- форматирование времени
function p.formatToString(seconds, format)
	if not seconds then return "" end
	if format == "" or format == nil then format = "text" end
	if format == "text"    then return p.formatText(seconds) end
	if format == "GOST"    then return p.formatGOST(seconds) end
	if format == "minutes" then return p.formatMinutes(seconds) end
	if format == "short"   then return p.formatShort(seconds) end
	if format == "long"    then return p.formatLong(seconds) end

	return "Unknown time format: "..format
end

-- время в формате: 00:00
function p.formatShort(seconds)
	if not seconds then return "" end
	local m = math.floor(seconds/60); seconds = seconds - m*60;
	return _z(m) .. ":" .. _z(seconds)
end

-- время в формате: 0:00:00
function p.formatLong(seconds)
	if not seconds then return "" end
	local h = math.floor(seconds/3600); seconds = seconds - h*3600;
	local m = math.floor(seconds/60);   seconds = seconds - m*60;
	local s = seconds

	local st = m .. ":" .. _z(s)
	if h > 0 then st = h .. ":" .. _z(m) .. ":" .. _z(s) end
	return st
end

-- время в формате: 1 час 23 мин
function p.formatText(seconds)
	if not seconds then return "" end
	local x
	x = 24*3600; local d = math.floor(seconds/x); seconds = seconds - d*x;
	x =    3600; local h = math.floor(seconds/x); seconds = seconds - h*x;
	x =      60; local m = math.floor(seconds/x); seconds = seconds - m*x;
	x =       0; local s = seconds

	local st = ""
	if d > 0 then st = st .. " " .. d .. " сут." end
	if h > 0 then st = st .. " " .. h .. " ч." end
	if m > 0 then st = st .. " " .. m .. " мин." end
	if s > 0 then st = st .. " " .. s .. " сек." end
	return mw.text.trim(st)
end

-- время в формате: 1 ч 23 мин
function p.formatGOST(seconds)
	local st = p.formatText(seconds)
	st = st:gsub("сут.", "сут")
	st = st:gsub("ч.",   "ч")
	st = st:gsub("мин.", "мин")
	st = st:gsub("сек.", "с")
	return st
end

-- время в формате: 83 мин
function p.formatMinutes(seconds)
	if not seconds then return "" end
	local m = math.floor(seconds/60)
	return m .. " мин"
end

-- преобразование строки в секунды
function p.stringToSeconds(st)
	-- отформатировано через двоеточие
	local _, _, h, m, s = string.find(st, "(%d+):(%d+):(%d+)")
	if _ then return h*3600 + m*60 + s end
	local _, _, m, s = string.find(st, "(%d+):(%d+)")
	if _ then return m*60 + s end

	-- удаляем неразрывные пробелы и лишние символы
	st = string.gsub(st, "(%d) (%d)", "%1%2")
	st = string.gsub(st, "(%d) (%d)", "%1%2")
	st = string.gsub(st, "&#%d+;", "")
	st = string.gsub(st, "<.->", "")

	local s, offset = 0, 1
	local _, _end, x = string.find(st, "(%d+)[^%d:]-су", offset)
	if _ then s = s + x * 24*3600; offset = _end; end
	local _, _end, x = string.find(st, "(%d+)[^%d:с]-д", offset)
	if _ then s = s + x * 24*3600; offset = _end; end
	local _, _end, x = string.find(st, "(%d+)[^%d:]-ч", offset)
	if _ then s = s + x * 3600; offset = _end end
	local _, _end, x = string.find(st, "(%d+)[^%d:]-м", offset)
	if _ then s = s + x * 60; offset = _end end
	local _, _end, x = string.find(st, "(%d+)[^%d:]-с", offset)
	if _ then s = s + x end

	if s > 0 then return s end
	return tonumber(st)
end

function _z(x) if x < 10 then return "0"..x else return x end end

return p