-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.ts
47 lines (44 loc) · 1.35 KB
/
crontab.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const exp = /^((?<predefined>@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(?<every>@every (?<every_value>((\d+)(ns|us|µs|ms|s|m|h))+))|(?<cron>(?<cron_minute>((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*)) (?<cron_hour>((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*)) (?<cron_day_month>((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*)) (?<cron_month>((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*)) (?<cron_day_week>((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*))))$/
const lines = [
'20 16 * * *',
'0 0 0 1 1',
'0 0 0 1 1,2',
'0 0 0 1 1,2,3',
'0 0 * * 1/4',
'0 0 * 0 1-4',
'0 * * * 2/4',
'* * * * *',
// '@annually',
// '@yearly',
// '@monthly',
// '@weekly',
// '@daily',
// '@hourly',
// '@reboot',
// '@every 5s',
// '@every 20h30m',
]
export class Cron {
constructor(
readonly minute: string,
readonly hour: string,
readonly day_month: string,
readonly month: string,
readonly day_week: string,
) { }
static parse(value: string) {
const res = exp.exec(value)?.groups
if (typeof res?.cron === 'string') {
return new Cron(
res.cron_minute,
res.cron_hour,
res.cron_day_month,
res.cron_month,
res.cron_day_week,
)
}
}
}
for (const l of lines) {
console.log(l, Cron.parse(l))
}