Modulo:Non firmato/sandbox

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
local p = {}

local function getDateObject(datetime, seconds)
	return string.format('%s - %s seconds', datetime, seconds)
end

local function getTimezoneAbbr(date)
	if date == '' then return end

	local lang = mw.language.getContentLanguage()
	local months = {
		gen = 1, feb = 2, mar = 3, apr = 4, mag = 5, giu = 6,
		lug = 7, ago = 8, set = 9, ott = 10, nov = 11, dic = 12
	}
	local monthnum = months[date:match('%a%a%a')]

	if monthnum then
		date = date:gsub('%s%a%a%a%s', string.format('-%s-', monthnum))
	end

	if pcall(lang.formatDate, lang, '', date) == false then return end

	local offset = lang:formatDate('Z', date, true)
	local localdate = lang:formatDate('r', getDateObject(date, offset), true)
	local hour = lang:formatDate('H', date)
	local localhour = lang:formatDate('H', localdate, true)

	-- verifica l'assenza di cambi d'ora imprevisti nella conversione all'ora locale
	if hour ~= localhour then
		local unix = lang:formatDate('U', date)
		local localunix = lang:formatDate('U', localdate, true)
		local difftime = os.difftime(unix, localunix)
		localdate = lang:formatDate('r', getDateObject(localdate, difftime), true)
	end

	-- non restituisce la sigla se c'è ambiguità col cambio d'ora all'ora solare
	if localhour ~= lang:formatDate('H', localdate .. '-1 hour', true) then
		local timezoneabbr = lang:formatDate('T', localdate, true)
		return string.format(' (%s)', timezoneabbr)
	end
end

function p.main(frame)
	local args = frame:getParent().args
	local date, username, userpage, talkpage
	local regex = '%d?%d:%d%d,? %d+ %a%a%a %d%d%d%d'

	-- se non c'è il 2º parametro, verifica se data e username sono insieme
	if args[1] and not args[2] then
		-- date e username possono essere separati da U+200E o da uno spazio
		local sep = args[1]:match(regex .. '(\226\128\142)') or ' '
		date, username = args[1]:match(string.format('(%s)%s(.*)', regex, sep))
		-- se non c'è la data, presume sia solo il nome utente (obbligatorio)
		if not date then
			date, username = '', args[1]
		end
	else
		-- presume di default che data e username siano al 1º e 2º parametro
		date, username = args[1] or '', args[2] or ''
		if not date:match(regex) and username:match(regex) then
			date, username = username, date
		end
	end

	date, username = mw.text.trim(date), mw.text.trim(username)

	userpage = frame:expandTemplate{ title = 'IP?', args = { username } } == '' and
		string.format('[[Utente:%s|%s]]', username, username) or username
	talkpage = string.format('[[Discussioni utente:%s|discussioni]] · [[Speciale:Contributi/%s|contributi]]', username, username)

	date = date .. (getTimezoneAbbr(date) or '')

	return string.format("<small>&mdash; ''Questo commento senza la [[Aiuto:Firma|firma utente]] è stato inserito da %s (%s)%s''.</small>",
		userpage, talkpage, date == '' and '' or ' ' .. date)
end

return p