( expr: string, count: number = 3, fromDate: Date = new Date(), timezone: string = 'UTC' )
| 195 | * @returns Array of Date objects representing next run times |
| 196 | */ |
| 197 | export function getNextRuns( |
| 198 | expr: string, |
| 199 | count: number = 3, |
| 200 | fromDate: Date = new Date(), |
| 201 | timezone: string = 'UTC' |
| 202 | ): Date[] { |
| 203 | const parts = parseCron(expr); |
| 204 | if (!parts) return []; |
| 205 | |
| 206 | const runs: Date[] = []; |
| 207 | const maxIterations = 525600; // Max 1 year of minutes |
| 208 | |
| 209 | // Start from the next minute |
| 210 | const current = new Date(fromDate); |
| 211 | current.setSeconds(0); |
| 212 | current.setMilliseconds(0); |
| 213 | current.setMinutes(current.getMinutes() + 1); |
| 214 | |
| 215 | let iterations = 0; |
| 216 | while (runs.length < count && iterations < maxIterations) { |
| 217 | if (matchesCron(current, parts)) { |
| 218 | runs.push(new Date(current)); |
| 219 | } |
| 220 | current.setMinutes(current.getMinutes() + 1); |
| 221 | iterations++; |
| 222 | } |
| 223 | |
| 224 | return runs; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Validate a cron expression |
no test coverage detected