import { CronExpressionParser } from 'cron-parser'; import type { CronSchedule } from './types.js'; export const MIN_INTERVAL_MS = 60_000; export function intervalToHuman(intervalMs: number): string { const units = [ { ms: 7 * 24 * 60 * 60_000, label: '周' }, { ms: 24 * 60 * 60_000, label: '天' }, { ms: 60 * 60_000, label: '小时' }, { ms: 60_000, label: '分钟' }, ]; for (const unit of units) { if (intervalMs % unit.ms === 0) return `每 ${intervalMs / unit.ms} ${unit.label}`; } return `每 ${Math.round(intervalMs / 60_000)} 分钟`; } /** Return the first wall-clock slot strictly after `fromMs`. */ export function nextScheduleRunMs(schedule: CronSchedule, fromMs: number): number | null { if (schedule.type === 'once') return Date.parse(schedule.at) > fromMs ? Date.parse(schedule.at) : null; if (schedule.type === 'cron') return cronIterator(schedule, fromMs).next().getTime(); const anchor = Date.parse(schedule.anchorAt); if (!Number.isFinite(anchor) || schedule.intervalMs < MIN_INTERVAL_MS) return null; if (fromMs < anchor) return anchor; return anchor + (Math.floor((fromMs - anchor) / schedule.intervalMs) + 1) * schedule.intervalMs; } /** Return the latest slot at or before `atMs`. */ export function latestScheduleRunMs(schedule: CronSchedule, atMs: number): number | null { if (schedule.type === 'once') return Date.parse(schedule.at) <= atMs ? Date.parse(schedule.at) : null; if (schedule.type === 'interval') { const anchor = Date.parse(schedule.anchorAt); if (!Number.isFinite(anchor) || atMs < anchor || schedule.intervalMs < MIN_INTERVAL_MS) { return null; } return anchor + Math.floor((atMs - anchor) / schedule.intervalMs) * schedule.intervalMs; } return cronIterator(schedule, atMs + 1) .prev() .getTime(); } export function countScheduleSlots( schedule: CronSchedule, afterExclusiveMs: number, throughInclusiveMs: number, ): number { if (throughInclusiveMs <= afterExclusiveMs) return 0; if (schedule.type === 'cron' && schedule.cron.trim().replace(/\s+/g, ' ') === '* * * * *') { return Math.floor(throughInclusiveMs / 60000) - Math.floor(afterExclusiveMs / 60000); } if (schedule.type === 'once') return Date.parse(schedule.at) > afterExclusiveMs && Date.parse(schedule.at) <= throughInclusiveMs ? 1 : 0; if (schedule.type === 'interval') { const first = nextScheduleRunMs(schedule, afterExclusiveMs); if (!first || first > throughInclusiveMs) return 0; return Math.floor((throughInclusiveMs - first) / schedule.intervalMs) + 1; } let count = 0; let cursor = afterExclusiveMs; const iterator = cronIterator(schedule, cursor); while (true) { const next = iterator.next().getTime(); if (!next || next > throughInclusiveMs) break; count++; cursor = next; } return count; } export function scheduleAnchorMs(schedule: CronSchedule, createdAt: string): number { const value = schedule.type === 'interval' ? schedule.anchorAt : createdAt; const parsed = Date.parse(value); return Number.isFinite(parsed) ? parsed : Date.now(); } function cronIterator(schedule: Extract, currentDate: number) { return CronExpressionParser.parse(schedule.cron, { currentDate, tz: schedule.timezone === 'local' ? undefined : schedule.timezone, }); }