Files
triggerdotdev--trigger.dev/apps/webapp/app/v3/utils/calculateNextSchedule.server.ts
T
2026-07-13 13:32:57 +08:00

60 lines
1.4 KiB
TypeScript

import { parseExpression } from "cron-parser";
export function calculateNextScheduledTimestampFromNow(schedule: string, timezone: string | null) {
return calculateNextScheduledTimestamp(schedule, timezone, new Date());
}
export function calculateNextScheduledTimestamp(
schedule: string,
timezone: string | null,
currentDate: Date = new Date()
) {
return calculateNextStep(schedule, timezone, currentDate);
}
function calculateNextStep(schedule: string, timezone: string | null, currentDate: Date) {
return parseExpression(schedule, {
currentDate,
utc: timezone === null,
tz: timezone ?? undefined,
})
.next()
.toDate();
}
export function previousScheduledTimestamp(
schedule: string,
timezone: string | null,
fromTimestamp: Date = new Date()
) {
return parseExpression(schedule, {
currentDate: fromTimestamp,
utc: timezone === null,
tz: timezone ?? undefined,
})
.prev()
.toDate();
}
export function nextScheduledTimestamps(
cron: string,
timezone: string | null,
lastScheduledTimestamp: Date,
count: number = 1
) {
const result: Array<Date> = [];
let nextScheduledTimestamp = lastScheduledTimestamp;
for (let i = 0; i < count; i++) {
nextScheduledTimestamp = calculateNextScheduledTimestamp(
cron,
timezone,
nextScheduledTimestamp
);
result.push(nextScheduledTimestamp);
}
return result;
}