« Module:Date complexe » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
mAucun résumé des modifications
0x010D (discussion | contributions)
utilisation systématique de Module:Date maintenant que les fonctionnalités nécessaires sont disponibles
Ligne 58 : Ligne 58 :


-- adaptation à mw.formatDate en attendant de passer par Module:Date
-- adaptation à mw.formatDate en attendant de passer par Module:Date
if yearstr then
while #yearstr < 4 do
yearstr = 0 .. yearstr
end
end


local era = dateobject.era
local era = dateobject.era
local linktopic
if (type(displayformat) == 'table' and displayformat.linktopic) then
linktopic = displayformat.linktopic
end
local nolinks
if linktopic == '-' then
nolinks = true
end
local precision = tonumber(dateobject.precision) or numericprecision[dateobject.precision] or guessprecision(dateobject)
local precision = tonumber(dateobject.precision) or numericprecision[dateobject.precision] or guessprecision(dateobject)
if precision == 6 then
if precision == 6 then
Ligne 79 : Ligne 82 :
str = centuryString(century)
str = centuryString(century)
elseif precision == 8 then
elseif precision == 8 then
local decade = tostring(math.floor(dateobject.year/10))  
local decade = tostring(math.floor(dateobject.year/10))
if era ~= '-' then
decade = decade + 1
end
str = decadeString(decade)
str = decadeString(decade)
elseif precision == 9 then
elseif precision == 9 then
str = yearstr:gsub('^0*', '')
str = datemodule.modeleDate{nil, nil, yearstr, displayformat.linktopic, nolinks = nolinks}
elseif precision == 10 then
elseif precision == 10 then
str = mw.language.new('fr'):formatDate('F Y', yearstr .. '-' .. monthstr )
daystr = nil
str = str:gsub('^0*', '')
str = datemodule.modeleDate{nil, monthstr, yearstr, displayformat.linktopic, nolinks = nolinks}
elseif precision == 11  then
else
if (type(displayformat) == 'table' and displayformat.linktopic) then
str = datemodule.modeleDate{daystr, monthstr, yearstr, displayformat.linktopic, nolinks = nolinks}
return datemodule.modeleDate({daystr, monthstr, yearstr, displayformat.linktopic})
end
str = mw.language.new('fr'):formatDate('j F Y', yearstr .. '-' .. monthstr .. '-' .. daystr)
if dateobject.day == 1 then -- ajustement "1 janvier" -> 1er janvier
str = str:gsub('1', "1<sup>er</sup>", 1) -- remplace "1 janvier" par "1er janvier"
end
str = str:gsub('^0*', '')
end
end
if era == '-' then
if era == '-' then

Version du 17 juillet 2015 à 11:18

-- TODO: améliorer les synergies avec Module:Date (gestion par module:Date de dates sans lien et de "XIe siècle en astronautique"

local datemodule = require 'Module:Date' local linguistic = require 'Module:Linguistique' local roman = require 'Module:Romain' local p = {}

local numericprecision = { -- convertir les précisions en valeurs numériques = à celles utilisées par Wikidata gigayear = 0, megayear = 3, millenium = 6, century = 7, decade = 8, year = 9, month = 10, day = 11, hour = 12, minute = 12, second = 14, }

local function vowelfirst(str) return linguistic.vowelfirst(str) end

local function guessprecision(obj) -- précision des dates qui ne viennent pas de Module:Wikidata/Dates local prec = 0 if type(obj) == 'string' then return prec end for i, j in pairs(obj) do if (numericprecision[i] or 0) > prec then prec = numericprecision[i] end end return prec end

local function centuryString(century) return roman.toRoman(century) .. 'e siècle' end

local function milleniumString(millenium) return roman.toRoman(millenium) .. 'e millénaire' end

local function decadeString(decade) return 'années ' .. decade .. '0' end

function p.simplestring(dateobject, displayformat) -- transforme un object date ponctuel en texte -- les dates de type ISO devraient passer par Module:Date, mais il faut pouvoir désactiver les liens if type(dateobject) == 'string' or type(dateobject) == 'nil' then return dateobject end local yearstr, monthstr, daystr= tostring(dateobject.year), tostring(dateobject.month), tostring(dateobject.day)

-- adaptation à mw.formatDate en attendant de passer par Module:Date

local era = dateobject.era local linktopic if (type(displayformat) == 'table' and displayformat.linktopic) then linktopic = displayformat.linktopic end local nolinks if linktopic == '-' then nolinks = true end local precision = tonumber(dateobject.precision) or numericprecision[dateobject.precision] or guessprecision(dateobject) if precision == 6 then local millenium = math.floor(dateobject.year/1000) if era ~= '-' then millenium = millenium + 1 end str = milleniumString(millenium) elseif precision == 7 then local century = math.floor(dateobject.year/100) if era ~= '-' then century = century + 1 end str = centuryString(century) elseif precision == 8 then local decade = tostring(math.floor(dateobject.year/10)) if era ~= '-' then decade = decade + 1 end str = decadeString(decade) elseif precision == 9 then str = datemodule.modeleDate{nil, nil, yearstr, displayformat.linktopic, nolinks = nolinks} elseif precision == 10 then daystr = nil str = datemodule.modeleDate{nil, monthstr, yearstr, displayformat.linktopic, nolinks = nolinks} else str = datemodule.modeleDate{daystr, monthstr, yearstr, displayformat.linktopic, nolinks = nolinks} end if era == '-' then str = str .. ' av. J.-C.' end return str or 'date invalide' end

local function fromdate(d, displayformat) -- retourne "à partir de date" en langage naturel local precision = d.precision or guessprecision(d) local datestr = p.simplestring(d, displayformat) if displayformat and displayformat.textformat == 'minimum' then return datestr -- par exemple pour les classements MH, juste afficher la date de début end if (precision >= 11) or (precision == 7) or (precision == 6) then -- ont dit "à partir du pour les dates avec jour, les siècles, les millénaires return 'à partir du ' .. datestr else if vowelfirst(str) then return "à partir d'" .. datestr else return 'à partir de ' .. datestr end end end

local function upto(d, displayformat) -- retourne "jusqu'à date' en langage naturel local datestring = p.simplestring(d, displayformat) local precision = d.precision or guessprecision(d) if (precision >= 11) or (precision == 7) or (precision == 6) then --on dit "jusqu'au" pour les dates avec jour, et pour les siècles return 'jusqu\'au ' .. datestring elseif (precision >= 9) then return "jusqu'à " .. datestring else return "jusqu\'en " .. datestring end end

local function fromuntillong(startstr, endstr) -- inutile ? -- on dit "du 3 au 14 janvier" mais "de septembe à octobre if precision >= 11 then -- >= day return "du " .. startstr .. " au " .. endstr else if vowelfirst(startstr) then return "d'" .. startstr .. " à ".. endstr else return "de " .. startstr .. " à " .. endstr end end end

local function fromuntil(startpoint, endpoint, displayformat) local precision = endpoint.precision or guessprecision(endpoint) -- may need 2 precisions for start and end dates

local startstr = p.simplestring(startpoint) local endstr = p.simplestring(endpoint) -- à améliorer pour éviter les tournures répétitives comme "du 13 septembre 2006 au 18 september 2006" if not params then params = {} end if params.displayformat == 'long' then return fromuntillong(startstr, endstr) else return startstr .. '-' .. endstr end end

function p.fuzzydate(dateobjet, displayformat) local str = p.simplestring(dateobject, displayformat) return "vers " .. str end

function p.daterange(startpoint, endpoint, displayformat) if startpoint and endpoint then return fromuntil(startpoint, endpoint, displayformat) elseif startpoint then return fromdate(startpoint, displayformat) elseif endpoint then return upto(endpoint, displayformat) else return nil end end

function p.duration(start, ending) if (not start) or (not ending) then return nil -- ? end return datemodule.age(start.year, start.month, start.day, ending.year, ending.month, ending.day) end return p