Modulo:Data: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
parametro level in error |
+classe Date, categoria, compare |
||
Riga 6:
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)
Line 15 ⟶ 23:
end
-- Controlla
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'
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',
local ret = { seconds = math.abs(
if
-- anni
Line 89 ⟶ 109:
end
return rawTable and ret or self:formatResult(mw.text.listToText(ret, ', ', ' e '),
end
-- Funzione di utilità per Date:diff
local function getMagnitudine(diff, magnitudine_min)
local units = {
Line 124 ⟶ 140:
end
-- Funzione di utilità per
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.
local diff, ret, val, result
diff =
magnitudine = magnitudine or getMagnitudine(diff, magnitudine_min)
Line 156 ⟶ 186:
end
return self:formatResult(result,
end
Line 163 ⟶ 193:
-------------------------------------------------------------------------------
local p = { Date = Date }
-- Entry point per {{#invoke:Data|diff}}
function p.
local args = getArgs(frame)
local success, result = xpcall(function() return parseArgs(args) end, errhandler)
return success and
end
-- Entry point per {{#invoke:Data|diff_ymd}}
function p.
local args = getArgs(frame)
local success, result = xpcall(function() return parseArgs(args) end, errhandler)
return success and
end
-- Entry point per {{#invoke:Data|
function p.
local success, result = xpcall(function() return
return success and (result.d1 == result.d2 and 0 or ( result.d1 < result.d2 and -1 or 1 )) or result
end
|