Modulis:Vikidati/Datumi

Vikipēdijas lapa
Jump to navigation Jump to search
Dokumentācijas ikona Moduļa dokumentācija[izveidot]
-- sert à récupérer des données usuelles sur Wikidata (les fonctions élémentaires pour l'extraction des données se fait sur Module:Wikidata
-- ce module peut-être appelé par Module:InfoboxBuilder/Helpers qui les met en infobox
local p = {}
local formatDate = require "Module:Komplekss datums"

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) -- transforme un snak en un nouvel objet utilisable par Module:Date complexe
	if not params then
		params = {}
	end
	
	local newobj = splitTimestamp(orig.time, orig.calendar) -- initalise l'object en mettant la valeur des dates
	
	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égorie'
			birthdate = birthdate .. Cat.makeCategory('Údržba:Chyba ve výpočtu věku')
		end
		if age >= 100 then
			local Cat = require 'Module:Catégorie'
			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égorie'
			deathdate = deathdate .. Cat.makeCategory('Století lidé')
		end
	end

	return deathdate
end

return p