From dfc81bb70effde6a0ba2d3511127f31a238af9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eason=20=28=E9=99=88=E5=8C=BB=E7=94=9F=29?= Date: Tue, 24 Feb 2026 15:25:17 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20lunar-javascript?= =?UTF-8?q?=20=E5=BA=93=E8=AE=A1=E7=AE=97=E5=86=9C=E5=8E=86=20-=20?= =?UTF-8?q?=E4=B8=93=E4=B8=9A=E5=87=86=E7=A1=AE=E7=9A=84=E5=86=9C=E5=8E=86?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/system-date/date.js | 62 +++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/skills/system-date/date.js b/skills/system-date/date.js index 2b0f139..d707f09 100644 --- a/skills/system-date/date.js +++ b/skills/system-date/date.js @@ -1,32 +1,34 @@ /** * System Date Skill - * 获取当前日期和时间(北京时间 UTC+8) + * 获取当前日期和农历(使用 lunar-javascript 库) */ +const { Solar, Lunar } = require('lunar-javascript'); + /** * 获取北京时间 */ function getBeijingTime() { const now = new Date(); - // UTC 时间 + 8 小时 = 北京时间 - const beijingTime = new Date(now.getTime() + (8 * 60 * 60 * 1000)); - return beijingTime; + // UTC + 8 = 北京时间 + return new Date(now.getTime() + (8 * 60 * 60 * 1000)); } /** - * 计算农历日期(2026 年春节 2 月 17 日 = 正月初一) + * 获取农历日期(使用专业库) */ -function getLunarDate(beijingDate) { - const springFestival = new Date('2026-02-17T00:00:00'); // 春节北京时间 - const diffMs = beijingDate.getTime() - springFestival.getTime(); - const daysSince = Math.floor(diffMs / (1000 * 60 * 60 * 24)); - const lunarDay = daysSince + 1; // 正月初一是第 1 天 +function getLunarInfo(beijingDate) { + // 从公历转换为农历 + const solar = Solar.fromDate(beijingDate); + const lunar = solar.getLunar(); - if (lunarDay >= 1 && lunarDay <= 30) { - return `农历正月初${lunarDay}`; - } else { - return `农历${lunarDay > 30 ? '二月' : '腊月'}${Math.abs(lunarDay - 30)}`; - } + return { + lunarDate: lunar.toString(), // 如:二〇二六年正月初八 + lunarDay: lunar.getDayInChinese(), // 如:初八 + lunarMonth: lunar.getMonthInChinese(), // 如:正月 + lunarYear: lunar.getYearInChinese(), // 如:二〇二六 + isLeap: false // lunar-javascript 需要其他方法判断闰月 + }; } /** @@ -41,6 +43,8 @@ function getCurrentDateTime() { const weekdays = ['日', '一', '二', '三', '四', '五', '六']; const weekday = `星期${weekdays[beijingNow.getDay()]}`; + const lunarInfo = getLunarInfo(beijingNow); + return { success: true, timezone: 'Asia/Shanghai', @@ -49,7 +53,11 @@ function getCurrentDateTime() { day: day, weekday: weekday, fullDate: `${year}年${month}月${day}日`, - lunarDate: getLunarDate(beijingNow), + lunarDate: lunarInfo.lunarDate, + lunarDay: lunarInfo.lunarDay, + lunarMonth: lunarInfo.lunarMonth, + lunarYear: lunarInfo.lunarYear, + isLeap: lunarInfo.isLeap, isoString: beijingNow.toISOString() }; } @@ -81,6 +89,8 @@ function getRelativeDate(relative = 'today') { const weekdays = ['日', '一', '二', '三', '四', '五', '六']; const weekday = `星期${weekdays[targetDate.getDay()]}`; + const lunarInfo = getLunarInfo(targetDate); + return { success: true, relative: relative, @@ -89,7 +99,11 @@ function getRelativeDate(relative = 'today') { day: day, weekday: weekday, fullDate: `${year}年${month}月${day}日`, - lunarDate: getLunarDate(targetDate), + lunarDate: lunarInfo.lunarDate, + lunarDay: lunarInfo.lunarDay, + lunarMonth: lunarInfo.lunarMonth, + lunarYear: lunarInfo.lunarYear, + isLeap: lunarInfo.isLeap, timezone: 'Asia/Shanghai' }; } @@ -105,6 +119,9 @@ function formatDateInfo(dateInfo, includeLunar = true) { if (includeLunar && dateInfo.lunarDate) { lines.push(`**农历:** ${dateInfo.lunarDate}`); + if (dateInfo.isLeap) { + lines.push(`**闰月:** 是`); + } } lines.push(`**时区:** ${dateInfo.timezone || 'Asia/Shanghai'}`); @@ -114,13 +131,16 @@ function formatDateInfo(dateInfo, includeLunar = true) { // 命令行测试 if (require.main === module) { - console.log('=== 北京时间测试 ==='); - console.log('今天:', new Date(new Date().getTime() + 8*60*60*1000).toISOString()); - console.log(''); + console.log('=== 农历计算测试(使用 lunar-javascript 库)===\n'); const arg = process.argv[2] || 'today'; const result = getRelativeDate(arg); console.log(formatDateInfo(result)); + console.log('\n详细信息:'); + console.log(`农历年:${result.lunarYear}`); + console.log(`农历月:${result.lunarMonth}`); + console.log(`农历日:${result.lunarDay}`); + console.log(`是否闰月:${result.isLeap ? '是' : '否'}`); } -module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo, getLunarDate, getBeijingTime }; +module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo, getLunarInfo, getBeijingTime };