Modulo:Data: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
+classe Date, categoria, compare
+newDMY, commenti
Riga 4:
]]
 
local getArgs = require('Modulo:Arguments').getArgs
require('Modulo:No globals')
 
local getArgs = require('Modulo:Arguments').getArgs
local errorCategory = '[[Categoria:Voci con errori del modulo Data]]'
-- classe esportata
local Date = {}
 
Line 63 ⟶ 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".
function Date:newnewDMY(str)
local retmonths = {
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+)")
if dirday then
return Date:new(string.format('%d-%d-%d', year, months[month], day))
else
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
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'))
return (mw.getContentLanguage():formatDate('j F Y'fmt, '@' .. self.ut):gsub('^1%s', '1º '))
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".
local function Date:formatResult(result, d2date1, date2, dir, diffVal)
returnlocal ret
if dir then
-- ritorna il 'fa' anche con selfdate1.ut == d2date2.ut (si potrebbe configurare con un parametro)
ret = selfdate1.ut < d2date2.ut and 'tra ' .. result or result .. ' fa'
else
ret = (selfdate1.ut <= d2date2.ut or diffVal == 0) and result or '-' .. result
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 109 ⟶ 158:
end
 
return rawTable and ret or self:formatResult(mw.text.listToText(ret, ',&#32;', '&#32;e&#32;'), self, date2, dir)
end
 
Line 146 ⟶ 195:
end
 
-- RitornaRestituisce la differenza tracon lela date d1 edata d2 (Unix time) in solo una tra le unità:
-- 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(d2date2, magnitudine, magnitudine_min, dir)
local diff, ret, val, result
 
diff = self:diffYMD(d2date2, true)
magnitudine = magnitudine or getMagnitudine(diff, magnitudine_min)
 
Line 186 ⟶ 221:
end
return self:formatResult(result, d2self, date2, dir, val)
end