edit cronJob algorithm (summertime)

This commit is contained in:
SeungJu Lim 2025-04-22 23:58:13 +09:00
parent 1ff7e96e50
commit db025ff34e
2 changed files with 16 additions and 15 deletions

View File

@ -5,25 +5,26 @@ export function initializeCronJobs(client) {
const channel = client.channels.cache.get(process.env.CH_FINANCE); 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('30 22-23 * * 1-5', async (now) => {
const openJob = new CronJob(openTime, async () => {
if (channel) {
const isDST = isDaylightSavingTime(); const isDST = isDaylightSavingTime();
const dstMessage = isDST const expectedHour = isDST ? 22 : 23;
? '(서머타임 적용)'
: '(서머타임 미적용)'; if (now.getHours() === expectedHour) {
const dstMessage = isDST ? '(서머타임 적용)' : '(서머타임 미적용)';
await channel.send(`미국 주식 시장이 열렸습니다. ${dstMessage}`); await channel.send(`미국 주식 시장이 열렸습니다. ${dstMessage}`);
} }
}, null, true, 'Asia/Seoul'); }, null, true, 'Asia/Seoul');
// 미국 주식 시장 폐장 시간 (월~금) → 한국 시간 화~토 새벽에 실행 const closeJob = new CronJob('0 5-6 * * 2-6', async (now) => {
const closeTime = isDaylightSavingTime() ? '0 5 * * 2-6' : '0 6 * * 2-6'; const isDST = isDaylightSavingTime();
const closeJob = new CronJob(closeTime, async () => { const expectedHour = isDST ? 5 : 6;
if (channel) {
if (now.getHours() === expectedHour) {
await channel.send('미국 주식 시장이 닫혔습니다.'); await channel.send('미국 주식 시장이 닫혔습니다.');
} }
}, null, true, 'Asia/Seoul'); }, null, true, 'Asia/Seoul');
// 작업 시작 // 작업 시작
openJob.start(); openJob.start();
closeJob.start(); closeJob.start();

View File

@ -26,6 +26,6 @@ export function isDaylightSavingTime() {
const now = new Date(); const now = new Date();
const jan = new Date(now.getFullYear(), 0, 1); const jan = new Date(now.getFullYear(), 0, 1);
const jul = new Date(now.getFullYear(), 6, 1); const jul = new Date(now.getFullYear(), 6, 1);
const standardOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); const stdTimezoneOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
return now.getTimezoneOffset() < standardOffset; // 서머타임 여부 반환 return now.getTimezoneOffset() < stdTimezoneOffset;
} }