]> gitweb.factorcode.org Git - factor.git/commitdiff
crontab: allow start and end of ranges to be optional
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 3 Dec 2023 00:08:49 +0000 (16:08 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 3 Dec 2023 00:08:49 +0000 (16:08 -0800)
extra/crontab/crontab-tests.factor
extra/crontab/crontab.factor

index 6874249741dff1a3868cccb40124ef9ac5fecfe2..f7173509694664e5abaff332b13e0f17ceded5b5 100644 (file)
@@ -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
index 66521a5d7002c666fd3d481a11179bbbf679a4b1..1bbb9a96060d654ade4a65b2fb1833031fe445a6 100644 (file)
@@ -15,12 +15,16 @@ 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
@@ -28,9 +32,9 @@ TUPLE: cronentry minutes hours days months days-of-week command ;
                 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