Skip to content

Commit

Permalink
crontab: allow start and end of ranges to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Dec 3, 2023
1 parent d2acccb commit 9877489
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 22 additions & 0 deletions extra/crontab/crontab-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,25 @@ CONSTANT: start-timestamp T{ timestamp
"Fri, 3 May 2019 04:05:00 -0700"
}
} [ "5 4 1-5/2 * *" next-few-times ] unit-test

! The first 10 minutes of every 04:00 hour
{
{
"Sun, 24 Mar 2019 04:00:00 -0700"
"Sun, 24 Mar 2019 04:01:00 -0700"
"Sun, 24 Mar 2019 04:02:00 -0700"
"Sun, 24 Mar 2019 04:03:00 -0700"
"Sun, 24 Mar 2019 04:04:00 -0700"
}
} [ "-10 4 * * *" next-few-times ] unit-test

! The last 10 minutes of every 04:00 hour
{
{
"Sun, 24 Mar 2019 04:50:00 -0700"
"Sun, 24 Mar 2019 04:51:00 -0700"
"Sun, 24 Mar 2019 04:52:00 -0700"
"Sun, 24 Mar 2019 04:53:00 -0700"
"Sun, 24 Mar 2019 04:54:00 -0700"
}
} [ "50- 4 * * *" next-few-times ] unit-test
10 changes: 7 additions & 3 deletions extra/crontab/crontab.factor
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ TUPLE: cronentry minutes hours days months days-of-week command ;

<PRIVATE

:: parse-range ( from/f to/f quot: ( value -- value' ) seq -- from to )
from/f to/f
[ [ seq first ] quot if-empty ]
[ [ seq last ] quot if-empty ] bi* ; inline

:: parse-value ( value quot: ( value -- value' ) seq -- value )
value {
{ [ CHAR: , over member? ] [
"," split [ quot seq parse-value ] map concat ] }
{ [ dup "*" = ] [ drop seq ] }
{ [ dup "~" = ] [ drop seq random 1array ] }
{ [ CHAR: / over member? ] [
"/" split1 [
quot seq parse-value
dup length 1 = [ seq swap first seq first - ] [ 0 ] if
over length dup 7 = [ [ <circular> ] 2dip ] [ 1 - ] if
] dip string>number <range> swap nths ] }
{ [ CHAR: - over member? ] [
"-" split1 quot bi@ [a..b] ] }
"-" split1 quot seq parse-range [a..b] ] }
{ [ CHAR: ~ over member? ] [
"~" split1 quot bi@ [a..b] random 1array ] }
"~" split1 quot seq parse-range [a..b] random 1array ] }
[ quot call 1array ]
} cond members sort ; inline recursive

Expand Down

0 comments on commit 9877489

Please sign in to comment.