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

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
Aucun résumé des modifications
0x010D (discussion | contributions)
Aucun résumé des modifications
Ligne 19 : Ligne 19 :
local function vowelfirst(str)
local function vowelfirst(str)
return linguistic.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
for i, j in pairs(obj) do
if (numericprecision[i] or 0) > prec then
prec = numericprecision[i]
end
end
return prec
end
end


local function centuryString(centurynumber)
local function centuryString(centurynumber)
return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { century }} .. '<sup>e</sup> siècle'
return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { centurynumber + 1 }} .. '<sup>e</sup> siècle'
end
 
local function centuryString(centurynumber)
return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { centurynumber + 1 }} .. '<sup>e</sup> millénaire'
end
end


Ligne 29 : Ligne 43 :
end
end


function p.simplestring(dateobject) -- transforme un object date ponctuel en texte
function p.simplestring(dateobject)  
local str
-- transforme un object date ponctuel en texte
local year, month, day = dateobject.year, dateobject.month, dateobject.day
-- les dates de type ISO devraient passer par Module:Date, mais il faut pouvoir désactiver les liens
local yearstr, monthstr, daystr= tostring(dateobject.year), tostring(dateobject.month), tostring(dateobject.day)
 
-- 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 precision = dateobject.precision
local precision = dateobject.precision or guessprecision(dateobject)
if object.precision == 7 then
local century = tostring(math.floor(year/100))
if precision == 6 then
local century = tostring(math.floor(dateobject.year/100))
str = centuryString(century)
elseif precision == 7 then
local century = tostring(math.floor(dateobject.year/100))
str = centuryString(century)
str = centuryString(century)
elseif precision == 8 then
elseif precision == 8 then
local decade = tostring(math.floor(year/10))  
local decade = tostring(math.floor(dateobject.year/10))  
str = decadestring(decade)
str = decadestring(decade)
elseif precision == 9 then
elseif precision == 9 then
str = tostring(year)
str = yearstr
elseif precision == 10 then
elseif precision == 10 then
str =mw.language.new('fr'):formatDate('F Y', year .. '-' .. month) -- devrait utiliser module date mais ne faut pas de lien
str =mw.language.new('fr'):formatDate('F Y', yearstr .. '-' .. monthstr )  
if dateobject.year < 1000 then -- enlève les zéros en trop
str = string.gsub(str, '0', '')
end
elseif precision == 11  then
elseif precision == 11  then
str = mw.language.new('fr'):formatDate('j F Y', day .. '-' .. year .. '-' .. month)
str = mw.language.new('fr'):formatDate('j F Y', yearstr .. '-' .. monthstr .. '-' .. daystr)
if day == 1 then -- ajustement "1 janvier" -> 1er janvier
if day == 1 then -- ajustement "1 janvier" -> 1er janvier
str = string.gsub(t, '1', "1<sup>er</sup>", 1) -- remplacer "1 janvier" par "1er janvier"
str = string.gsub(t, '1', "1<sup>er</sup>", 1) -- remplacer "1 janvier" par "1er janvier"
end
if dateobject.year < 1000 then
str = string.gsub(str, '0', '')
end
end
end
end
Ligne 53 : Ligne 86 :
str = str .. ' av. J.-C.'
str = str .. ' av. J.-C.'
end
end
return str
return str or 'date invalide'
end
end


local function fromdate(d)  -- retourne "à partir de date" en langage naturel
local function fromdate(d)  -- retourne "à partir de date" en langage naturel
local str = simplestring(d)
local precision = d.precision or guessprecision(d)
local datestr = p.simplestring(d)
if d.precision >= 11 then
 
return 'à partir du ' .. datestring
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
else
if vowelfirst(str) then
if vowelfirst(str) then
return "à partir d'" .. datestring
return "à partir d'" .. datestr
else
else
return 'à partir de ' .. datestring
return 'à partir de ' .. datestr
end
end
end
end
end
end


function p.upto(d)  -- retourne "jusqu'à date' en langage naturel
local function upto(d)  -- retourne "jusqu'à date' en langage naturel
local datestring = p.simplestring(d)
local datestring = p.simplestring(d)
local precision = d.precision
local precision = d.precision or guessprecision(d)
if (precision >= 11) then  
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
return 'jusqu\'au ' .. datestring
elseif (precision >= 9) then
elseif (precision >= 9) then
Ligne 83 : Ligne 117 :


local function fromuntil(startpoint, endpoint)
local function fromuntil(startpoint, endpoint)
local precision = endpoint.precision -- may need 2 precisions for start and end dates
local precision = endpoint.precision or guessprecision(endpoint) -- may need 2 precisions for start and end dates
local startstr = p.simplestring(startpoint)
local startstr = p.simplestring(startpoint)
local endstr = p.simplestring(endpoint)
local endstr = p.simplestring(endpoint)
Ligne 90 : Ligne 124 :
-- on dit "du 3 au 14 janvier" mais "de septembe à octobre
-- on dit "du 3 au 14 janvier" mais "de septembe à octobre
if precision >= 11 then -- >= day
if precision >= 11 then -- >= day
return "du " .. startsr .. " au " ..  endstr
return "du " .. startstr .. " au " ..  endstr
else
else
if vowelfirst(startstr) then
if vowelfirst(startstr) then
Ligne 101 : Ligne 135 :


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


function p.daterange(startpointobject, endpointobject)
function p.daterange(startpointobject, endpointobject)  
if startpointobject and endpointobject then
if startpointobject and endpointobject then
return fromuntil(startpointobject, endpointobject)
return fromuntil(startpointobject, endpointobject)
Ligne 116 : Ligne 150 :
end
end
end
end
return p

Version du 18 janvier 2015 à 16:25

local datemodule = require('Module:Date') local linguistic = require('Module:Linguistique') 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 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(centurynumber) return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { centurynumber + 1 }} .. 'e siècle' end

local function centuryString(centurynumber) return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { centurynumber + 1 }} .. 'e millénaire' end

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

function p.simplestring(dateobject) -- 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 local yearstr, monthstr, daystr= tostring(dateobject.year), tostring(dateobject.month), tostring(dateobject.day)

-- 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 precision = dateobject.precision or guessprecision(dateobject)

if precision == 6 then local century = tostring(math.floor(dateobject.year/100)) str = centuryString(century) elseif precision == 7 then local century = tostring(math.floor(dateobject.year/100)) str = centuryString(century) elseif precision == 8 then local decade = tostring(math.floor(dateobject.year/10)) str = decadestring(decade) elseif precision == 9 then str = yearstr elseif precision == 10 then str =mw.language.new('fr'):formatDate('F Y', yearstr .. '-' .. monthstr ) if dateobject.year < 1000 then -- enlève les zéros en trop str = string.gsub(str, '0', ) end elseif precision == 11 then str = mw.language.new('fr'):formatDate('j F Y', yearstr .. '-' .. monthstr .. '-' .. daystr) if day == 1 then -- ajustement "1 janvier" -> 1er janvier str = string.gsub(t, '1', "1er", 1) -- remplacer "1 janvier" par "1er janvier" end if dateobject.year < 1000 then str = string.gsub(str, '0', ) end end if era == '-' then str = str .. ' av. J.-C.' end return str or 'date invalide' end

local function fromdate(d) -- retourne "à partir de date" en langage naturel local precision = d.precision or guessprecision(d) local datestr = p.simplestring(d)

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) -- retourne "jusqu'à date' en langage naturel local datestring = p.simplestring(d) 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 fromuntil(startpoint, endpoint) 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"

-- 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

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

function p.daterange(startpointobject, endpointobject) if startpointobject and endpointobject then return fromuntil(startpointobject, endpointobject) elseif startpointobject then return fromdate(startpointobject) elseif endpointobject then return upto(endpointobject) else return nil end end