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 i18n = mw.loadData("Modul:Wikidata/i18n")

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 okPrecision(timevalue, opt_precision, target_precision)
	if timevalue == 'somevalue' or timevalue == 'novalue' then
		return target_precision < 10
	end
	return target_precision == getPrecision(timevalue, opt_precision)
end

function p.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
				age = age - 1
			elseif sooner.month == later.month and sooner.day > later.day then
				age = age - 1
			end
			return age, age
		elseif later.precision == 10 then
			if sooner.month == later.month then
				return age - 1, age - 1 .. '–' .. age
			end
			if sooner.month > later.month then
				age = age - 1
			end
			return age, age
		end
	elseif sooner.precision == 10 then
		if later.precision > 9 then
			if sooner.month == later.month then
				return age - 1, age - 1 .. '–' .. age
			end
			if sooner.month > later.month then
				age = age - 1
			end
			return age, age
		end
	end
	return age - 1, age - 1 .. '–' .. age
end

function p.formatDateFromTimevalue(timevalue, options)
	local lang = mw.getContentLanguage()
	local precision = getPrecision(timevalue, options.precision)
	local timestring = tostring(timevalue)
	local year_string = i18n.date.year
	local BCE = timevalue.year < 0
	if BCE then
		year_string = i18n.date['year-BCE']
		timestring = mw.ustring.sub(timestring, 2)
	end

	local century = tonumber(mw.ustring.sub(timestring, 1, 2))
	if tonumber(mw.ustring.sub(timestring, 3, 4)) > 0 then
		century = century + 1
	end
	if precision > 10 then
		timestring = lang:formatDate( mw.ustring.format( i18n.date['year-month-day'], year_string ), timestring )
	elseif precision == 10 then
		timestring = lang:formatDate( mw.ustring.format( i18n.date['year-month'], year_string ), timestring )
	elseif precision == 9 then
		timestring = lang:formatDate( mw.ustring.format( '[[%s]]', year_string ), timestring )
	elseif precision == 8 then
		local dec = tonumber(mw.ustring.sub(timestring, 3, 3)) * 10
		timestring = mw.ustring.format( i18n.date.decade, dec + (century - 1) * 100 )
		if BCE then
			timestring = mw.ustring.format( i18n.date.BCE, timestring )
		end
	elseif precision == 7 then
		timestring = mw.ustring.format( i18n.date.century, century )
		if BCE then
			timestring = mw.ustring.format( i18n.date.BCE, timestring )
		end
	elseif precision == 6 then
		local mill = tonumber(mw.ustring.sub(timestring, 1, 1))
		if century % 10 > 0 then
			mill = mill + 1
		end
		timestring = mw.ustring.format( i18n.date.millenium, mill )
		if BCE then
			timestring = mw.ustring.format( i18n.date.BCE, timestring )
		end
	end
	if lib.IsOptionTrue(options, 'nolink') then
		timestring = mw.ustring.gsub( timestring, '%[%[[^]|]+|', '[[' )
		timestring = mw.ustring.gsub( timestring, '%[%[([^]|]+)%]%]', '%1' )
	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

local function formatDateOrSpecial(timevalue, options)
	if timevalue == 'somevalue' then
		return '?'
	else
		return p.formatDateFromTimevalue(timevalue, options)
	end
end

function p.formatDateRange(dates, options)
	if not (dates.begin or dates.ending) then
		return error()
	end
	if not dates.begin then
		return mw.ustring.format(i18n.date['end'], formatDateOrSpecial(dates.ending, options))
	end
	if not dates.ending then
		return mw.ustring.format(i18n.date['start'], formatDateOrSpecial(dates.begin, options))
	end

	local begin = formatDateOrSpecial(dates.begin, options)
	local ending = formatDateOrSpecial(dates.ending, options)
	if begin == ending then
		return begin
	end
	local connector = ' – '
	if okPrecision(dates.begin, options.precision, 9) and okPrecision(dates.ending, options.precision, 9) then
		connector = '–'
	end
	return table.concat( { begin, ending }, connector )
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, age_text = p.yearDifference(timevalue, Time.new(os.date('!*t')))
		birthdate = mw.ustring.format('%s (%s)', birthdate,
			mw.getCurrentFrame():preprocess( 
				mw.message.newRawMessage(i18n.date.age)
					:params(age_text)
					:numParams(age)
					:plain()
			)
		)
		if age < 0 then
			birthdate = birthdate .. lib.category('failed-age-computing')
		end
		if age >= 100 then
			local Cat = require 'Modul:Kategorie'
			birthdate = birthdate .. lib.category('centenarians-living')
		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, age_text = p.yearDifference(birthvalue, deathvalue)
		deathdate = mw.ustring.format('%s (%s)', deathdate,
			mw.getCurrentFrame():preprocess(
				mw.message.newRawMessage(i18n.date['in-the-age'])
					:params(age_text)
					:numParams(age)
					:plain()
			)
		)
		if age >= 100 then
			local Cat = require 'Modul:Kategorie'
			deathdate = deathdate .. lib.category('centenarians')
		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

-- fixme: metamethod
function p.AreDatesSame(first, second, strict)
	strict = strict or strict == nil -- b/c
	if first.precision ~= second.precision and not strict then
		return false
	end
	local min_precision = math.min(first.precision, second.precision)
	if first.year == second.year then
		if min_precision < 10 then
			return true
		end
		if first.month == second.month then
			if min_precision < 11 then
				return true
			end
			if first.day == second.day then
				if min_precision < 12 then
					return true
				end
			end
		end
	end
	return false
end

return p