Saltar para o conteúdo

Módulo:Cumbersor de data

Ourige: Biquipédia, la anciclopédia lhibre.

La decumentaçon pa este módulo puode ser criada na páigina Módulo:Cumbersor de data/doc

local p = {}

local patterns = {"(%d+)-(%d+)-(%d+)", "(%d+)/(%d+)/(%d+)", "(%d+)%.(%d+)%.(%d+)"}
local month_names = {"janeiro", "febreiro", "márcio", "abril", "maio", "júnio", "júlio", "agosto", "setembre", "outubre", "nobembre", "dezembre"}

function p.main( frame )
	local date = frame.args[1]
	local day, month, year = 0, 0, 0
	
	for key, value in pairs(patterns) do
		local day, month, year = string.match(date, value)
		
		year = tonumber(year)
		day = tonumber(day)
		month = tonumber(month)
		
		-- Inverte dia pelo ano, caso o formato da data for xxxx/xx/xx
		if (day and day > 100) then
			day, year = year, day
		end
		
		-- Soma dois mil caso a data inserida for de dois dígitos
		if (year and year < 100) then
			year = year + 2000
		end
		
		-- Inverte dia pelo mes, caso o formato da data mês/dia/ano
		if (month and (month > 12 and month < 32)) then
			day, month = month, day
		end
		
		-- Algumas poucas páginas tinham erro de índice inexistente para o mês
		if (month and (month > 12 or month < 1)) then
			month = nil
		end
		
		if (day and month and year) ~= nil then
			return day .. " de " .. month_names[month] .. " de " .. year
		end
	end
	
	return date
end

return p