Modul:GregorianDate

De la Wikipedia, enciclopedia liberă
Jump to navigation Jump to search

Modul folosit pentru convertirea unei date din calendarul iulian în data corespunzătoare din calendarul gregorian.


local p = {}

local DateUtils = require('Modul:DateUtils')
local getArgs = require('Modul:Arguments').getArgs

local compareDates = function(date1, date2)
	if date1.year < date2.year then return 1 end
	if date2.year < date1.year then return -1 end

	if date1.month < date2.month then return 1 end
	if date2.month < date1.month then return -1 end

	if date1.day < date2.day then return 1 end
	if date2.day < date1.day then return -1 end
	
	return 0
end

local initialOffset = -3
local limitDates = {
	{year = 4, month = 3, day = 3, calendar = 'julian' },
	{year = 100, month = 3, day = 2, calendar = 'julian' },
	{year = 200, month = 3, day = 1, calendar = 'julian' },
	{year = 300, month = 2, day = 29, calendar = 'julian' },
	{year = 500, month = 2, day = 28, calendar = 'julian' },
	{year = 600, month = 2, day = 27, calendar = 'julian' },
	{year = 700, month = 2, day = 26, calendar = 'julian' },
	{year = 900, month = 2, day = 25, calendar = 'julian' },
	{year = 1000, month = 2, day = 24, calendar = 'julian' },
	{year = 1100, month = 2, day = 23, calendar = 'julian' },
	{year = 1300, month = 2, day = 22, calendar = 'julian' },
	{year = 1400, month = 2, day = 21, calendar = 'julian' },
	{year = 1500, month = 2, day = 20, calendar = 'julian' },
	{year = 1700, month = 2, day = 19, calendar = 'julian' },
	{year = 1800, month = 2, day = 18, calendar = 'julian' },
	{year = 1900, month = 2, day = 17, calendar = 'julian' },
	{year = 2100, month = 2, day = 16, calendar = 'julian' },
	{year = 2200, month = 2, day = 15, calendar = 'julian' },
	{year = 2300, month = 2, day = 14, calendar = 'julian' }
}

p.julianToGregorian = function(indate)
	if indate.calendarmodel ~= 'http://www.wikidata.org/entity/Q1985786' and indate.calendarmodel ~= 'http://www.wikidata.org/entity/Q11184'
		and indate.calendar ~= 'julian' then
			return indate
	end
	local offset = initialOffset
	local limitDateIdx = 1
	while limitDateIdx < #limitDates and compareDates(limitDates[limitDateIdx], indate) >= 0 do
		limitDateIdx = limitDateIdx + 1
		offset = offset + 1
	end
	
	local outputDate = DateUtils.addDaysToDate(indate, offset)
	outputDate.calendar = 'gregorian'
	outputDate.calendarmodel = 'http://www.wikidata.org/entity/Q1985727'
	
	return outputDate
end

local function extractDateFromFrame(frame)
	local outdate = {}
	local frameargs = getArgs(frame)
	outdate.year = tonumber(frameargs[1] or frameargs['year'] or 1)
	outdate.month = tonumber(frameargs[2] or frameargs['month'] or 1)
	outdate.day = tonumber(frameargs[3] or frameargs['day'] or 1)
	outdate.precision = 8
	if frameargs[1] or frameargs['year'] then
		if frameargs[2] or frameargs['month'] then
			if frameargs[3] or frameargs['day'] then
				outdate.precision = outdate.precision + 1
			end
			outdate.precision = outdate.precision + 1
		end
		outdate.precision = outdate.precision + 1
	end
	outdate.calendar = mw.ustring.lower(mw.text.trim(frameargs['calendar'] or 'gregorian'))
	return outdate
end

p.julianToGregorianFromFrame = function(frame)
	local indate = extractDateFromFrame(frame)
	indate.calendar = 'julian'
	return DateUtils.formatDate(p.julianToGregorian(indate))
end

p.convertToGregorianIfInInterval = function(indate)
	local julianDateOfGregorianStart = {year = 1582, month = 10, day = 5}
	local outdate = indate
	if compareDates(julianDateOfGregorianStart, indate) >= 0 then
		outdate = p.julianToGregorian(indate)
	end
	return outdate
end

p.displayDualDateIfInInterval = function(indate, link)
	local outdate = p.convertToGregorianIfInInterval(indate)
	
	local linkstat = { old = false, new = false }
	if (type(link) == 'boolean') and link or link == 'both' or link == 'ambele' then
		linkstat.old = true
		linkstat.new = true
	elseif link == 'old' then
		linkstat.old = true
	elseif link == 'new' then
		linkstat.new = true
	end

	if outdate.year ~= indate.year then
		return DateUtils.formatDate(indate, linkstat.old) .. '/' .. DateUtils.formatDate(outdate, linkstat.new)
	elseif outdate.month ~= indate.month then
		return mw.language.getContentLanguage():formatDate(linkstat.old and '[[j F]]' or 'j F', os.date('%d %B %Y', os.time(indate))) .. '/' .. DateUtils.formatDate(outdate, linkstat.new)
	elseif outdate.day ~= indate.day then
		return (linkstat.old and mw.getContentLanguage():formatDate('[[j F|',  os.date('%d %B %Y', os.time(indate))) or '') .. tostring(indate.day) .. (linkstat.old and ']]' or '') .. '/' .. DateUtils.formatDate(outdate, linkstat.new)
	else 
		return DateUtils.formatDate(outdate, linkstat.new or linkstat.old)
	end
end

p.displayDualDateIfInIntervalFromFrame = function(frame)
	local indate = extractDateFromFrame(frame)
	local args = getArgs(frame)
	indate.calendar = indate.calendar or (frame.args['julian'] and 'julian' or 'gregorian')
	local link = args['link']
	local argslink = args['link']
	if argslink == 'yes' or argslink == 'y' or argslink == 'da' or argslink == 'true' or argslink == 'd' then link = 'both' end
	return p.displayDualDateIfInInterval(indate, link)
end

return p