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

function formatDate(d)
	if precision == 8 then
		return 'anii ' .. tostring(math.floor(math.abs(d.year) / 10) * 10)
	end
	if precision == 9 then
		return d.year
	end
	if precision == 10  then
		return mw.language.getContentLanguage():formatDate('F y', os.date('%A %Y', os.time(d)))
	end
	mw.log('Formatting time' .. os.time(d))
	mw.log('Formatting date' .. os.date('%d %A %Y', os.time(d)))
	return mw.language.getContentLanguage():formatDate('j F y', os.date('%d %A %Y', os.time(d)))
end

p.getBda = function(frame) 
	local args = getArgs(frame)
	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

	return formatDate(d) .. ' (' .. plural{args={computeYearsPastSince(d), 'an'}} .. ')'
end

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

p.getDda = function(frame)
	local args = getArgs(frame)
	local d = {}
	d.year = args[1]
	d.month = args[2] or '1'
	d.day = 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 = args[4]
	b.month = args[5] or '1'
	b.day = 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) .. '(' .. computeYearsPastBetween(d, b) .. ' ani)'
end

p.getDdaByWikidata = function(frame) 
	local birthDates = wikidata.findDateValues('P569', nil)
	local deathDates = wikidata.findDateValues('P570', nil)
	if birthDates and birthDates[1] and deathDates and deathDates[1] then
		return formatDate(deathDates[1]) .. ' (' .. computeYearsPastBetween(deathDates[1], birthDates[1]) .. ' ani)'
	end
	return ''
end

return p