Module:Durée
Apparence
[voir] [modifier] [historique] [purger]
Utilisation
Fonctions exportables :
duree(frame)
– Fonction principale utilisable dans les modèles._duree(args)
– Fonction pour utilisation dans d'autres modules avec le tableau des paramètres.
Autres fonctions :
joursMois(m, a)
erreur(texte)
determinationMois(mois)
La documentation de ce module est générée par le modèle {{Documentation module}}.
Elle est incluse depuis sa sous-page de documentation. Veuillez placer les catégories sur cette page-là.
Les éditeurs peuvent travailler dans le bac à sable (modifier).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local p = {}
-- liste des mois, écriture exacte et simplifiée, en minuscule
local liste_mois = {
{ "janvier", "jan.", "janv.", "jan", "janv", "january", nJour = 31 },
{ "février", "fevrier", "fev.", "fev", "fév.", "fév", "february", nJour = 28 },
{ "mars", "mar.", "mar", "march", nJour = 31 },
{ "avril", "avr.", "avr", "apr", "april", nJour = 30 },
{ "mai", "may", nJour = 31 },
{ "juin", "jun", "june", nJour = 30 },
{ "juillet", "juil.", "juil", "juill.", "juill", "jul", "july", nJour = 31 },
{ "août", "aout", "aou", "aug", "august", nJour = 31 },
{ "septembre", "sept.", "sept", "sep.", "sep", "september", nJour = 30 },
{ "octobre", "oct.", "oct", "october", nJour = 31 },
{ "novembre", "nov.", "nov", "november", nJour = 30 },
{ "décembre", "decembre", "déc.", "dec.", "dec", "déc", "december", nJour = 31 },
}
local function joursMois( m, a )
a = a or 1
if m == 0 then
return 31
elseif m == 2 then
b = ( a % 4 == 0 ) and ( ( a % 100 ~= 0 ) or ( a % 400 == 0 ) )
return 28 + ( b and 1 or 0 )
else
return liste_mois[m].nJour
end
end
---
local function determinationMois( mois )
if tonumber( mois ) then
local num = math.floor( tonumber( mois ) )
if num > 0 and num <= 12 then
return num
end
elseif type( mois ) == 'string' and mw.text.trim( mois ) ~= '' then
local nom = mw.ustring.lower( mois )
for num = 1, 12 do
local i = 1
while liste_mois[num][i] do
if liste_mois[num][i] == nom then
return num
end
i = i + 1
end
end
end
end
function p._duree( args )
local njour = args.jour2 - args.jour1
local nmois = args.mois2 - args.mois1
local nannee = args.annee2 - args.annee1
if njour < 0 then
nmois = nmois - 1
njour = njour + joursMois( args.mois2 - 1, args.annee2 )
end
if nmois < 0 then
nannee = nannee - 1
nmois = nmois + 12
end
if nannee < 0 then
return '<span class="error">erreur : durée inférieure à 0</span>'
end
local result = {}
local function add( nombre, singulier, pluriel )
if nombre > 0 then
local texte = pluriel
if nombre == 1 then
texte = singulier
end
table.insert( result, nombre .. ' ' .. texte )
end
end
add( nannee, 'an', 'ans' )
add( nmois, 'mois', 'mois' )
add( njour, 'jour', 'jours' )
return mw.text.listToText( result, ' ', ' et ' )
end
function p.duree( frame )
local args = frame:getParent().args
local maintenant = os.date( '!*t' )
local fuseau = mw.getContentLanguage():formatDate("Z", nil, true)/3600
if maintenant.hour + fuseau > 23 then
maintenant.day = maitenant.day + 1
end
local params = {}
params.jour1 = tonumber( args[1] ) or maintenant.day
params.mois1 = determinationMois( args[2] ) or maintenant.month
params.annee1 = tonumber( args[3] ) or maintenant.year
params.jour2 = tonumber( args[4] ) or maintenant.day
params.mois2 = determinationMois( args[5] ) or maintenant.month
params.annee2 = tonumber( args[6]) or maintenant.year
return p._duree( params )
end
return p