1 ! Copyright (C) 2018 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays ascii assocs calendar calendar.english
5 calendar.private combinators io kernel literals locals math
6 math.order math.parser math.ranges sequences splitting ;
10 :: parse-value ( value quot: ( value -- value' ) seq -- value )
12 { [ CHAR: , over member? ] [
13 "," split [ quot seq parse-value ] map concat ] }
14 { [ dup "*" = ] [ drop seq ] }
15 { [ CHAR: / over member? ] [
16 "/" split1 [ quot seq parse-value 0 over length 1 - ] dip
17 string>number <range> swap nths ] }
18 { [ CHAR: - over member? ] [
19 "-" split1 quot bi@ [a,b] ] }
21 } cond ; inline recursive
23 : parse-day ( str -- n )
24 dup string>number [ ] [
25 >lower $[ day-abbreviations3 [ >lower ] map ] index
28 : parse-month ( str -- n )
29 dup string>number [ ] [
30 >lower $[ month-abbreviations [ >lower ] map ] index
33 TUPLE: cronentry minutes hours days months days-of-week command ;
36 { "@yearly" "0 0 1 1 *" }
37 { "@annually" "0 0 1 1 *" }
38 { "@monthly" "0 0 1 * *" }
39 { "@weekly" "0 0 * * 0" }
40 { "@daily" "0 0 * * *" }
41 { "@midnight" "0 0 * * *" }
42 { "@hourly" "0 * * * *" }
45 : parse-cronentry ( entry -- cronentry )
46 " " split1 [ aliases ?at drop ] dip " " glue
47 " " split1 " " split1 " " split1 " " split1 " " split1 {
48 [ [ string>number ] T{ range f 0 60 1 } parse-value ]
49 [ [ string>number ] T{ range f 0 24 1 } parse-value ]
50 [ [ string>number ] T{ range f 1 31 1 } parse-value ]
51 [ [ parse-month ] T{ range f 1 12 1 } parse-value ]
52 [ [ parse-day ] T{ range f 0 7 1 } parse-value ]
54 } spread cronentry boa ;
58 :: (next-time-after) ( cronentry timestamp -- )
60 f ! should we keep searching for a matching time
62 timestamp month>> :> month
63 cronentry months>> [ month >= ] find nip
64 dup month = [ drop ] [
65 [ cronentry months>> first timestamp 1 +year drop ] unless*
66 timestamp 1 >>day 0 >>hour 0 >>minute month<< drop t
69 timestamp day>> :> day
70 cronentry days>> [ day >= ] find nip
72 [ cronentry days>> first timestamp 1 +month drop ] unless*
73 timestamp 0 >>hour 0 >>minute day<< drop t
76 timestamp day-of-week :> weekday
77 cronentry days-of-week>> [ weekday >= ] find nip [
78 cronentry days-of-week>> first 7 +
80 timestamp 0 >>hour 0 >>minute swap +day 2drop t
83 timestamp hour>> :> hour
84 cronentry hours>> [ hour >= ] find nip
86 [ cronentry hours>> first timestamp 1 +day drop ] unless*
87 timestamp 0 >>minute hour<< drop t
90 timestamp minute>> :> minute
91 cronentry minutes>> [ minute >= ] find nip
92 dup minute = [ drop ] [
93 [ cronentry minutes>> first timestamp 1 +hour drop ] unless*
94 timestamp minute<< drop t
97 [ cronentry timestamp (next-time-after) ] when ;
101 : next-time-after ( cronentry timestamp -- timestamp )
102 1 minutes time+ 0 >>second [ (next-time-after) ] keep ;
104 : next-time ( cronentry -- timestamp )
105 now next-time-after ;
107 : next-times-after ( cronentry n timestamp -- timestamps )
108 swap [ dupd next-time-after dup ] replicate 2nip ;
110 : next-times ( cronentry n -- timestamps )
111 now next-times-after ;
113 : read-crontab ( -- entries )
114 lines harvest [ parse-cronentry ] map ;