มอดูล:Year in other calendars

-- Load dependencies.
local getArgs = require('Module:Arguments').getArgs
local numToRoman = require( 'Module:Roman' ).main
local getOlympiad = require( 'Module:Ancient Olympiads' )._main
local getDynasty = require( 'Module:Ancient Egypt era' )._main
local getPharaoh = require( 'Module:Ancient Egypt kings' )._main
local numToArmenian = require( 'Module:Armenian' ).main
local getRegnal = require( 'Module:British regnal year' ).main
local japaneseEra = require( 'Module:Japanese calendar' ).era()
-- Define constants.
local lang = mw.language.getContentLanguage()
local currentYear = tonumber( lang:formatDate( 'Y' ) )
--------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------
local function isInteger( num )
	-- Checks if a value is an integer. If so, returns the value converted to a number.
	-- If not, returns false.
	num = tonumber( num )
	if num and math.floor( num ) == num and num ~= math.huge then
		return num
	else
		return false
	end
end
local function BCToNum( s )
	-- Converts strings of the format "n BC" to their corresponding
	-- numerical values.
	if type( s ) ~= 'string' then
		return nil
	end
	s = mw.ustring.match( mw.ustring.upper( s ), '^(1-9%d*)%s*BC$' )
	if not s then
		return nil
	end
	local num = tonumber( s )
	num = ( num - 1 ) * -1
	return num
end
local function numToBC( num )
	-- For BC years, returns a string with the year name appended with " BC".
	-- Otherwise returns nil.
	num = isInteger( num )
	if not num then return end
	if num = 1941 then
		thai:setYear( year + 543 )
	else -- if year >= 1912 or year %s,
				gregorian.yearLink, gregorian.romanYear
		) )
	else
		gregorian:setYear( gregorian.yearLink )
	end
	box:addCalendar( gregorian ) 
	
	----------------------------------------------------------------------
	-- French Republican calendar
	-- แสดงเฉพาะใน ค.ศ. 1793 - 1805 และ 1871
	-- This calendar was in use and had defined years only for the short period on display.
	-- Its importance during these few years is also the reason why it should stay out of the alphabetic order.
	-- See discussion on talk page.
	----------------------------------------------------------------------
	
	if year >= 1793 and year = -752 then
		local abUrbe = calendar:new()
		abUrbe:setLink( 'Ab urbe condita' )
		abUrbe:setYear( year + 753 )
		box:addCalendar( abUrbe )
	end
	
	----------------------------------------------------------------------
	-- Ancient Egypt era 
	-- Displays dynasty between 1549 BC and 30 BC
	-- Displays pharaoh or king between 752 BC and 30 BC
	----------------------------------------------------------------------
	if year > -1549 and year  - 752 and year - ฟาโรห์'
		)
	ancPharaoh:setYear( getPharaoh( year ) )
	box:addCalendar( ancPharaoh )
	end
	----------------------------------------------------------------------
	-- Ancient Olympiads 
	-- Currently only the first 194 Olympiads
	-- May be expanded until 394 AD when data available
	----------------------------------------------------------------------
	if year >= -1300 and year  551 then
	local armenian = calendar:new()
armenian:setLink( 'ปฏิทินอาร์มีเนีย' )
		local armenianYear = year - 551
		armenian:setYear( string.format( '%sԹՎ %s', armenianYear, numToArmenian( armenianYear ) ) )
	box:addCalendar( armenian )
	end
	
----------------------------------------------------------------------
-- Assyrian calendar
----------------------------------------------------------------------
local assyrian = calendar:new()
assyrian:setLink( 'ปฏิทินอัสซีเรีย' )
assyrian:setYear( year + 4750 )
box:addCalendar( assyrian )

----------------------------------------------------------------------
-- Bahá'í calendar
----------------------------------------------------------------------

	if year >= 1844 then
		local bahai = calendar:new()
		bahai:setLink( "ปฏิทินบาไฮ" )
		bahai:setYearRange( year - 1844, year - 1843 )
		box:addCalendar( bahai )
	end
	
----------------------------------------------------------------------
-- Balinese saka calendar
---------------------------------------------------------------------- 
local balinese = calendar:new()
balinese:setLink( 'ปฏิทินศกบาหลี' )
if year - 76 > 0 then
	balinese:setYearRange( year - 79, year - 78 )
end
box:addCalendar( balinese )
	----------------------------------------------------------------------
-- Bengali calendar
----------------------------------------------------------------------
local bengali = calendar:new()
bengali:setLink( 'ปฏิทินเบงกอล' )
	bengali:setYearRange( year - 594, year - 593 )
	box:addCalendar( bengali )
	----------------------------------------------------------------------
-- Berber calendar
----------------------------------------------------------------------

local berber = calendar:new()
berber:setLink( 'ปฏิทินเบอร์เบอร์' )
berber:setYear( year + 950 )
box:addCalendar( berber )
----------------------------------------------------------------------
-- Regnal year
----------------------------------------------------------------------
	if year >= 1000 then
		local regnal = calendar:new()
		local regnalName
		if year > 1706 then
regnalName = 'อังกฤษ'
else
regnalName = 'อังกฤษ'
end
regnal:setLink( 'ปีในรัชกาลของพระมหากษัตริย์อังกฤษ', 'ปีในรัชกาล' .. regnalName )
		regnal:setYear( getRegnal( year ) )
		box:addCalendar( regnal )
	end
----------------------------------------------------------------------
-- Buddhist calendar
----------------------------------------------------------------------
local buddhist = calendar:new()
buddhist:setLink( 'พุทธศักราช' )
buddhist:setYear( year + 544 )
box:addCalendar( buddhist )
----------------------------------------------------------------------
-- Burmese calendar
----------------------------------------------------------------------
local burmese = calendar:new()
burmese:setLink( 'ปฏิทินพม่าดั้งเดิม', 'ปฏิทินพม่า' )
burmese:setYear( year - 638 )
box:addCalendar( burmese )
----------------------------------------------------------------------
-- Byzantine calendar
----------------------------------------------------------------------
local byzantine = calendar:new()
byzantine:setLink( 'ปฏิทินไบแซนไทน์' )
byzantine:setYearRange( year + 5508, year + 5509 )
box:addCalendar( byzantine )
----------------------------------------------------------------------
-- Chinese calendar
----------------------------------------------------------------------

local chinese = calendar:new()
chinese:setLink( 'ปฏิทินจีน' )
-- Define the information for the "heavenly stems" and "earthly branches" year cycles.
-- See Chinese calendar#Cycle of years for information.
local earthlyBranches = {
{ '子', 'ชวด' },                        -- 1
{ '丑', 'ฉลู' },                         -- 2
{ '寅', 'ขาล' },                        -- 3
{ '卯', 'เถาะ' },                        -- 4
{ '辰', 'มะโรง' },                        -- 5
{ '巳', 'มะเส็ง' },                        -- 6
{ '午', 'มะเมีย' },                        -- 7
{ '未', 'มะแม' },                        -- 8
{ '申', 'วอก' },                         -- 9
{ '酉', 'ระกา' },                         -- 10
{ '戌', 'จอ' },                          -- 11
{ '亥', 'กุน' }                           -- 12
}
local heavenlyStems = {
{ '甲', 'ธาตุไม้' },   -- 1
{ '乙', 'ธาตุไม้' },   -- 2
{ '丙', 'ธาตุไฟ' },   -- 3
{ '丁', 'ธาตุไฟ' },   -- 4
{ '戊', 'ธาตุดิน' },  -- 5
{ '己', 'ธาตุดิน' },  -- 6
{ '庚', 'ธาตุโลหะ' },  -- 7
{ '辛', 'ธาตุโลหะ' },  -- 8
{ '壬', 'ธาตุน้ำ' },  -- 9
{ '癸', 'ธาตุน้ำ' }   -- 10
}
	-- Calculate the cycle numbers from the year. The first sexagenary year corresponds to the previous year's entry
	-- in Chinese calendar correspondence table, as the Chinese New Year doesn't happen until Jan/Feb in
	-- Gregorian years.
	local sexagenaryYear1 = ( year - 4 ) % 60
	local sexagenaryYear2 = ( year - 3 ) % 60
	local heavenlyNum1 = (sexagenaryYear1 - 1) % 10 + 1 -- amod, since lua arrays are 1-indexed
	local heavenlyNum2 = (sexagenaryYear2 - 1) % 10 + 1
	local earthlyNum1 = (sexagenaryYear1 - 1) % 12 + 1
	local earthlyNum2 = (sexagenaryYear2 - 1) % 12 + 1
	-- Get the data tables for each permutation.
	local heavenlyTable1 = heavenlyStems heavenlyNum1 
	local heavenlyTable2 = heavenlyStems heavenlyNum2 
	local earthlyTable1 = earthlyBranches earthlyNum1 
	local earthlyTable2 = earthlyBranches earthlyNum2 
	-- Work out the continously-numbered year. (See Chinese calendar#Continuously numbered years.)
	local year1 = year + 2697
	local year2 = year + 2698
	local year1Alt = year1 - 207
	local year2Alt = year2 - 207
	-- Format any negative numbers.
	year1 = formatNegative( tostring( year1 ) )
	year2 = formatNegative( tostring( year2 ) )
	year1Alt = formatNegative( tostring( year1Alt ) )
	year2Alt = formatNegative( tostring( year2Alt ) )
-- Return all of that data in a (hopefully) reader-friendly format.
chinese:setYear( mw.ustring.format(
**%s%s年 (%s%s)%s หรือ %s    — ถึง —%s%s年 (%s%s)%s หรือ %s=,
heavenlyTable1 1 ,
earthlyTable1 1 ,
earthlyTable1 2 ,
heavenlyTable1 2 ,
year1,
year1Alt,
heavenlyTable2 1 ,
earthlyTable2 1 ,
earthlyTable2 2 ,
heavenlyTable2 2 ,
year2,
year2Alt
) )
box:addCalendar( chinese )

----------------------------------------------------------------------
-- Coptic calendar
----------------------------------------------------------------------
local coptic = calendar:new()
coptic:setLink( 'ปฏิทินคอปติก' )
coptic:setYearRange( year - 284, year - 283 )
box:addCalendar( coptic )

----------------------------------------------------------------------
-- Discordian calendar
----------------------------------------------------------------------

local discordian = calendar:new()
discordian:setLink( 'ปฏิทินดิสคอร์เดีย' )
discordian:setYear( year + 1166 )
box:addCalendar( discordian )
----------------------------------------------------------------------
-- Ethiopian calendar
----------------------------------------------------------------------
local ethiopian = calendar:new()
ethiopian:setLink( 'ปฏิทินเอธิโอเปีย' )
ethiopian:setYearRange( year - 8, year - 7 )
box:addCalendar( ethiopian )
----------------------------------------------------------------------
-- Hebrew calendar
----------------------------------------------------------------------
local hebrew = calendar:new()
hebrew:setLink( 'ปฏิทินฮีบรู' )
hebrew:setYearRange( year + 3760, year + 3761 )
box:addCalendar( hebrew )
----------------------------------------------------------------------
-- Hindu calendars
----------------------------------------------------------------------
local hindu = calendarGroup:new{ heading = 'ปฏิทินฮินดู' }
-- Vikram Samvat
local vikramSamvat = calendar:new()
vikramSamvat:setLink( 'วิกรมสมวัต' )
	vikramSamvat:setYearRange( year + 56, year + 57 )
	hindu:addCalendar( vikramSamvat )
	-- Shaka Samvat
local shakaSamvat = calendar:new()
shakaSamvat:setLink( 'ปฏิทินประจำชาติอินเดีย', 'ศกสมวัต' )
	if year >= 78 then
		shakaSamvat:setYearRange( year - 79, year - 78 )
	end
	hindu:addCalendar( shakaSamvat )
	-- Kali Yuga

local kaliYuga = calendar:new()
kaliYuga:setLink( 'กลียุค' ) -- use italics
kaliYuga:setYearRange( year + 3101, year + 3102 )
hindu:addCalendar( kaliYuga )
box:addCalendarGroup( hindu )
----------------------------------------------------------------------
-- Holocene calendar
----------------------------------------------------------------------
local holocene = calendar:new()
holocene:setLink( 'ปฏิทินโฮโลซีน' )
holocene:setYear( year + 10000 )
box:addCalendar( holocene )
----------------------------------------------------------------------
	-- Igbo calendar
	----------------------------------------------------------------------
	-- In the old template this was a calendar group with just one calendar; intentionally adding this as a single
	-- calendar here, as the previous behaviour looked like a mistake.
	if year >= 1000 then
		local igbo = calendar:new()
igbo:setLink( 'ปฏิทินอิกโบ' )
igbo:setYearRange( year - 1000, year - 999 )
		box:addCalendar( igbo )
	end
----------------------------------------------------------------------
-- Iranian calendar
----------------------------------------------------------------------
local iranian = calendar:new()
iranian:setLink( 'ปฏิทินอิหร่าน' )
	if year - 621 > 0 then
		iranian:setYearRange( year - 622, year - 621 )
	else
		iranian:setYear( string.format( '%d BP – %d BP', 622 - year, 621 - year ) )
	end
	box:addCalendar( iranian )
----------------------------------------------------------------------
-- Islamic calendar
----------------------------------------------------------------------
local islamic = calendar:new()
islamic:setLink( 'ปฏิทินอิสลาม' )
	local islamicMult = 1.030684 -- the factor to multiply by
	local islamicSub = 621.5643 -- the factor to subtract by
	if year - 621 > 0 then
		local year1 = math.floor( islamicMult * ( year - islamicSub ) )
		local year2 = math.floor( islamicMult * ( year - islamicSub + 1 ) )
		islamic:setYearRange( year1, year2 )
	else
		local year1 = math.ceil( -islamicMult * ( year - islamicSub ) )
		local year2 = math.ceil( -islamicMult * ( year - islamicSub + 1 ) )
		islamic:setYear( string.format( '%d BH – %d BH', year1, year2 ) )
	end
	box:addCalendar( islamic )
----------------------------------------------------------------------
-- Japanese calendar
----------------------------------------------------------------------
	if year >= 600 then
	local japanese = calendar:new()
japanese:setLink( 'ปฏิทินญี่ปุ่น' )
	japanese.thisEra = japaneseEra:new{ year = year }
	if japanese.thisEra then
		local japaneseYearText = {}
		japanese.oldEra = japanese.thisEra:getOldEra()
		if japanese.oldEra and japanese.oldEra.eraYear and japanese.thisEra.article ~= japanese.oldEra.article then
			japanese.oldText = string.format( '%s %d', japanese.oldEra.link, japanese.oldEra.eraYear )
			table.insert( japaneseYearText, japanese.oldText )
			table.insert( japaneseYearText, ' / ' )
		end
		if japanese.thisEra.eraYear then
			table.insert( japaneseYearText, string.format( '%s %d', japanese.thisEra.link, japanese.thisEra.eraYear ) )
		end
		table.insert( japaneseYearText, string.format( '(%s%s年)', japanese.thisEra.kanji, japanese.thisEra.eraYearKanji ) )
		japanese:setYear( table.concat( japaneseYearText ) )
	end
	box:addCalendar( japanese )
	end
	----------------------------------------------------------------------
	-- Javanese calendar
	----------------------------------------------------------------------
	local javanese = calendar:new()
	javanese:setLink( 'ปฏิทินชวา' )
	local javaneseMult = 1.030684 -- the factor to multiply by
	local javaneseSub = 124.9 -- the factor to subtract by
	if year - 124 > 0 then
		local year1 = math.floor( javaneseMult * ( year - javaneseSub ) )
		local year2 = math.floor( javaneseMult * ( year - javaneseSub + 1 ) )
		javanese:setYearRange( year1, year2 )
	else
		local year1 = math.ceil( -javaneseMult * ( year - javaneseSub ) )
		local year2 = math.ceil( -javaneseMult * ( year - javaneseSub + 1 ) )
	end
	box:addCalendar( javanese )
	
	----------------------------------------------------------------------
	-- Juche calendar
	-- แสดงหลัง ค.ศ. 1910
----------------------------------------------------------------------
	if year >= 1910 then
		local juche = calendar:new()
juche:setLink( 'ปฏิทินเกาหลีเหนือ', 'ปฏิทินจูเช' )
		if year > 1911 then
			juche:setYear( year - 1911 )
		end
		box:addCalendar( juche )
	end

----------------------------------------------------------------------
-- Julian calendar
----------------------------------------------------------------------
local julian = calendar:new()
julian:setLink( 'ปฏิทินจูเลียส' )
	if year >= -45 and year = 1582 then
		local diff = math.floor(year/100-2) - math.floor(year/400)
		if year % 100 == 0 and year % 400 ~= 0 then
			julian:setYear('เกรกอรีลบ ' .. diff-1 .. ' หรือ ' .. diff .. ' วัน')
		else
			julian:setYear('เกรกอรีลบ ' .. diff .. ' วัน')
		end
	end
	box:addCalendar( julian )

----------------------------------------------------------------------
-- Korean calendar
----------------------------------------------------------------------
local korean = calendar:new()
korean:setLink( 'ปฏิทินเกาหลี' )
korean:setYear( year + 2333 )
box:addCalendar( korean )

----------------------------------------------------------------------
-- Minguo calendar
----------------------------------------------------------------------
	local minguo = calendar:new()
minguo:setLink( 'ปฏิทินหมินกั๋ว' )
	if year > 1949 then
		local minguoYear = year - 1911
		minguo:setYear( string.format( 'ROC %d民國%d年', minguoYear, minguoYear ) )
	elseif year > 1911 then
		local minguoYear = year - 1911
		minguo:setYear( string.format( 'ROC %d民國%d年', minguoYear, minguoYear ) )
	else
		local minguoYear = 1911 - year + 1
		minguo:setYear( string.format( '%d ก่อน ROC民前%d年', minguoYear, minguoYear ) )
	end
	box:addCalendar( minguo )

	----------------------------------------------------------------------
	-- Nanakshahi calendar
	----------------------------------------------------------------------
	local nanakshahi = calendar:new()
	nanakshahi:setLink( 'ปฏิทินนานักชาฮี' )
	nanakshahi:setYear( year - 1468 )
	box:addCalendar( nanakshahi )
	
	----------------------------------------------------------------------
	-- Seleucid era
	-- displays from 312 BC until 1200 AD
	----------------------------------------------------------------------
	
	if year >= -311 and year (%s-%s)%s หรือ %s หรือ %s    — ถึง —%s%s年(%s-%s)%s หรือ %s หรือ %s=,
			heavenlyTable1 1 ,
			earthlyTable1 1 ,
			heavenlyTable1 2 ,
			earthlyTable1 2 ,
			year1,
			year1Alt1,
			year1Alt2,
			heavenlyTable2 1 ,
			earthlyTable2 1 ,
			heavenlyTable2 2 ,
			earthlyTable2 2 ,
			year2,
			year2Alt1,
			year2Alt2
			) )
	box:addCalendar( tibetan )
	----------------------------------------------------------------------
	-- Unix time
	----------------------------------------------------------------------
	local unix = calendar:new()
	local function getUnixTime( year )
		if year = 0 then
			return unixTime - 1
		end
	end
	unix.thisYear = getUnixTime( year )
	unix.nextYear = getUnixTime( year + 1 )
	if unix.thisYear and unix.nextYear then
		unix:setLink( 'เวลายูนิกซ์' )
		unix:setYear( (unix.thisYear + 1) .. " – " .. unix.nextYear )
	end
	box:addCalendar( unix )
	return box:export()
end
--------------------------------------------------------------------
-- Process arguments from #invoke
--------------------------------------------------------------------
local p = {}
function p.main( frame )
	-- Process the arguments and pass them to the box-building function.
	local args = getArgs( frame )
	-- Pass year argument with 'year' parameter or without any name but first argument
	args.year = args.year or args1
	return makeCalendarBox( args )
end
return p