Aller au contenu

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

De Wreck
Modèle:Infobox>Zolo
mAucun résumé des modifications
Modèle:Infobox>Zolo
ce
Ligne 24 : Ligne 24 :
end
end


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


Ligne 68 : Ligne 62 :
end
end


function p.simplestring(dateobject, displayformat, precison)  
function p.simplestring(dateobject, displayformat)  
-- transforme un object date ponctuel en texte
-- 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
-- les dates de type ISO devraient passer par Module:Date, mais il faut pouvoir désactiver les liens
Ligne 75 : Ligne 69 :
end
end
local yearstr, monthstr, daystr= tostring(dateobject.year), tostring(dateobject.month), tostring(dateobject.day)
local yearstr, monthstr, daystr= tostring(dateobject.year), tostring(dateobject.month), tostring(dateobject.day)
 
-- adaptation à mw.formatDate en attendant de passer par Module:Date
-- adaptation à mw.formatDate en attendant de passer par Module:Date
local era = dateobject.era
local era = dateobject.era
Ligne 86 : Ligne 80 :
nolinks = true
nolinks = true
end
end
precision = precision or guessprecision(dateobject, displayformat)
 
if type(precision) == 'string' then
local precision = setprecision(dateobject, displayformat.precision)
precision = numericprecision[precision]
end
if not precision then
precision = guessprecision(dateobject)
end
if precision == 6 then
if precision == 6 then
local millenium = math.floor(dateobject.year/1000)
local millenium = math.floor(dateobject.year/1000)
Ligne 123 : Ligne 113 :


local function fromdate(d, displayformat)  -- retourne "à partir de date" en langage naturel
local function fromdate(d, displayformat)  -- retourne "à partir de date" en langage naturel
local precision = guessprecision(d, displayformat)
local precision = setprecision(d, displayformat)
local datestr = p.simplestring(d, displayformat, precision)
local datestr = p.simplestring(d, displayformat, precision)
if displayformat and displayformat.textformat == 'minimum' then
if displayformat and displayformat.textformat == 'minimum' then
Ligne 141 : Ligne 131 :
local function upto(d, displayformat)  -- retourne "jusqu'à date' en langage naturel
local function upto(d, displayformat)  -- retourne "jusqu'à date' en langage naturel
local datestring = p.simplestring(d, displayformat)
local datestring = p.simplestring(d, displayformat)
local precision = guessprecision(d)
local precision = setprecision(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
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
Ligne 165 : Ligne 155 :


local function fromuntil(startpoint, endpoint, displayformat)
local function fromuntil(startpoint, endpoint, displayformat)
local precision = guessprecision(displayformat, endpoint)
local precision = setprecision(displayformat, endpoint)
local startstr = p.simplestring(startpoint, displayformat)
local startstr = p.simplestring(startpoint, displayformat)
local endstr = p.simplestring(endpoint, displayformat)
local endstr = p.simplestring(endpoint, displayformat)

Version du 21 juillet 2015 à 09:04

La documentation pour ce module peut être créée à Module:Date complexe/doc

-- 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 setprecision(obj, maxprecision)
	if (not obj) or (type(obj) == 'string') then
		return obj
	end
	local precision = tonumber(obj.precision) or numericprecision[obj.precision]
	if maxprecision then
		maxprecision = tonumber(maxprecision) or numericprecision[maxprecision]
	end
	if maxprecision then
		return math.min(precision, maxprecision)
	end
	return precision
end

local function centuryString(century, era)
	local str = roman.toRoman(century) .. '<sup>e</sup> siècle'
	if era == '-' then
		str = str .. ' av. J.-C.'
	end
	return '' -- trop d'erreurs sur Wikidata, en attente du UI correcte
end

local function milleniumString(millenium, era)
	local str = roman.toRoman(millenium) .. '<sup>e</sup> millénaire'
	if era == '-' then
		str = str .. ' av. J.-C.'
	end
	return str
end

local function decadeString(decade, era)
	local str = 'années ' .. decade .. '0'
	if era == '-' then
		str = str .. ' av. J.-C.'
	end
	return str
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
	if not displayformat then
		displayformat = {}
	end
	local linktopic = displayformat.linktopic
	local nolinks
	if linktopic == '-' then
		nolinks = true
	end

	local precision = setprecision(dateobject, displayformat.precision)
	
	if precision == 6 then
		local millenium = math.floor(dateobject.year/1000)
		if era ~= '-' then
			millenium = millenium + 1
		end
		str = milleniumString(millenium, era)
	elseif precision == 7 then
		local century = math.floor(dateobject.year/100)
		if era ~= '-' then
			century = century + 1
		end
		str = centuryString(century, era)
	elseif precision == 8 then
		local decade = tostring(math.floor(dateobject.year/10))
		if era ~= '-' then
			decade = decade + 1
		end
		str = decadeString(decade, era)
	elseif precision == 9 then
		str = datemodule.modeleDate{nil, nil, yearstr, linktopic, nolinks = nolinks}
	elseif precision == 10 then
		daystr = nil
		str = datemodule.modeleDate{nil, monthstr, yearstr, linktopic, nolinks = nolinks}
	else
		str = datemodule.modeleDate{daystr, monthstr, yearstr, linktopic, nolinks = nolinks}
	end
	return str
end

local function fromdate(d, displayformat)  -- retourne "à partir de date" en langage naturel
	local precision = setprecision(d, displayformat)
	local datestr = p.simplestring(d, displayformat, precision)
	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 = setprecision(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 = setprecision(displayformat, endpoint)
	local startstr = p.simplestring(startpoint, displayformat)
	local endstr = p.simplestring(endpoint, displayformat)
	-- à 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