Přeskočit na obsah

Modul:Wikidata/Formatters/time

Tato stránka je zamčena
Z Wikipedie, otevřené encyklopedie

require "Modul:No globals"

local p = {}

local lib = require 'Modul:Wikidata/lib'

local function getTimevalue(value)
	local Time = require 'Modul:Time'
	return Time.newFromWikidataValue(value)
end

local function getPrecision(timevalue, opt_precision)
	return math.min(timevalue.precision, opt_precision or 15)
end

local function yearDifference(sooner, later)
	local age = later.year - sooner.year
	--[[ pravděpodobně chybně, prozatím skryto
	if age % 4 == 0 and (age % 100 ~= 0 or age % 400 == 0) then
		age = math.floor(age/4)
	end
	]]--
	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

function p.formatDateFromTimevalue(timevalue, options)
	local lang = mw.getContentLanguage()
	local precision = getPrecision(timevalue, options.precision)
	local timestring = tostring(timevalue)
	local year_string = 'Y'
	local BCE = false
	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

function p.formatDateFromWikidataValue(value, options)
	return p.formatDateFromTimevalue(getTimevalue(value), options)
end

function p.formatDateRange(dates, options)
	if dates.begin then
		if dates.ending then
			local connector = ' – '
			if (
				getPrecision(dates.begin, options.precision) == 9
				and getPrecision(dates.ending, options.precision) == 9
			) then
				connector = '–'
			end
			return p.formatDateFromTimevalue(dates.begin, options)
				.. connector .. p.formatDateFromTimevalue(dates.ending, options)
		else
			return 'od ' .. p.formatDateFromTimevalue(dates.begin, options)
		end
	elseif dates.ending then
		return 'do ' .. p.formatDateFromTimevalue(dates.ending, options)
	end
end

function p.formatBirthdate(value, options)
	local timevalue = getTimevalue(value)
	local birthdate = p.formatDateFromTimevalue(timevalue, options)

	local Statements = options.entity:getBestStatements('P570')
	if #Statements > 0 then
		return birthdate
	end

	if timevalue.precision > 8 then
		local Time = require 'Modul:Time'
		local age = yearDifference(timevalue, Time.new(os.date('!*t')))
		birthdate = birthdate .. ' (' .. age .. '&nbsp;let)'
		if age < 0 then
			birthdate = birthdate .. lib.category('failed-age-computing')
		end
		if age >= 100 then
			local Cat = require 'Modul:Kategorie'
			birthdate = birthdate .. Cat.makeCategory('Století lidé')
		end
	end

	return birthdate
end

function p.formatDeathdate(value, options)
	local deathvalue = getTimevalue(value)
	local deathdate = p.formatDateFromTimevalue(deathvalue, options)

	local birthvalue
	local Formatters = require 'Modul:Wikidata/Formatters'
	local Statements = options.entity:getBestStatements('P569')
	for _, statement in pairs(Statements) do
		if lib.IsSnakValue(statement.mainsnak) then
			birthvalue = Formatters.getRawValue(statement.mainsnak)
			break
		end
	end
	if birthvalue and birthvalue.precision > 8 and deathvalue.precision > 8 then
		local age = yearDifference(birthvalue, deathvalue)
		deathdate = deathdate .. ' (ve věku ' .. age .. '&nbsp;let)'
		if age >= 100 then
			local Cat = require 'Modul:Kategorie'
			deathdate = deathdate .. Cat.makeCategory('Století lidé')
		end
	end

	return deathdate
end

function p.IsSecondLaterThanFirst(first, second)
	if first.year ~= second.year then
		return first.year < second.year
	end
	if first.month ~= second.month then
		return first.month < second.month
	end
	return first.day < second.day
end

-- TODO: metamethod
function p.AreDatesSame(first, second)
	if first.precision == second.precision then
		local precision = first.precision
		if first.year == second.year then
			if precision < 10 then
				return true
			end
			if first.month == second.month then
				if precision < 11 then
					return true
				end
				if first.day == second.day then
					if precision < 12 then
						return true
					end
				end
			end
		end
	end
	return false
end

-- @deprecated
function p.formatDate(...)
	return p.formatDateFromWikidataValue(...)
end

return p