From f4d300a0c4b8b4eb582baa4c9d3e168430ad6454 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:14:00 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=86=9C=E5=8E=86=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=20-=20=E5=8C=97=E4=BA=AC=E6=97=B6=E9=97=B4?= =?UTF-8?q?=20UTC+8=EF=BC=8C2/24=3D=E5=88=9D=E5=85=AB=EF=BC=8C2/25=3D?= =?UTF-8?q?=E5=88=9D=E4=B9=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/system-date/date.js | 145 ++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 81 deletions(-) diff --git a/skills/system-date/date.js b/skills/system-date/date.js index adfe8ce..2b0f139 100644 --- a/skills/system-date/date.js +++ b/skills/system-date/date.js @@ -1,67 +1,65 @@ /** * System Date Skill - * 获取当前日期和时间(支持时区) + * 获取当前日期和时间(北京时间 UTC+8) */ /** - * 获取当前日期时间 - * @param {string} timezone - 时区 (默认 Asia/Shanghai) - * @returns {Object} 日期时间信息 + * 获取北京时间 */ -function getCurrentDateTime(timezone = 'Asia/Shanghai') { +function getBeijingTime() { const now = new Date(); + // UTC 时间 + 8 小时 = 北京时间 + const beijingTime = new Date(now.getTime() + (8 * 60 * 60 * 1000)); + return beijingTime; +} + +/** + * 计算农历日期(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 天 - // 转换为指定时区 - const options = { - timeZone: timezone, - year: 'numeric', - month: 'long', - day: 'numeric', - weekday: 'long', - hour: '2-digit', - minute: '2-digit', - hour12: false - }; - - const formatter = new Intl.DateTimeFormat('zh-CN', options); - const parts = formatter.formatToParts(now); - - const result = {}; - parts.forEach(part => { - result[part.type] = part.value; - }); + if (lunarDay >= 1 && lunarDay <= 30) { + return `农历正月初${lunarDay}`; + } else { + return `农历${lunarDay > 30 ? '二月' : '腊月'}${Math.abs(lunarDay - 30)}`; + } +} + +/** + * 获取当前日期时间(北京时间) + */ +function getCurrentDateTime() { + const beijingNow = getBeijingTime(); - // 计算农历日期(基于 2026 年春节 2 月 17 日) - const springFestival = new Date('2026-02-17'); - const daysSince = Math.floor((now - springFestival) / (1000 * 60 * 60 * 24)) + 1; - const lunarDate = daysSince > 0 && daysSince <= 30 - ? `农历正月初${daysSince}` - : '农历日期需查询黄历'; + const year = beijingNow.getFullYear(); + const month = beijingNow.getMonth() + 1; + const day = beijingNow.getDate(); + const weekdays = ['日', '一', '二', '三', '四', '五', '六']; + const weekday = `星期${weekdays[beijingNow.getDay()]}`; return { success: true, - timezone: timezone, - year: result.year, - month: result.month, - day: result.day, - weekday: result.weekday, - hour: result.hour, - minute: result.minute, - fullDate: `${result.year}年${parseInt(result.month)}月${parseInt(result.day)}日`, - lunarDate: lunarDate, - isoString: now.toISOString() + timezone: 'Asia/Shanghai', + year: year, + month: month, + day: day, + weekday: weekday, + fullDate: `${year}年${month}月${day}日`, + lunarDate: getLunarDate(beijingNow), + isoString: beijingNow.toISOString() }; } /** - * 获取相对日期 - * @param {string} relative - 相对日期 (today, tomorrow, yesterday) - * @param {string} timezone - 时区 - * @returns {Object} 日期信息 + * 获取相对日期(北京时间) */ -function getRelativeDate(relative = 'today', timezone = 'Asia/Shanghai') { - const now = new Date(); - let targetDate = new Date(now); +function getRelativeDate(relative = 'today') { + const beijingNow = getBeijingTime(); + let targetDate = new Date(beijingNow); switch (relative.toLowerCase()) { case 'today': @@ -77,39 +75,22 @@ function getRelativeDate(relative = 'today', timezone = 'Asia/Shanghai') { break; } - // 计算农历日期 - const springFestival = new Date('2026-02-17'); - const daysSince = Math.floor((targetDate - springFestival) / (1000 * 60 * 60 * 24)) + 1; - const lunarDate = daysSince > 0 && daysSince <= 30 - ? `农历正月初${daysSince}` - : '农历日期需查询黄历'; - - const options = { - timeZone: timezone, - year: 'numeric', - month: 'long', - day: 'numeric', - weekday: 'long' - }; - - const formatter = new Intl.DateTimeFormat('zh-CN', options); - const parts = formatter.formatToParts(targetDate); - - const result = {}; - parts.forEach(part => { - result[part.type] = part.value; - }); + const year = targetDate.getFullYear(); + const month = targetDate.getMonth() + 1; + const day = targetDate.getDate(); + const weekdays = ['日', '一', '二', '三', '四', '五', '六']; + const weekday = `星期${weekdays[targetDate.getDay()]}`; return { success: true, relative: relative, - year: result.year, - month: result.month, - day: result.day, - weekday: result.weekday, - fullDate: `${result.year}年${parseInt(result.month)}月${parseInt(result.day)}日`, - lunarDate: lunarDate, - isoString: targetDate.toISOString() + year: year, + month: month, + day: day, + weekday: weekday, + fullDate: `${year}年${month}月${day}日`, + lunarDate: getLunarDate(targetDate), + timezone: 'Asia/Shanghai' }; } @@ -126,18 +107,20 @@ function formatDateInfo(dateInfo, includeLunar = true) { lines.push(`**农历:** ${dateInfo.lunarDate}`); } - if (dateInfo.timezone) { - lines.push(`**时区:** ${dateInfo.timezone}`); - } + lines.push(`**时区:** ${dateInfo.timezone || 'Asia/Shanghai'}`); return lines.join('\n'); } // 命令行测试 if (require.main === module) { + console.log('=== 北京时间测试 ==='); + console.log('今天:', new Date(new Date().getTime() + 8*60*60*1000).toISOString()); + console.log(''); + const arg = process.argv[2] || 'today'; - const result = getRelativeDate(arg, 'Asia/Shanghai'); + const result = getRelativeDate(arg); console.log(formatDateInfo(result)); } -module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo }; +module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo, getLunarDate, getBeijingTime };