La documentation pour ce module peut être créée à Module:Wikidata/Dâtes/doc

-- sèrvét a rècupèrar de balyês d’usâjo dessus Wikidata (les fonccions èlèmentères por l’èxtraccion de les balyês sè fât dessus Modulo:Wikidata
-- cél modulo pôt étre apelâ per Modulo:EnfocajonBuilder/Helpers que les bete en enfocajon
local p = {}
local formatDate = require "Module:Dâta complèxa"

local function splitTimestamp(timestamp, calendar)
	local pattern = "(%W)(%d+)%-(%d+)%-(%d+)"
	local era, year, month, day = timestamp:match(pattern)
	year, month, day = tonumber(year), tonumber(month), tonumber(day)

	if calendar == 'julian' then
		year, month, day = formatdate.gregorianToJulian( era .. year, month, day )
	end

	return {day = day, month = month, year = year, era = era, timestamp = timestamp, type = 'dateobject'}
end

function p.rangeobject(begin, ending, params)
	local timestamp
	if begin then
		timestamp = begin.timestamp
	else
		timestamp = ending.timestamp
	end
	return {begin = begin, ending = ending, timestamp = timestamp, type = 'rangeobject'}
end

function p.dateobject(orig, params) -- transfôrme un snak en na chousa novèla empleyâbla per Modulo:Dâta complèxa
	if not params then
		params = {}
	end
	
	local newobj = splitTimestamp(orig.time, orig.calendar) -- inicialise la chousa en betent la valor de les dâtes
	
	newobj.precision = params.precision or orig.precision
	newobj.type = 'dateobject'
	return newobj
end

function p.objecttotext(obj, params)
	if obj.type == 'dateobject' then
		return formatDate.simplestring(obj, params)
	elseif obj.type == 'rangeobject' then
		return formatDate.daterange(obj.begin, obj.ending, params)
	end
end

function p.formatBirthdate(timevalue, options)
	local birthvalue, deathvalue = Time.newFromWikidataValue(timevalue), nil

	if options.entity.claims.P570 then
		local Wikidata = require 'Module:Wikidata'
		local Filtered = Wikidata.filterStatementsFromLua(options.entity, { limit = 1, property = 'P570', rank = 'best' })
		if #Filtered > 0 then
			 deathvalue = Time.newFromWikidataValue(Filtered[1].mainsnak.datavalue.value)
		end
	end

	local birthdate = formatDate(birthvalue, options)
	local age
	if not deathvalue and birthvalue.precision > 8 then
		age = yearDifference(birthvalue, Time.new(os.date('!*t')))
	end
	if age then
		birthdate = birthdate .. ' (' .. age .. ' let)'
		if age < 0 then
			local Cat = require 'Module:Catègoria'
			birthdate = birthdate .. Cat.makeCategory('Údržba:Chyba ve výpočtu věku')
		end
		if age >= 100 then
			local Cat = require 'Module:Catègoria'
			birthdate = birthdate .. Cat.makeCategory('Století lidé')
		end
	end

	return birthdate
end

function p.formatDeathdate(timevalue, options)
	local deathvalue, birthvalue = Time.newFromWikidataValue(timevalue), nil

	if options.entity.claims.P569 then
		local Wikidata = require 'Module:Wikidata'
		local Filtered = Wikidata.filterStatementsFromLua(options.entity, { limit = 1, property = 'P569', rank = 'best' })
		if #Filtered > 0 then
			 birthvalue = Time.newFromWikidataValue(Filtered[1].mainsnak.datavalue.value)
		end
	end

	local deathdate = formatDate(deathvalue, options)
	local age
	if birthvalue and deathvalue.precision > 8 then
		age = yearDifference(birthvalue, deathvalue)
	end
	if age then
		deathdate = deathdate .. ' (ve věku ' .. age .. '&nbsp;let)'
		if age >= 100 then
			local Cat = require 'Module:Catègoria'
			deathdate = deathdate .. Cat.makeCategory('Století lidé')
		end
	end

	return deathdate
end

return p