$2, $3 dagi (joriy) koʻrinishi

Bu modul uchun Modul:Old Style New Style converter/doc nomli hujjat sahifasini yaratishingiz mumkin


local getArgs = require('Module:Arguments').getArgs
local calendrical = require('Module:Calendrical Calculations')

local p = {}
local monthNames = { 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' }

local function callCalendrical(frame, functionName, ...)
	local childArgs = {}
	for i = 1, select('#', ...) do
		childArgs[i] = tostring(select(i, ...))
	end

	local childFrame = frame:newChild { title = 'Module:Calendrical Calculations', args = childArgs }
	local func = calendrical[functionName]

	return func(childFrame)
end

local function parseDateTriple(value, functionName)
	value = mw.text.trim(tostring(value))
	local year, month, day = value:match('^(-?%d+)%-(%d+)%-(%d+)$')

	if not year then
		error(string.format('Module:Calendrical Calculations returned an unexpected value from %s: %s', functionName, value), 0)
	end

	return { year = tonumber(year), month = tonumber(month), day = tonumber(day) }
end

local function sourceDateFromArgs(args)
	local year = tonumber(args[1])
	local month = tonumber(args[2])
	local day = tonumber(args[3])

	if not year or not month or not day or year % 1 ~= 0 or month % 1 ~= 0 or day % 1 ~= 0 then
		error('The year, month and day must all be integers', 0)
	end

	return { year = year, month = month, day = day }
end

-- The Julian functions in Module:Calendrical Calculations do not use a year zero, but #time does. So this converts between them
local function toCalendricalJulianYear(year)
	if year <= 0 then
		return year - 1
	end
	return year
end

local function fromCalendricalJulianYear(year)
	if year < 0 then
		return year + 1
	end
	return year
end

local function fixedFromGregorian(frame, date)
	local fixed = tonumber(callCalendrical(frame, 'fixed_from_gregorian', date.year, date.month, date.day))
	if not fixed then
		error('fixed_from_gregorian did not return a number', 0)
	end
	return fixed
end

local function fixedFromJulian(frame, date)
	local fixed = tonumber(callCalendrical(frame, 'fixed_from_julian', toCalendricalJulianYear(date.year), date.month, date.day))
	if not fixed then
		error('fixed_from_julian did not return a number', 0)
	end
	return fixed
end

local function gregorianFromFixed(frame, fixed)
	return parseDateTriple(callCalendrical(frame, 'gregorian_from_fixed', fixed), 'gregorian_from_fixed')
end

local function julianFromFixed(frame, fixed)
	local date = parseDateTriple(callCalendrical(frame, 'julian_from_fixed', fixed), 'julian_from_fixed')
	date.year = fromCalendricalJulianYear(date.year)
	return date
end

local function normalizeFormat(value)
	value = mw.ustring.upper(mw.text.trim(tostring(value or '')))
	if value == 'MDY' then
		return 'MDY'
	end
	return 'DMY'
end

local function formatYear(year)
	return tostring(year)
end

local function formatWithoutYear(date, dateFormat)
	local month = monthNames[date.month]
	if dateFormat == 'MDY' then
		return string.format('%s %d', month, date.day)
	end
	return string.format('%d %s', date.day, month)
end

local function formatWithYear(date, dateFormat)
	local withoutYear = formatWithoutYear(date, dateFormat)
	if dateFormat == 'MDY' then
		return string.format('%s, %s', withoutYear, formatYear(date.year))
	end
	return string.format('%s %s', withoutYear, formatYear(date.year))
end

local function formatConversion(source, converted, dateFormat, styleLink)
	if source.year ~= converted.year then
		return string.format('%s &#91;%s %s&#93;', formatWithYear(source, dateFormat), styleLink, formatWithYear(converted, dateFormat))
	end
	if dateFormat == 'MDY' then
		return string.format('%s &#91;%s %s&#93; %s', formatWithoutYear(source, dateFormat), styleLink, formatWithoutYear(converted, dateFormat), formatYear(source.year))
	end
	return string.format('%s &#91;%s %s&#93; %s', formatWithoutYear(source, dateFormat), styleLink, formatWithoutYear(converted, dateFormat), formatYear(source.year))
end

local function isBeforeAnnunciationNewYear(month, day)
	return month < 3 or (month == 3 and day < 25)
end

function p.gregorian_to_julian(frame)
	local args = getArgs(frame)
	local source = sourceDateFromArgs(args)
	local converted = julianFromFixed(frame, fixedFromGregorian(frame, source))
	return formatConversion(source, converted, normalizeFormat(args.format or args[4]), '[[Old Style|O.S.]]')
end

function p.julian_to_gregorian(frame)
	local args = getArgs(frame)
	local source = sourceDateFromArgs(args)
	local converted = gregorianFromFixed(frame, fixedFromJulian(frame, source))
	return formatConversion(source, converted, normalizeFormat(args.format or args[4]), '[[New Style|N.S.]]')
end

function p.new_style_to_annunciation_style(frame)
	local args = getArgs(frame)
	local source = sourceDateFromArgs(args)
	local julian = julianFromFixed(frame, fixedFromGregorian(frame, source))
	local annunciation = { year = julian.year, month = julian.month, day = julian.day }

	if isBeforeAnnunciationNewYear(annunciation.month, annunciation.day) then
		annunciation.year = annunciation.year - 1
	end

	return formatConversion( source, annunciation, normalizeFormat(args.format or args[4]), '[[Old Style|O.S.]]')
end

function p.annunciation_style_to_new_style(frame)
	local args = getArgs(frame)
	local source = sourceDateFromArgs(args)
	local julian = { year = source.year, month = source.month, day = source.day }

	if isBeforeAnnunciationNewYear(julian.month, julian.day) then
		julian.year = julian.year + 1
	end

	local converted = gregorianFromFixed(frame, fixedFromJulian(frame, julian))

	return formatConversion( source, converted, normalizeFormat(args.format or args[4]), '[[New Style|N.S.]]')
end

return p