--[[
* Modulo per eseguire operazioni sulle date.
* Utilizzato da template come {{Età wikipediana}} e {{Tempo trascorso}}.
]]
local getArgs = require('Modulo:Arguments').getArgs
require('Modulo:No globals')
local errorCategory = '[[Categoria:Voci con errori del modulo Data]]'
local Date = {}
-------------------------------------------------------------------------------
-- Funzioni di utilità
-------------------------------------------------------------------------------
-- 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
local function isValidDate(date)
return pcall(function() mw.getContentLanguage():formatDate('', date) end)
end
-- Controlla le date inserite dall'utente e le restituisce come oggetti Date
local function parseArgs(args, isCompare)
local data1, data2, label1, label2
if isCompare then
data1, data2 = 'data1', 'data2'
label1, label2 = 'prima data', 'seconda data'
else
data1, data2 = 'inizio', 'fine'
label1, label2 = 'data di partenza', 'data di fine'
end
if not args[data1] then
error(string.format('la %s è obbligatoria', label1), 2)
elseif not isValidDate(args[data1]) then
error(string.format('la %s non è valida', label1), 2)
elseif not args[data2] then
error(string.format('la %s è obbligatoria', label2), 2)
elseif not isValidDate(args[data2]) then
error(string.format('la %s non è valida', label2), 2)
end
return {
d1 = Date:new(args[data1]),
d2 = Date:new(args[data2])
}
end
-------------------------------------------------------------------------------
-- classe Date
-------------------------------------------------------------------------------
local function date_eq(t, t2)
return t.ut == t2.ut
end
local function date_lt(t, t2)
return t.ut < t2.ut
end
function Date:new(str)
local self = {}
setmetatable(self, { __index = Date, __eq = date_eq, __lt = date_lt })
self.ut = tonumber(mw.getContentLanguage():formatDate('U', str, true))
return self
end
function Date:getDateString()
return (mw.getContentLanguage():formatDate('j F Y', '@' .. self.ut):gsub('^1%s', '1º '))
end
function Date:addDays(days)
return Date:new('@' .. (self.ut + days * 86400))
end
function Date:diffYMD(date2, rawTable, dir)
local monthdays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
local d1, d2 = os.date('*t', self.ut), os.date('*t', date2.ut)
local ret = { seconds = math.abs(self.ut - date2.ut) }
if self.ut >= date2.ut then d1, d2 = d2, d1 end
-- anni
ret.years = d2.year - d1.year
if ret.years > 0 and (d1.month > d2.month or (d1.month == d2.month and d1.day > d2.day)) then
ret.years = ret.years - 1
end
-- mesi
ret.months = (d2.month - d1.month + 12) % 12
if d1.day > d2.day then
ret.months = (ret.months == 0 and d1.year < d2.year) and 11 or ret.months - 1
end
-- giorni
ret.days = d2.day >= d1.day and d2.day - d1.day or (monthdays[d1.month] - d1.day) + d2.day
-- utilizza la sequence per ritornare anche la versione testuale
if ret.years > 0 then
table.insert(ret, string.format('%s %s', ret.years, ret.years == 1 and 'anno' or 'anni'))
end
if ret.months > 0 then
table.insert(ret, string.format('%s %s', ret.months, ret.months == 1 and 'mese' or 'mesi'))
end
if ret.days > 0 or ret.years + ret.months + ret.days == 0 then
table.insert(ret, string.format('%s %s', ret.days, ret.days == 1 and 'giorno' or 'giorni'))
end
return rawTable and ret or self:formatResult(mw.text.listToText(ret, ', ', ' e '), date2, dir)
end
-- Funzione di utilità per Date:diff
local function getMagnitudine(diff, magnitudine_min)
local units = {
secondi = 0, minuti = 1, ore = 2, giorni = 3, settimane = 4, mesi = 5, anni = 6
}
local ret
if diff.seconds < 120 then -- minore di due minuti
ret = 'secondi'
elseif diff.seconds < 7200 then -- minore di due ore
ret = 'minuti'
elseif diff.seconds < 172800 then -- minore di due giorni
ret = 'ore'
elseif diff.years == 0 and diff.months < 2 then -- minore di due mesi
ret = 'giorni'
elseif diff.years < 2 then -- minore di due anni
ret = 'mesi'
else
ret = 'anni'
end
-- utilizzo di magnitudine_min (il valore minimo quando è automatica)
if magnitudine_min and units[magnitudine_min] then
ret = units[magnitudine_min] > units[ret] and magnitudine_min or ret
end
return ret
end
-- Funzione di utilità per Date:diff
local function convert(seconds, unit, text, text2)
local ret = math.floor(seconds / unit)
return ret, string.format('%s %s', ret, ret == 1 and text or text2)
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(d2, magnitudine, magnitudine_min, dir)
local diff, ret, val, result
diff = self:diffYMD(d2, true)
magnitudine = magnitudine or getMagnitudine(diff, magnitudine_min)
if magnitudine == 'secondi' then
val, result = convert(diff.seconds, 1, 'secondo', 'secondi')
elseif magnitudine == 'minuti' then
val, result = convert(diff.seconds, 60, 'minuto', 'minuti')
elseif magnitudine == 'ore' then
val, result = convert(diff.seconds, 3600, 'ora', 'ore')
elseif magnitudine == 'giorni' then
val, result = convert(diff.seconds, 86400, 'giorno', 'giorni')
elseif magnitudine == 'settimane' then
val, result = convert(diff.seconds, 604800, 'settimana', 'settimane')
elseif magnitudine == 'mesi' then
val = diff.years * 12 + diff.months
result = string.format('%s %s', val, val == 1 and 'mese' or 'mesi')
else
val = diff.years
result = string.format('%s %s', diff.years, diff.years == 1 and 'anno' or 'anni')
end
return self:formatResult(result, d2, dir, val)
end
-------------------------------------------------------------------------------
-- API
-------------------------------------------------------------------------------
local p = { Date = Date }
-- Entry point per {{#invoke:Data|diff}}
function p.diff(frame)
local args = getArgs(frame)
local success, result = xpcall(function() return parseArgs(args) end, errhandler)
return success and result.d1:diff(result.d2, args.magnitudine, args['magnitudine min'], args.dir) or result
end
-- Entry point per {{#invoke:Data|diff_ymd}}
function p.diff_ymd(frame)
local args = getArgs(frame)
local success, result = xpcall(function() return parseArgs(args) end, errhandler)
return success and result.d1:diffYMD(result.d2, false, args.dir) or result
end
-- Entry point per {{#invoke:Data|compare}}
function p.compare(frame)
local success, result = xpcall(function() return parseArgs(getArgs(frame), true) end, errhandler)
return success and (result.d1 == result.d2 and 0 or ( result.d1 < result.d2 and -1 or 1 )) or result
end
return p