Modulo:Data/sandbox: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
+getDateString |
m Bot: rimuovo no globals obsoleto |
||
(6 versioni intermedie di un altro utente non mostrate) | |||
Riga 3:
* Utilizzato da template come {{Età wikipediana}} e {{Tempo trascorso}}.
]]
require('strict')
local getArgs = require('Modulo:Arguments').getArgs
local errorCategory = '[[Categoria:Voci con errori del modulo Data]]'
-- classe esportata
local Date = {}
Line 14 ⟶ 17:
-- Error handler per xpcall, formatta l'errore
local function errhandler(msg)
local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
return string.format('<span class="error">Errore: %s</span>%s', msg, cat)
end
Line 61 ⟶ 65:
end
-- Costruisce un oggetto Date a partire da una stringa nel formato
function Date:new(str)▼
-- accettato dalla funzione parser #time.
function Date:new(str, precision)
local self = {}
setmetatable(self, { __index = Date, __eq = date_eq, __lt = date_lt })
self.ut = tonumber(mw.getContentLanguage():formatDate('U', str, true))
self.precision = precision or 11
return self
end
-- Costruisce un oggetto Date a partire da una stringa nel formato:
-- "giorno mese_per_esteso anno" oppure "mese_per_esteso anno" oppure "anno".
gennaio = 1, febbraio = 2, marzo = 3, aprile = 4, maggio = 5, giugno = 6,
luglio = 7, agosto = 8, settembre = 9, ottobre = 10, novembre = 11, dicembre = 12
}
local success, result = pcall(function()
local day, month, year = str:match("(%d+) (%a+) (%d+)")
return Date:new(string.format('%d-%d-%d', year, months[month], day))
month, year = str:match("(%a+) (%d+)")
if month then
return Date:new(string.format('%d-%d', year, months[month]), 10)
else
return Date:new(string.format('%d', str:match("(%d+)")), 9)
end▼
end )
return success and result or nil
end
-- Restituisce una stringa che rappresenta la data, senza l'ora.
function Date:getDateString()
local fmt = self.precision == 9 and 'Y' or
return (mw.getContentLanguage():formatDate('j F Y', '@' .. self.ut):gsub('^1%s', '1º '))▼
(self.precision == 10 and 'F Y' or
(self.precision == 11 and 'j F Y' or 'j F Y'))
end
-- Restituisce un nuovo oggetto Date la cui data è avanzata del numero di giorni specificati.
function Date:addDays(days)
return Date:new('@' .. (self.ut + days * 86400))
end
-- Funzione di utilità per Date:diffYMD e Date:diff
-- Aggiunge un eventuale prefisso e suffisso al risultato invece del segno.▼
-- L'ultimo parametro diffVal è utilizzato solo da diff per evitare che▼
-- {{#invoke:Data|diff|inizio=2016/01/01|fine=2015/12/31|magnitudine=anni}} ritorni "-0 anni".▼
if dir then
else
end
return ret
end
-- Restituisce la differenza con la data date2 in anni, mesi e giorni.
function Date:diffYMD(date2, rawTable, dir)
local monthdays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
Line 107 ⟶ 158:
end
return rawTable and ret or
end
Line 144 ⟶ 195:
end
--
▲-- Aggiunge un eventuale prefisso e suffisso al risultato invece del segno.
▲-- L'ultimo parametro diffVal è utilizzato solo da diff per evitare che
▲-- {{#invoke:Data|diff|inizio=2016/01/01|fine=2015/12/31|magnitudine=anni}} ritorni "-0 anni".
▲function Date:formatResult(result, d2, dir, diffVal)
▲ local ret
▲ if dir then
▲ -- ritorna il 'fa' anche con self.ut == d2.ut (si potrebbe configurare con un parametro)
▲ ret = self.ut < d2.ut and 'tra ' .. result or result .. ' fa'
▲ else
▲ ret = (self.ut <= d2.ut or diffVal == 0) and result or '-' .. result
▲ end
▲ return ret
▲end
▲-- Ritorna la differenza tra le date d1 e d2 (Unix time) in solo una tra le unità:
-- anni, mesi, settimane, giorni, ore, minuti e secondi.
function Date:diff(
local diff, ret, val, result
diff = self:diffYMD(
magnitudine = magnitudine or getMagnitudine(diff, magnitudine_min)
Line 184 ⟶ 221:
end
return
end
|