« Module:Date complexe » : différence entre les versions
Apparence
Modèle:Infobox>Zolo Nouvelle page : -- Translates and format date range sand other non punctual dates --inspired by Commons:Module:Other dates local datemodule = require('Module:Date') local p = {} --[[ wikibase... |
Modèle:Infobox>Zolo Aucun résumé des modifications |
||
| Ligne 1 : | Ligne 1 : | ||
local datemodule = require('Module:Date') | local datemodule = require('Module:Date') | ||
local linguistic = require('Module:Linguisique') | |||
local p = {} | local p = {} | ||
local function | local numericprecision = { -- convertir les précisions en valeurs numériques = à celles utilisées par Wikidata | ||
return | 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 | end | ||
local function | local function centuryString(centurynumber) | ||
return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { century }} .. '<sup>e</sup> siècle' | |||
end | end | ||
local function decadeString(decadenumber) | |||
return 'années ' .. decade .. '0' | |||
end | |||
function p.simplestring(dateobject) -- transforme un object date ponctuel en texte | |||
local precision = | local str | ||
local year, month, day = dateobject.year, dateobject.month, dateobject.day | |||
local era = dateobject.era | |||
local precision = dateobject.precision | |||
if object.precision == 7 then | |||
local century = tostring(math.floor(year/100)) | |||
str = centuryString(century) | |||
elseif precision == 8 then | |||
local decade = tostring(math.floor(year/10)) | |||
str = decadestring(decade) | |||
elseif precision == 9 then | |||
str = tostring(year) | |||
elseif precision == 10 then | |||
str =mw.language.new('fr'):formatDate('F Y', year .. '-' .. month) -- devrait utiliser module date mais ne faut pas de lien | |||
elseif precision == 11 then | |||
str = mw.language.new('fr'):formatDate('j F Y', day .. '-' .. year .. '-' .. month) | |||
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" | |||
end | end | ||
return | end | ||
if era == '-' then | |||
str = str .. ' av. J.-C.' | |||
end | |||
return str | |||
end | end | ||
local function fromdate(d) -- retourne "à partir de date" en langage naturel | |||
local function | local str = simplestring(d) | ||
local | |||
if d.precision >= 11 then | |||
return 'à partir du ' .. datestring | |||
else | |||
if vowelfirst(str) then | |||
return "à partir d'" .. datestring | |||
else | |||
return 'à partir de ' .. datestring | |||
end | end | ||
end | |||
end | end | ||
function p. | function p.upto(d) -- retourne "jusqu'à date' en langage naturel | ||
local | local datestring = p.simplestring(d) | ||
if | local precision = d.precision | ||
return | if (precision >= 11) then | ||
return 'jusqu\'au ' .. datestring | |||
elseif (precision >= 9) then | |||
return "jusqu'à " .. datestring | |||
else | |||
return "jusqu\'en " .. datestring | |||
end | end | ||
end | |||
end | local function fromuntil(startpoint, endpoint) | ||
local precision = endpoint.precision -- may need 2 precisions for start and end dates | |||
local startstr = p.simplestring(startpoint) | |||
local endstr = p.simplestring(endpoint) | |||
if | -- à améliorer pour éviter les tournures répétitives comme "du 13 septembre 2006 au 18 september 2006" | ||
return | |||
-- on dit "du 3 au 14 janvier" mais "de septembe à octobre | |||
if precision >= 11 then -- >= day | |||
return "du " .. startsr .. " au " .. endstr | |||
else | |||
if vowelfirst(startstr) then | |||
return "d'" .. startstr .. " à ".. endstr | |||
else | |||
return "de " .. startstr .. " à " .. endstr | |||
end | |||
end | end | ||
end | end | ||
function p. | function p.fuzzydate(dateobjet) | ||
local | local str = simplestring(dateobject) | ||
if | 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 | ||
end | end | ||
return p | return p | ||
Version du 18 janvier 2015 à 16:42
La documentation pour ce module peut être créée à Module:Date complexe/doc
local datemodule = require('Module:Date')
local linguistic = require('Module:Linguisique')
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 centuryString(centurynumber)
return mw.getCurrentFrame():expandTemplate{ title = 'Nombre en romain', args = { century }} .. '<sup>e</sup> siècle'
end
local function decadeString(decadenumber)
return 'années ' .. decade .. '0'
end
function p.simplestring(dateobject) -- transforme un object date ponctuel en texte
local str
local year, month, day = dateobject.year, dateobject.month, dateobject.day
local era = dateobject.era
local precision = dateobject.precision
if object.precision == 7 then
local century = tostring(math.floor(year/100))
str = centuryString(century)
elseif precision == 8 then
local decade = tostring(math.floor(year/10))
str = decadestring(decade)
elseif precision == 9 then
str = tostring(year)
elseif precision == 10 then
str =mw.language.new('fr'):formatDate('F Y', year .. '-' .. month) -- devrait utiliser module date mais ne faut pas de lien
elseif precision == 11 then
str = mw.language.new('fr'):formatDate('j F Y', day .. '-' .. year .. '-' .. month)
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"
end
end
if era == '-' then
str = str .. ' av. J.-C.'
end
return str
end
local function fromdate(d) -- retourne "à partir de date" en langage naturel
local str = simplestring(d)
if d.precision >= 11 then
return 'à partir du ' .. datestring
else
if vowelfirst(str) then
return "à partir d'" .. datestring
else
return 'à partir de ' .. datestring
end
end
end
function p.upto(d) -- retourne "jusqu'à date' en langage naturel
local datestring = p.simplestring(d)
local precision = d.precision
if (precision >= 11) then
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 -- 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 " .. startsr .. " 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 = 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
return p