Modulo:Data: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
rimosse variabili inutilizzate e vecchio controllo |
rinominati from e to in inizio e fine, aggiunto segno nel risultato e parametro direzione |
||
Riga 15:
end
-- Controlla args.
local function parseArgs(args)
if not args.
error('la data di partenza è obbligatoria')
elseif not isValidDate(args.
error('la data di partenza non è valida')
elseif not args.
error('la data di arrivo è obbligatoria')
elseif not isValidDate(args.
error('la data di arrivo non è valida')
end
return {
d1 = tonumber(mw.getContentLanguage():formatDate('U', args.
d2 = tonumber(mw.getContentLanguage():formatDate('U', args.
}
end
Riga 36:
local function errhandler(msg)
return string.format('<span class="error">Errore: %s</span>', msg:match('.+%d:%s(.+)$'))
end
-- Aggiunge un eventuale prefisso e suffisso al risultato invece del segno
local function formatResult(response, past, direzione)
local ret
if direzione then
ret = past and response .. ' fa' or 'tra ' .. response
else
ret = past and '-' .. response or response
end
return ret
end
Line 43 ⟶ 54:
-- Ritorna la differenza in anni, mesi e giorni tra le date d1 e d2 (Unix time).
-- Se rawTable è true ritorna una table con le chiavi: year, month, day, seconds
local function dateDiffYMD(d1, d2, rawTable, direzione)
local monthdays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
local ret = { seconds = math.abs(d1 - d2) }
local past = true
if d1 >= d2 then
d1, d2 = d2, d1 past = false
end
d1, d2 = os.date('*t', d1), os.date('*t', d2)
Line 75 ⟶ 90:
end
return rawTable and ret or formatResult(mw.text.listToText(ret, ', ', ' e '), past, direzione)
end
Line 118 ⟶ 133:
-- 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 function dateDiff(d1, d2, magnitudine, magnitudine_min, direzione)
local diff, ret, val
Line 141 ⟶ 156:
end
return formatResult(ret, d1
end
Line 153 ⟶ 168:
function p._diff(args)
local success, result = xpcall(function() return parseArgs(args) end, errhandler)
return success and dateDiff(result.d1, result.d2, args.magnitudine, args['magnitudine min'], args.direzione) or result
end
Line 159 ⟶ 174:
function p._diff_ymd(args)
local success, result = xpcall(function() return parseArgs(args) end, errhandler)
return success and dateDiffYMD(result.d1, result.d2, false, args.direzione) or result
end
|