La documentation pour ce module peut être créée à Module:Datum/Documentation

-- ModuleDatum.lua from https://cs.wikipedia.org/wiki/Modul:Wikidata/datum 20150918 from :
-- https://www.mediawiki.org/wiki/Extension_talk:Wikibase_Client/Lua#Get_time_properties_in_ISO_8601_format

local p = {}

local Time = require 'Module:Time'
local BCE = false

local function formatDate(timevalue, options)
	local lang = mw.getContentLanguage()
	local precision = timevalue.precision
	if options.precision and tonumber(options.precision) then
		local opt_precision = tonumber(options.precision)
		if opt_precision < precision then
			precision = opt_precision
		end
	end
	local timestring = timevalue:toString()
	local year_string = 'Y'
	if timevalue.year < 0 then
		BCE = true
		year_string = 'Y" př. n. l.|"Y"&nbsp;př.&nbsp;n.&nbsp;l."'
		timestring = mw.ustring.sub(timestring, 2)
	end
	if precision > 10 then
		timestring = lang:formatDate( '[[j. F|j."&nbsp;"xg]] [[' .. year_string .. ']]', timestring )
	elseif precision == 10 then
		timestring = lang:formatDate( 'F [[' .. year_string .. ']]', timestring )
	elseif precision == 9 then
		timestring = lang:formatDate( '[[' .. year_string .. ']]', timestring )
	elseif precision == 8 then
		timestring = mw.ustring.format( '%s. léta %s. století', mw.ustring.sub(timestring, 3, 3) .. '0', tonumber(mw.ustring.sub(timestring, 1, 2)) + 1 )
		if BCE then
			timestring = mw.ustring.format( '%s&nbsp;př.&nbsp;n.&nbsp;l.', timestring )
		end
	elseif precision == 7 then
		timestring = mw.ustring.format( '%s.&nbsp;století', tonumber(mw.ustring.sub(timestring, 1, 2)) )
		if BCE then
			timestring = mw.ustring.format( '%s&nbsp;př.&nbsp;n.&nbsp;l.', timestring )
		end
	end
--	odstraň nuly na začátku (rok < 1000 a > -1000)
	if mw.ustring.find( timestring, '%[%[0+' ) then
		timestring = mw.ustring.gsub( timestring, '%[%[0+', '[[' )
		timestring = mw.ustring.gsub( timestring, '|0+', '|' )
	end
	return timestring
end

local function yearDifference(sooner, later)
	if not BCE then
		local age = later.year - sooner.year
		if sooner.precision > 10 then
			if later.precision > 10 then
				if sooner.month > later.month then
					return age - 1
				elseif sooner.month == later.month and sooner.day > later.day then
					return age - 1
				end
			elseif later.precision == 10 then
				if sooner.month > later.month then
					return age - 1
				end
			end
		elseif sooner.precision == 10 then
			if later.precision > 9 then
				if sooner.month > later.month then
					return age - 1
				end
			end
		end
		return age
	end
	return nil
end

function p.formatDate(timevalue, options)
	local timevalue = Time.newFromWikidataValue(timevalue)
	return formatDate(timevalue, options)
end

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

	if options.entity.claims.P570 then
		local Wikidata = require 'Modul: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 .. '&nbsp;let)'
		if age < 0 then
			birthdate = birthdate .. '[[Kategorie:Údržba:Chyba ve výpočtu věku]]'
		end
		if age >= 100 then
			birthdate = birthdate .. '[[Kategorie: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 'Modul: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
			deathdate = deathdate .. '[[Kategorie:Století lidé]]'
		end
	end

	return deathdate
end

function p.IsSecondLaterThanFirst(first, second)
	if first.year < second.year then
		return true
	elseif first.year > second.year then
		return false
	end
	if first.month < second.month then
		return true
	elseif first.month > second.month then
		return false
	end
	if first.day < second.day then
		return true
	end
	return false
end

function p.IsSecondSameAsFirst(first, second)
	if first.year == second.year then
		if first.precision == 9 or second.precision == 9 then
			return true
		end
		if first.month == second.month then
			if first.precision == 10 or second.precision == 10 then
				return true
			end
			if first.day == second.day then
				return true
			end
		end
	end
	return false
end

return p