Modul:Wikiúdaje/dátum

z Wikipédie, slobodnej encyklopédie
Skočit na navigaci Skočit na vyhledávání


Túto dokumentáciu [upraviť] [história] [obnoviť]
Dokumentácia Dokumentácia

Dokumentácia pre tento modul zatiaľ neexistuje. Môžete ju vytvoriť na Modul:Wikiúdaje/dátum/Dokumentácia


Ak máte otázku k tomuto modulu, alebo potrebujete jeho rozšírenie a neviete ho správne naformátovať, pýtajte sa v diskusii k modulu. Pokiaľ je potrebné modul urgentne opraviť, obráťte sa na technickú podporu.

require "Modul:No globals"

local p = {}

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

local function getPrecision(timevalue, options)
	local precision = timevalue.precision
	if options.precision then
		local opt_precision = tonumber(options.precision)
		if opt_precision and opt_precision < precision then
			precision = opt_precision
		end
	end
	return precision
end

local function formatDate(timevalue, options)
	local lang = mw.getContentLanguage()
	local precision = getPrecision(timevalue, options)
	local timestring = timevalue:toString()
	local year_string = 'Y'
	if timevalue.year < 0 then
		BCE = true
		year_string = 'Y" pred Kr.|"Y"&nbsp;pred&nbsp;Kr."'
		timestring = mw.ustring.sub(timestring, 2)
	end
	if precision > 10 then
		timestring = lang:formatDate( '[[j. F]] [[' .. 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. roky %s. storočia', mw.ustring.sub(timestring, 3, 3) .. '0', tonumber(mw.ustring.sub(timestring, 1, 2)) + 1 )
		if BCE then
			timestring = mw.ustring.format( '%s&nbsp;pred&nbsp;Kr.', timestring )
		end
	elseif precision == 7 then
		timestring = mw.ustring.format( '%s.&nbsp;storočie', tonumber(mw.ustring.sub(timestring, 1, 2)) )
		if BCE then
			timestring = mw.ustring.format( '%s&nbsp;pred&nbsp;Kr.', 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.formatDateRange(options, begin, ending)
	if begin then
		if ending then
			local connector = ' – '
			local first_prec, second_prec = getPrecision(begin.value, options), getPrecision(ending.value, options)
			if first_prec == 9 and second_prec == 9 then
				connector = '–'
			end
			return p.formatDate(begin.value, options) .. connector .. p.formatDate(ending.value, options)
		else
			return 'od ' .. p.formatDate(begin.value, options)
		end
	elseif ending then
		return 'do ' .. p.formatDate(ending.value, options)
	end
end

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

	if options.entity.claims.P570 then
		local Wikidata = require 'Modul:Wikidata-test'
		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;rokov)'
		if age < 0 then
			local Cat = require 'Modul:Kategorie'
			birthdate = birthdate .. Cat.makeCategory('Údržba:Chyba ve výpočtu věku')
		end
		if age >= 100 then
			local Cat = require 'Modul:Kategorie'
			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 'Modul:Wikidata-test'
		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 .. ' ( ' .. age .. '&nbsp;rokov)'
		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 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