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 data = mw.loadData('Modul:GregorianDate/data')

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 = data.limitDates
local julianDateOfGregorianStart = data.julianDateOfGregorianStart

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 < data.limitDatesSize 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 outdate = indate
	if compareDates(julianDateOfGregorianStart, indate) >= 0 then
		outdate = p.julianToGregorian(indate)
	end
	return outdate
end

p.displayDualDateIfInInterval = function(indate, link, dateFormat, julianFormat)
	local outdate = p.convertToGregorianIfInInterval(indate)
	local gregoriandate = compareDates(julianDateOfGregorianStart, indate) < 0 and p.julianToGregorian(indate) or outdate
	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

	local fullDate
	if outdate.year ~= indate.year then
		fullDate = DateUtils.formatDate(indate, linkstat.old, true, julianFormat or dateFormat) .. '/' .. DateUtils.formatDate(outdate, linkstat.new, true, dateFormat)
	elseif outdate.month ~= indate.month then
		fullDate = DateUtils.formatDate(indate, linkstat.old, true, julianFormat or dateFormat or linkstat.old and '[[j F]]' or 'j F') .. '/' .. DateUtils.formatDate(outdate, linkstat.new, true, dateFormat)
	elseif outdate.day ~= indate.day then
		fullDate = DateUtils.formatDate(indate, linkstat.old, true, julianFormat or dateFormat or linkstat.old and '[[j F|j]]' or 'j') .. '/' .. DateUtils.formatDate(outdate, linkstat.new, true, dateFormat)
	else
		fullDate = DateUtils.formatDate(outdate, linkstat.new or linkstat.old, true, dateFormat)
	end

	return indate.precision >= 11 and DateUtils.surroundWithTimeTag(fullDate, gregoriandate) or fullDate
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
	
	local dateFormat = args['format'] or args['date_format'] or args['dateFormat']
	
	return p.displayDualDateIfInInterval(indate, link, dateFormat)
end

return p