Ir para o conteúdo

Módulo:Infobox/datas

Origem: Wikipédia, a enciclopédia livre.
local getArgs = require('Módulo:Arguments').getArgs

local default_error_category = "[[Categoria:!Páginas usando Info/Televisão com datas não padronizadas]]"

local p = {}

function p.start_end_date_template_validation(frame)
	local args = getArgs(frame)
	local error_category = args.error_category or args.categoria_erro or default_error_category

	local start_date = args.first_aired or args.released or args.airdate or args.release_date or args.airdate_overall or args['primeiro-episódio'] or args.data_inicio or args['data_início'] or args['lançamento']
	if start_date then
		if not start_date:find("dtstart") then
			return error_category
		end
	end

	local end_date = args.last_aired or args['último-episódio'] or args.data_fim
	if end_date then
		if not end_date:find("dtend") and end_date ~= "presente" then
			return error_category
		end
	end
end

local function fixMonthCase(month)
	if month:match("^%u+$") then -- todo maiúsculo
		return mw.language.getContentLanguage():ucfirst(month:lower()) -- só inicial maiúscula
	else
		return month -- mantém como está
	end
end

function p.dates(frame)
	local returnval
	local args = getArgs(frame)

	if table.getn(args) < 2 then
		if args['1'] == nil and args['2'] == nil then
			return ''
		elseif args['1'] == nil then
			return args['2']
		elseif args['2'] == nil then
			return args['1']
		end
	end

	args['1'] = args['1']:gsub("&nbsp;"," ")
	args['2'] = args['2']:gsub("&nbsp;"," ")

	-- Verifica se já existe <br /> nos argumentos para decidir o dash
	local dash
	if args['1']:find("<br%s*/>") or args['2']:find("<br%s*/>") then
		dash = '&nbsp;–'
	else
		dash = '&nbsp;–<br />'
	end

	local function normalize_date(date_str)
		local y,m,d = string.match(date_str, '(%d%d%d%d)[%-/](%d%d?)[%-/](%d%d?)')
		if y and m and d then
			local MONTHS_INV = {
				[1] = "janeiro", [2] = "fevereiro", [3] = "março", [4] = "abril",
				[5] = "maio", [6] = "junho", [7] = "julho", [8] = "agosto",
				[9] = "setembro", [10] = "outubro", [11] = "novembro", [12] = "dezembro"
			}
			m = tonumber(m)
			d = tonumber(d)
			y = tonumber(y)
			if MONTHS_INV[m] then
				local mes = fixMonthCase(MONTHS_INV[m])
				return string.format("%d de %s de %d", d, mes, y), d, mes, y
			end
		else
			local d2, m2, y2 = string.match(date_str, '(%d%d?)%s+de%s+(%a+)%s+de%s+(%d+)')
			if d2 and m2 and y2 then
				local mes = fixMonthCase(m2)
				return date_str, tonumber(d2), mes, tonumber(y2)
			end
		end
		return date_str, nil, nil, nil
	end

	local norm1, d1, m1, y1 = normalize_date(args['1'])
	local norm2, d2, m2, y2 = normalize_date(args['2'])

	local MONTHS = {janeiro=1, fevereiro=2, ['março']=3, abril=4, maio=5, junho=6, julho=7, agosto=8, setembro=9, outubro=10, novembro=11, dezembro=12}

	if y1 ~= nil and y2 ~= nil then
		local diff = os.time({year=y2, month=MONTHS[m2:lower()], day=d2, hour=0, min=0, sec=0}) - os.time({year=y1, month=MONTHS[m1:lower()], day=d1, hour=0, min=0, sec=0})

		if diff < 0 then
			returnval = 'Intervalo de datas inválido'
		else
			if y1 == y2 then
				returnval = string.format("%d de %s%s%d de %s de %d", d1, m1, dash, d2, m2, y2)
			else
				returnval = norm1 .. dash .. norm2
			end
		end
	else
		returnval = args['1'] .. dash .. args['2']
	end

	return returnval
end

return p