From db025ff34efd5633a01479ff5a26ff78e7a6e635 Mon Sep 17 00:00:00 2001 From: space2lim Date: Tue, 22 Apr 2025 23:58:13 +0900 Subject: [PATCH] edit cronJob algorithm (summertime) --- config/cronJobs.js | 25 +++++++++++++------------ feature/utility.js | 6 +++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/config/cronJobs.js b/config/cronJobs.js index 836931e..a487e1a 100644 --- a/config/cronJobs.js +++ b/config/cronJobs.js @@ -5,24 +5,25 @@ export function initializeCronJobs(client) { const channel = client.channels.cache.get(process.env.CH_FINANCE); // 미국 주식 시장 개장 시간 (월~금) - const openTime = isDaylightSavingTime() ? '30 22 * * 1-5' : '30 23 * * 1-5'; - const openJob = new CronJob(openTime, async () => { - if (channel) { - const isDST = isDaylightSavingTime(); - const dstMessage = isDST - ? '(서머타임 적용)' - : '(서머타임 미적용)'; + const openJob = new CronJob('30 22-23 * * 1-5', async (now) => { + const isDST = isDaylightSavingTime(); + const expectedHour = isDST ? 22 : 23; + + if (now.getHours() === expectedHour) { + const dstMessage = isDST ? '(서머타임 적용)' : '(서머타임 미적용)'; await channel.send(`미국 주식 시장이 열렸습니다. ${dstMessage}`); } }, null, true, 'Asia/Seoul'); - - // 미국 주식 시장 폐장 시간 (월~금) → 한국 시간 화~토 새벽에 실행 - const closeTime = isDaylightSavingTime() ? '0 5 * * 2-6' : '0 6 * * 2-6'; - const closeJob = new CronJob(closeTime, async () => { - if (channel) { + + const closeJob = new CronJob('0 5-6 * * 2-6', async (now) => { + const isDST = isDaylightSavingTime(); + const expectedHour = isDST ? 5 : 6; + + if (now.getHours() === expectedHour) { await channel.send('미국 주식 시장이 닫혔습니다.'); } }, null, true, 'Asia/Seoul'); + // 작업 시작 openJob.start(); diff --git a/feature/utility.js b/feature/utility.js index 5347f9f..9f01ff8 100644 --- a/feature/utility.js +++ b/feature/utility.js @@ -26,6 +26,6 @@ export function isDaylightSavingTime() { const now = new Date(); const jan = new Date(now.getFullYear(), 0, 1); const jul = new Date(now.getFullYear(), 6, 1); - const standardOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); - return now.getTimezoneOffset() < standardOffset; // 서머타임 여부 반환 -} \ No newline at end of file + const stdTimezoneOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); + return now.getTimezoneOffset() < stdTimezoneOffset; +}