Sari la conținut

Modul:BirthDateAndAge

Permanently protected module
De la Wikipedia, enciclopedia liberă
local p = {}
local getArgs = require('Modul:Arguments').getArgs
local wikidata = require('Modul:Wikidata')
local plural = require('Modul:Plural').get_plural

local computeYearsPastBetween = function(momentDate, referenceDate)
	local yearsDiff = tonumber(momentDate.year) - tonumber(referenceDate.year)
	if tonumber(momentDate.month) < tonumber(referenceDate.month) or (tonumber(momentDate.month) == tonumber(referenceDate.month) and tonumber(momentDate.day) < tonumber(referenceDate.day)) then
		yearsDiff = yearsDiff - 1
	end
	return yearsDiff	
end

local computeYearsPastSince = function(referenceDate) 
	return computeYearsPastBetween(os.date("*t"), referenceDate)
end

local suffixFormatYear = function(y)
	return (y < 0 and ' î.e.n.' or (y < 1000 and ' e.n.' or ''))
end

local formatYear = function(y, link)
	local out = ''
	if link and mw.text.trim(link) ~= '' then	out = out .. '[[' end
	out = out .. tostring(math.abs(y))
	if y > 0 and link and mw.text.trim(link) ~= '' then out = out .. ']]' end
	out = out .. suffixFormatYear(y)
	if y < 0 and link and mw.text.trim(link) ~= '' then out = out .. ']]' end
	return out
end

local formatDate = function(d, link)
	if d.precision == 7 then
		return 'secolul ' .. tostring(math.floor(1 + math.abs(d.year) / 100)) .. suffixFormatYear(d.year)
	end
	if d.precision == 8 then
		return 'anii ' .. tostring(math.floor(math.abs(d.year) / 10) * 10) .. suffixFormatYear(d.year)
	end
	if d.precision == 9 then
		return formatYear(d.year)
	end
	if d.precision > 9  then
		local d1 = {}
		d1.day = d.day
		d1.month = d.month
		d1.year = math.abs(d.year)
		local out = ''
		if link and mw.text.trim(link) ~= '' then	out = out .. '[[' end
		out = out ..mw.language.getContentLanguage():formatDate((d.precision >= 11) and 'j F' or 'F', os.date('%d %B %Y', os.time(d1)))
		if link and mw.text.trim(link) ~= '' then	out = out .. ']]' end
		out = out .. ' ' .. formatYear(d.year, link)
		return out
	end
end

p.getBdaByWikidata = function(frame) 
	local args = getArgs(frame)
	local link = args['link']
	local birthDates = wikidata.findDateValues('P569', nil)
	local deathDates = wikidata.findDateValues('P570', nil)
	if birthDates and birthDates[1] then
		local out = formatDate(birthDates[1], link)
		if not deathDates or not deathDates[1] then
			out = out .. ' (' .. plural{args={computeYearsPastSince(birthDates[1]), 'an'}} .. ')'
		end
		return out
	end
	return ''
end

p.getBda = function(frame) 
	local args = getArgs(frame)
	local link = args['link']
	local d = {}
	if args[1] then
		d.year = tonumber(args[1])
		d.month = tonumber(args[2] or '1')
		d.day = tonumber(args[3] or '1')
		d.precision = 9
		if args[2] then d.precision = 10 end
		if args[3] then d.precision = 11 end
		return formatDate(d, link) .. ' (' .. plural{args={computeYearsPastSince(d), 'an'}} .. ')'
	else
		return p.getBdaByWikidata(frame)
	end
end

p.getDdaByWikidata = function(frame) 
	local birthDates = wikidata.findDateValues('P569', nil)
	local deathDates = wikidata.findDateValues('P570', nil)
	local args = getArgs(frame)
	local link = args['link']
	if birthDates and birthDates[1] and deathDates and deathDates[1] then
		local out = ''
		out = out .. formatDate(deathDates[1], link)
		if deathDates[1].precision > 7 and birthDates[1].precision > 7 then
			out = out .. ' (' .. plural{args={computeYearsPastBetween(deathDates[1], birthDates[1]), 'an'}} .. ')'
		end
		return out
	end
	return ''
end

p.getDda = function(frame)
	local args = getArgs(frame)
	local link = args['link']
	if args[1] then
		local d = {}
		d.year = tonumber(args[1])
		d.month = tonumber(args[2] or '1')
		d.day = tonumber(args[3] or '1')
		d.precision = 9
		if args[2] then d.precision = 10 end
		if args[3] then d.precision = 11 end
		local b = {}
		b.year = tonumber(args[4])
		b.month = tonumber(args[5] or '1')
		b.day = tonumber(args[6] or '1')
		b.precision = 9
		if args[5] then b.precision = 10 end
		if args[6] then b.precision = 11 end
		return formatDate(d, link) .. ' (' ..  plural{args={computeYearsPastBetween(d, b), 'an'}} .. ')'
	else
		return p.getDdaByWikidata(frame)
	end
end

return p