欢迎来到奇葩栖息地!欢迎加入Discord服务器:XfrfHCzfbW。请先至特殊:参数设置验证邮箱后再进行编辑。在特殊:参数设置挑选自己想要使用的小工具!不会编辑?请至这里学习Wikitext语法。
模块:Chinese calendar
来自奇葩栖息地
local p = {}
local function extract(num, pos, len)
local shifted = math.floor(num / (2 ^ pos))
local result = shifted % (2 ^ len)
return result
end
-- 天干
local HEAVENLY_STEMS = { '甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸' }
-- 地支
local EARTHLY_BRANCHES = { '子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥' }
-- 月份
local LUNAR_MONTHS = { '正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊' }
-- 日期
local LUNAR_DAYS = {
'初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十',
'十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十',
'廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十'
}
-- 农历数据 (1900-2100)
local LUNAR_INFO = {
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
}
-- 更新节气数据(每个节气的日期偏移量)
local SOLAR_TERMS_OFFSET = {
5, 20, 3, 18, 5, 20, 4, 19, 5, 20, 5, 21,
6, 22, 7, 22, 7, 22, 7, 23, 7, 22, 6, 21
}
-- 节气名称
local SOLAR_TERMS = {
'小寒', '大寒', '立春', '雨水', '惊蛰', '春分',
'清明', '谷雨', '立夏', '小满', '芒种', '夏至',
'小暑', '大暑', '立秋', '处暑', '白露', '秋分',
'寒露', '霜降', '立冬', '小雪', '大雪', '冬至'
}
-- 检查日期是否合法
local function isValidDate(year, month, day)
local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
days[2] = 29
end
return day <= days[month]
end
-- 计算指定公历日期是一年中的第几天
local function getDayOfYear(year, month, day)
local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
days[2] = 29
end
local dayOfYear = day
for i = 1, month - 1 do
dayOfYear = dayOfYear + days[i]
end
return dayOfYear
end
-- 获取某年农历数据中的月份大小信息
local function getMonthSizes(lunarCode)
local monthSizes = {}
for i = 1, 12 do
monthSizes[i] = extract(lunarCode, 12 - i, 1) == 1 and 30 or 29
end
return monthSizes
end
-- 获取闰月信息
local function getLeapMonth(lunarCode)
return extract(lunarCode, 12, 4)
end
-- 获取春节日期
local function getSpringFestival(lunarCode)
local month = extract(lunarCode, 16, 2) + 1
local day = extract(lunarCode, 18, 5)
return month, day
end
-- 计算节气的修正值
local function getTermOffset(year, termIndex)
local y = year - 1900
local d = 0
-- 针对不同节气使用不同的系数
local coefficient = 0.2422
if termIndex <= 2 then -- 小寒、大寒
d = math.floor(y * coefficient + 4.81) - math.floor(y/4)
elseif termIndex <= 6 then -- 立春到春分
d = math.floor(y * coefficient + 4.6) - math.floor(y/4)
elseif termIndex <= 12 then -- 清明到夏至
d = math.floor(y * coefficient + 4.8) - math.floor(y/4)
elseif termIndex <= 18 then -- 小暑到秋分
d = math.floor(y * coefficient + 4.87) - math.floor(y/4)
else -- 寒露到冬至
d = math.floor(y * coefficient + 4.83) - math.floor(y/4)
end
return d
end
-- 计算节气
local function getSolarTerm(year, month, day)
local termIndex = (month - 1) * 2 + 1
for i = 0, 1 do
local idx = termIndex + i
local baseDay = SOLAR_TERMS_OFFSET[idx]
local offset = getTermOffset(year, idx)
local termDay = (baseDay + offset) % 31
if termDay == 0 then termDay = 31 end
if day == termDay then
return SOLAR_TERMS[idx]
end
end
return nil
end
-- 计算农历日期
local function calculateLunarDate(yearCode, year, month, day)
-- 获取春节日期
local sfMonth, sfDay = getSpringFestival(yearCode)
-- 计算目标日期和春节的天数差
local targetDayOfYear = getDayOfYear(year, month, day)
local springFestivalDayOfYear = getDayOfYear(year, sfMonth, sfDay)
local dayDiff = targetDayOfYear - springFestivalDayOfYear
-- 如果在春节前,则属于上一个农历年
if dayDiff < 0 then
if year > 1900 then
local prevYearCode = LUNAR_INFO[year - 1900]
local prevMonthSizes = getMonthSizes(prevYearCode)
local totalDays = dayDiff + prevMonthSizes[12]
return 12, totalDays + 1, false, year - 1
else
return 12, 30 + dayDiff, false, year - 1
end
end
-- 获取当年农历月份大小
local monthSizes = getMonthSizes(yearCode)
local leapMonth = getLeapMonth(yearCode)
-- 计算农历月份和日期
local days = dayDiff
local lunarMonth = 1
local isLeapMonth = false
while days >= monthSizes[lunarMonth] do
days = days - monthSizes[lunarMonth]
if lunarMonth == leapMonth then
if not isLeapMonth then
isLeapMonth = true
if days >= monthSizes[lunarMonth] then
days = days - monthSizes[lunarMonth]
lunarMonth = lunarMonth + 1
end
else
lunarMonth = lunarMonth + 1
isLeapMonth = false
end
else
lunarMonth = lunarMonth + 1
end
if lunarMonth > 12 then
return 1, days + 1, false, year + 1
end
end
return lunarMonth, days + 1, isLeapMonth, year
end
-- 将公历日期转换为农历日期
function p.toLunar(frame)
local date = frame.args[1]
local showSolarTerm = frame.args.showSolarTerm
if type(showSolarTerm) == "string" then
showSolarTerm = (showSolarTerm == "true" or showSolarTerm == "1")
end
-- 输入格式验证
if not date:match('^%d%d%d%d%-%d%d%-%d%d$') then
return '错误:日期格式无效,请使用YYYY-MM-DD格式'
end
local year = tonumber(date:sub(1, 4))
local month = tonumber(date:sub(6, 7))
local day = tonumber(date:sub(9, 10))
-- 输入日期范围验证
if year < 1900 or year > 2100 then
return '错误:年份超出范围(1900-2100)'
end
if month < 1 or month > 12 then
return '错误:月份无效'
end
if not isValidDate(year, month, day) then
return '错误:日期无效'
end
-- 输出日期
local yearCode = LUNAR_INFO[year - 1900 + 1]
local lunarMonth, lunarDay, isLeapMonth, lunarYear = calculateLunarDate(yearCode, year, month, day)
local stemIndex = (lunarYear - 4) % 10
local branchIndex = (lunarYear - 4) % 12
local result = HEAVENLY_STEMS[stemIndex + 1] .. EARTHLY_BRANCHES[branchIndex + 1] .. '年'
if isLeapMonth then
result = result .. '闰'
end
result = result .. LUNAR_MONTHS[lunarMonth] .. '月' .. LUNAR_DAYS[lunarDay]
-- 输出节气
if showSolarTerm then
local solarTerm = getSolarTerm(year, month, day)
if solarTerm then
result = result .. '(' .. solarTerm .. ')'
end
end
return result
end
return p