模块:沙盒/XXTEACUP/Test

维基百科,自由的百科全书
跳转到导航 跳转到搜索
local p = {}
local list_chinese_numerals = {'零','一','二','三','四','五','六','七','八','九'}
local list_chinese_units = {'','十','百','千'}
local list_remainder_ganzhi = {
	'辛酉', -- 1
	'壬戌', -- 2
	'癸亥', -- 3
	'甲子', -- 4
	'乙丑', -- 5
	'丙寅', -- 6
	'丁卯', -- 7
	'戊辰', -- 8
	'己巳', -- 9
	'庚午', -- 10
	'辛未', -- 11
	'壬申', -- 12
	'癸酉', -- 13
	'甲戌', -- 14
	'乙亥', -- 15
	'丙子', -- 16
	'丁丑', -- 17
	'戊寅', -- 18
	'己卯', -- 19
	'庚辰', -- 20
	'辛巳', -- 21
	'壬午', -- 22
	'癸未', -- 23
	'甲申', -- 24
	'乙酉', -- 25
	'丙戌', -- 26
	'丁亥', -- 27
	'戊子', -- 28
	'己丑', -- 29
	'庚寅', -- 30
	'辛卯', -- 31
	'壬辰', -- 32
	'癸巳', -- 33
	'甲午', -- 34
	'乙未', -- 35
	'丙申', -- 36
	'丁酉', -- 37
	'戊戌', -- 38
	'己亥', -- 39
	'庚子', -- 40
	'辛丑', -- 41
	'壬寅', -- 42
	'癸卯', -- 43
	'甲辰', -- 44
	'乙巳', -- 45
	'丙午', -- 46
	'丁未', -- 47
	'戊申', -- 48
	'己酉', -- 49
	'庚戌', -- 50
	'辛亥', -- 51
	'壬子', -- 52
	'癸丑', -- 53
	'甲寅', -- 54
	'乙卯', -- 55
	'丙辰', -- 56
	'丁巳', -- 57
	'戊午', -- 58
	'己未', -- 59
	'庚申'  -- 60
}
local list_order_ganzhi = {
	'甲子', -- 1
	'乙丑', -- 2
	'丙寅', -- 3
	'丁卯', -- 4
	'戊辰', -- 5
	'己巳', -- 6
	'庚午', -- 7
	'辛未', -- 8
	'壬申', -- 9
	'癸酉', -- 10
	'甲戌', -- 11
	'乙亥', -- 12
	'丙子', -- 13
	'丁丑', -- 14
	'戊寅', -- 15
	'己卯', -- 16
	'庚辰', -- 17
	'辛巳', -- 18
	'壬午', -- 19
	'癸未', -- 20
	'甲申', -- 21
	'乙酉', -- 22
	'丙戌', -- 23
	'丁亥', -- 24
	'戊子', -- 25
	'己丑', -- 26
	'庚寅', -- 27
	'辛卯', -- 28
	'壬辰', -- 29
	'癸巳', -- 30
	'甲午', -- 31
	'乙未', -- 32
	'丙申', -- 33
	'丁酉', -- 34
	'戊戌', -- 35
	'己亥', -- 36
	'庚子', -- 37
	'辛丑', -- 38
	'壬寅', -- 39
	'癸卯', -- 40
	'甲辰', -- 41
	'乙巳', -- 42
	'丙午', -- 43
	'丁未', -- 44
	'戊申', -- 45
	'己酉', -- 46
	'庚戌', -- 47
	'辛亥', -- 48
	'壬子', -- 49
	'癸丑', -- 50
	'甲寅', -- 51
	'乙卯', -- 52
	'丙辰', -- 53
	'丁巳', -- 54
	'戊午', -- 55
	'己未', -- 56
	'庚申', -- 57
	'辛酉', -- 58
	'壬戌', -- 59
	'癸亥'  -- 60
}
function p.main(frame)
	local args = frame.args
	local era_name = args['era_name']
	local start_years = tonumber(args['start_years'])
	local start_month = tonumber(args['start_month'])
	local end_years = tonumber(args['end_years'])
	local end_month  = tonumber(args['end_month'])

	local duration_time = end_years-start_years

	if duration_time > 400 then return '<br><span style="color:darkred;font-weight:700">年份历时上限为400年</span>' end
	local text = '{| class="wikitable"  style="text-align:center;"'
	text = text .. '\n![[西元]]!![[干支]]!!紀年!!正月!!二月!!三月!!四月!!五月!!六月!!七月!!八月!!九月!!十月!!十一月!!十二月!!閏月'
	text = text .. '\n|-'
	for index = 0, duration_time do
		local years = start_years+index
		local ganzhi_years = years
		if years == 0 then
			years = years + 1
		end
		if years < 0 then
			ganzhi_years = ganzhi_years + 1
		end
		local bc_years = ''
		if years < 0 then bc_years = '前' end
		local ganzhi = list_remainder_ganzhi[(ganzhi_years-1)%60+1]
		local chinese_numerals_years = numToChinese_num(index)

		text = text .. '\n! rowspan="2" |[[' .. bc_years ..  ''.. math.abs(years) .. '年]]'
		text = text .. '\n! rowspan="2" |[[' .. ganzhi .. ']]'
		text = text .. '\n! rowspan="2" |' .. era_name .. '' .. chinese_numerals_years .. '年'
		text = text .. '\n| || || || || || || || || || || || '
		text = text .. '\n|-'
		text = text .. '\n| || || || || || || || || || || || '
		text = text .. '\n|-'
	end
	text = text .. '\n|}'
	return text
end
function numToChinese_num(n)
	if n == 0 then return '元' end
	local s = tostring(n+1)
	local len = #s
	local result = ''

	for i = 1, len do
		local digit = tonumber(s:sub(i,i))
		local unit = list_chinese_units[len - i +1]
		if digit ~= 0 then
			if digit == 1 and unit == '十' and n < 20 then
				result = result  .. unit
			else
				result = result .. list_chinese_numerals[digit + 1] .. unit
			end
		else
			if not result:match('零$') then
				result = result .. '零'
			end
		end
		result = result:gsub('零+$','')
		result = result:gsub('零零','零')
	end
	return result
end
function p.convertGanZhiDay(frame)
	local args = frame.args
	local year = tonumber(args['year'])
	local month = tonumber(args['month'])
	local day = tonumber(args['day'])
	local hour = tonumber(args['hour'])
	local minute = tonumber(args['minute'])
	local second = tonumber(args['second'])
	
	if not hour then hour = 12 end
	if not minute then minute = 0 end
	if not second then second = 0 end

	a = math.floor((14 - month) / 12)
	y = year + 4800 - a
	m = month + 12 * a - 3

	JDN = 0
	if year <  1582 or (year == 1582 and month < 10) or (year == 1582 and month == 10 and day <  5) then
		JDN=367*year-7*(year+5001+(month-9)/7)/4+(275*month)/9+day+1729777
	else
		JDN=day+math.floor((153*m+2)/5)+365*y+math.floor(y/4)-math.floor(y/100)+math.floor(y/400)-32045
	end
	JD=JDN+(hour-12)/24+minute/1440+second/86400
	gz = (JD+50.5-1)%60+1
	return list_order_ganzhi[math.floor(gz)]  .. ' | ' .. year .. ' / ' .. month.. ' / ' .. day .. ' ' .. hour .. ' : ' .. minute .. ' : ' .. second .. ' | ' .. JD 
end
return p