├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── project.clj ├── src └── clj_cron_parse │ └── core.clj └── test └── clj_cron_parse └── core_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | *.iml 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: clojure 2 | script: lein2 do clean, midje, cljfmt check 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clj-cron-parse 2 | 3 | A Clojure library for using cron expressions. 4 | 5 | Current build status: [![Build Status](https://travis-ci.org/shmish111/clj-cron-parse.png)](https://travis-ci.org/shmish111/clj-cron-parse) 6 | 7 | The library mainly consists of one function, `next-date`, which takes a cron expression and a from date and returns the 8 | next `org.joda.time.DateTime` to occur for that cron expression. From this function you should be able to do anything 9 | you want using cron expressions. 10 | 11 | ## Usage 12 | 13 | In [Leiningen](http://github.com/technomancy/leiningen/) add the dependency [![Clojars Project](http://clojars.org/clj-cron-parse/latest-version.svg)](http://clojars.org/clj-cron-parse) 14 | 15 | ```clojure 16 | (require '[clj-cron-parse.core :refer [next-date]]) 17 | (def now (t/date-time 2015 01 01 12 00 00 000)) 18 | 19 | (def next-every-second 20 | (next-date now "1 * * * * *")) 21 | 22 | (def next-every-year 23 | (next-date now "@yearly")) 24 | ``` 25 | 26 | ## License 27 | 28 | Copyright © 2015 David Smith 29 | 30 | Distributed under the Eclipse Public License either version 1.0 or (at 31 | your option) any later version. 32 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clj-cron-parse "0.1.5-SNAPSHOT" 2 | :description "A Clojure library for using cron expressions" 3 | :url "https://github.com/finity-ai/clj-cron-parse" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.11.1"] 7 | [clj-time "0.15.2"] 8 | [org.clojure/core.match "1.0.0"]] 9 | :pedantic? :abort 10 | :profiles {:dev {:dependencies [[midje "1.10.5"]] 11 | :plugins [[lein-cljfmt "0.8.0"] 12 | [lein-midje "3.2.2"]]}} 13 | :release-tasks [["vcs" "assert-committed"] 14 | ["change" "version" 15 | "leiningen.release/bump-version" "release"] 16 | ["vcs" "commit"] 17 | ["vcs" "tag" "--no-sign"] 18 | ["deploy"]] 19 | :deploy-repositories {"releases" {:url "https://repo.clojars.org" :creds :gpg :sign-releases false}}) 20 | -------------------------------------------------------------------------------- /src/clj_cron_parse/core.clj: -------------------------------------------------------------------------------- 1 | (ns clj-cron-parse.core 2 | (:require [clojure.string :as s] 3 | [clj-time.core :as t] 4 | [clojure.core.match :refer [match]] 5 | [clj-time.predicates :as pr])) 6 | 7 | (defn int-or-nil 8 | [s] 9 | (try 10 | (Integer/parseInt s) 11 | (catch Exception _ nil))) 12 | 13 | (defn num-or-range 14 | [s] 15 | (let [x (re-find #"^\d+$" s)] 16 | (if-not (empty? x) 17 | (Integer/parseInt x) 18 | (if-let [[_ b t _ s] (re-find #"^(\d+)-(\d+)(/(\d+))?$" s)] 19 | (let [r (range (Integer/parseInt b) (inc (Integer/parseInt t)) (Integer/parseInt (or s "1")))] 20 | (if (empty? r) nil r)) 21 | (if-let [[_ step] (re-find #"^\*/(\d+)$" s)] 22 | {:range (Integer/parseInt step)} 23 | nil))))) 24 | 25 | (defn parse-single-month 26 | [s] 27 | (let [s (s/upper-case s)] 28 | (cond 29 | (= "JAN" s) 1 30 | (= "FEB" s) 2 31 | (= "MAR" s) 3 32 | (= "APR" s) 4 33 | (= "MAY" s) 5 34 | (= "JUN" s) 6 35 | (= "JUL" s) 7 36 | (= "AUG" s) 8 37 | (= "SEP" s) 9 38 | (= "OCT" s) 10 39 | (= "NOV" s) 11 40 | (= "DEC" s) 12 41 | :else (int-or-nil s)))) 42 | 43 | (defn month-num-or-range 44 | [s] 45 | (let [x (re-find #"^(\d+|\w{3})$" s)] 46 | (if-not (empty? x) 47 | (parse-single-month (second x)) 48 | (if-let [[_ b t _ s] (re-find #"^(\d+|\w{3})-(\d+|\w{3})(/(\d+))?$" s)] 49 | (let [r (range (parse-single-month b) (inc (parse-single-month t)) (Integer/parseInt (or s "1")))] 50 | (if (empty? r) nil r)) 51 | (if-let [[_ step] (re-find #"^\*/(\d+)$" s)] 52 | {:range (Integer/parseInt step)} 53 | nil))))) 54 | 55 | (defn parse-single-day 56 | [s] 57 | (let [s (s/upper-case s)] 58 | (cond 59 | (= "MON" s) 1 60 | (= "TUE" s) 2 61 | (= "WED" s) 3 62 | (= "THU" s) 4 63 | (= "FRI" s) 5 64 | (= "SAT" s) 6 65 | (= "SUN" s) 7 66 | (= "W" s) [1 2 3 4 5] 67 | (= "1L" s) :1L 68 | (= "2L" s) :2L 69 | (= "3L" s) :3L 70 | (= "4L" s) :4L 71 | (= "5L" s) :5L 72 | (= "6L" s) :6L 73 | (= "7L" s) :7L 74 | :else (let [n (int-or-nil s)] 75 | (if (= 0 n) 7 n))))) 76 | 77 | (defn dow-num-or-range 78 | [s] 79 | (let [x (re-find #"^(\d+|\w{3}|L|W|1L|2L|3L|4L|5L|6L|7L)$" s)] 80 | (if-not (empty? x) 81 | (parse-single-day (second x)) 82 | (let [y (re-find #"^(\d+|\w{3})-(\d+|\w{3})$" s)] 83 | (if y (->> (rest y) 84 | (map parse-single-day) 85 | ((fn [[a b]] (concat (range a b) [b])))) 86 | (if-let [[_ step] (re-find #"^\*/(\d+)$" s)] 87 | (range 1 7 (Integer/parseInt step)) 88 | nil)))))) 89 | 90 | (defn parse-item 91 | [s range-fn] 92 | (cond 93 | (= s "*") :star 94 | (= s "L") :L 95 | (= s "W") :W 96 | :else (let [x (s/split s #",")] 97 | (if (empty? x) nil 98 | (let [y (->> x 99 | (map range-fn) 100 | flatten 101 | distinct)] 102 | (if (and 103 | (every? identity y) 104 | (seq y)) 105 | (sort y) 106 | nil)))))) 107 | 108 | (defn parse-dow-item 109 | [s] 110 | (cond 111 | (= s "*") :star 112 | :else (let [x (s/split s #",")] 113 | (if (empty? x) nil 114 | (let [y (->> x 115 | (map dow-num-or-range) 116 | flatten 117 | distinct)] 118 | (if (and 119 | (every? identity y) 120 | (seq y)) 121 | (sort y) 122 | nil)))))) 123 | 124 | (defn bound-seq? 125 | [minimum maximum xs] 126 | (and (coll? xs) 127 | (not-empty xs) 128 | (every? number? xs) 129 | (<= minimum (first xs) (last xs) maximum))) 130 | 131 | (defn next-divisible 132 | [x d] 133 | (if (= x 0) 134 | 0 135 | (* d (+ 1 (quot x d))))) 136 | 137 | (defn parse-field 138 | [minimum maximum range-fn s] 139 | (let [d (parse-item s range-fn)] 140 | (match (parse-item s range-fn) 141 | nil nil 142 | :star :star 143 | ([{:range r}] :seq) (range (next-divisible minimum r) maximum r) 144 | (xs :guard (partial bound-seq? minimum maximum)) d 145 | :else nil))) 146 | 147 | (defn parse-dom 148 | [s] 149 | (let [d (parse-item s num-or-range)] 150 | (match d 151 | nil nil 152 | :star :star 153 | :L :L 154 | :W :W 155 | ([{:range _} :as r] :seq) r 156 | (xs :guard (partial bound-seq? 1 31)) d 157 | :else nil))) 158 | 159 | (defn parse-dow 160 | [s] 161 | (let [d (parse-dow-item s)] 162 | (match d 163 | nil nil 164 | :star :star 165 | (xs :guard (partial bound-seq? 1 7)) d 166 | (xs :guard #(seq %)) d 167 | :else nil))) 168 | 169 | (defn make-cron-map 170 | [cron] 171 | (match (s/split cron #" ") 172 | [sec minute hour dom month dow] (let [cron-map {:dow (parse-dow dow) 173 | :month (parse-field 1 12 month-num-or-range month) 174 | :dom (parse-dom dom) 175 | :hour (parse-field 0 23 num-or-range hour) 176 | :minute (parse-field 0 59 num-or-range minute) 177 | :sec (parse-field 0 59 num-or-range sec)}] 178 | (if (every? identity (vals cron-map)) 179 | cron-map 180 | nil)) 181 | ["@yearly"] {:dow :star :month [1] :dom [1] :hour [0] :minute [0] :sec [0]} 182 | ["@annually"] {:dow :star :month [1] :dom [1] :hour [0] :minute [0] :sec [0]} 183 | ["@monthly"] {:dow :star :month :star :dom [1] :hour [0] :minute [0] :sec [0]} 184 | ["@weekly"] {:dow [1] :month :star :dom :star :hour [0] :minute [0] :sec [0]} 185 | ["@daily"] {:dow :star :month :star :dom :star :hour [0] :minute [0] :sec [0]} 186 | ["@midnight"] {:dow :star :month :star :dom :star :hour [0] :minute [0] :sec [0]} 187 | ["@hourly"] {:dow :star :month :star :dom :star :hour :star :minute [0] :sec [0]} 188 | :else nil)) 189 | 190 | (defn next-val 191 | [now as] 192 | (->> as 193 | (filter #(>= % now)) 194 | first)) 195 | 196 | (defn now-with-seconds 197 | [now sec] 198 | (match sec 199 | ([& xs] :seq) (if-let [ns (next-val (t/second (t/plus now (t/seconds 1))) xs)] 200 | (t/plus now (t/seconds (- ns (t/second now)))) 201 | (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now))))) 202 | :else now)) 203 | 204 | (defn now-with-minutes 205 | [now minute] 206 | (match minute 207 | ([& xs] :seq) (if-let [ns (next-val (t/minute now) xs)] 208 | (t/plus now (t/minutes (- ns (t/minute now)))) 209 | (t/plus now (t/hours 1) (t/minutes (- (first xs) (t/minute now))))) 210 | :else now)) 211 | 212 | (defn now-with-hours 213 | [now hour] 214 | (match hour 215 | ([& xs] :seq) (if-let [ns (next-val (t/hour now) xs)] 216 | (t/plus now (t/hours (- ns (t/hour now)))) 217 | (t/plus now (t/days 1) (t/hours (- (first xs) (t/hour now))))) 218 | :else now)) 219 | 220 | (defn next-week-dom 221 | [now] 222 | (let [tomorrow (t/plus now (t/days 1))] 223 | (if (pr/weekday? tomorrow) 224 | tomorrow 225 | (next-week-dom tomorrow)))) 226 | 227 | (defn now-with-doms 228 | [now dom] 229 | (match dom 230 | :L (-> now 231 | (t/plus (t/months 1)) 232 | (t/minus (t/days (t/day now)))) 233 | :W (next-week-dom now) 234 | ([& xs] :seq) (if-let [ns (next-val (t/day now) xs)] 235 | (t/plus now (t/days (- ns (t/day now)))) 236 | (t/plus now (t/months 1) (t/days (- (first xs) (t/day now))))) 237 | {:range x} (t/plus now (t/days 1)) 238 | :else now)) 239 | 240 | (defn last-dow-of-month 241 | [now ls] 242 | (letfn [(f [d] 243 | (let [day-of-week (t/day-of-week (t/last-day-of-the-month now)) 244 | diff (- day-of-week d) 245 | diff2 (if (neg? diff) 246 | (- (+ 7 day-of-week) d) 247 | diff)] 248 | (t/minus (t/last-day-of-the-month now) (t/days diff2))))] 249 | (->> ls 250 | (map #(match % 251 | :1L (f 1) 252 | :2L (f 2) 253 | :3L (f 3) 254 | :4L (f 4) 255 | :5L (f 5) 256 | :6L (f 6) 257 | :7L (f 7) 258 | :else nil)) 259 | sort 260 | first))) 261 | 262 | (defn now-with-dows 263 | [now dow] 264 | (match dow 265 | :star now 266 | (xs :guard (partial bound-seq? 1 7)) (if-let [ns (next-val (t/day-of-week now) xs)] 267 | (t/plus now (t/days (- ns (t/day-of-week now)))) 268 | (t/plus now (t/days (- 7 (t/day-of-week now) (* -1 (first xs)))))) 269 | (xs :guard #(seq %)) (last-dow-of-month now xs) 270 | :else now)) 271 | 272 | (defn now-with-months 273 | [now month] 274 | (match month 275 | ([& xs] :seq) (if-let [ns (next-val (t/month now) xs)] 276 | (t/plus now (t/months (- ns (t/month now)))) 277 | (t/plus now (t/years 1) (t/months (- (first xs) (t/month now))))) 278 | :else now)) 279 | 280 | (defn next-date-by-dom 281 | [now {:keys [sec minute hour dom month]}] 282 | (-> now 283 | (now-with-seconds sec) 284 | (now-with-minutes minute) 285 | (now-with-hours hour) 286 | (now-with-doms dom) 287 | (now-with-months month))) 288 | 289 | (defn next-date-by-dow 290 | [now {:keys [sec minute hour dow month] :as e}] 291 | (-> now 292 | (now-with-seconds sec) 293 | (now-with-minutes minute) 294 | (now-with-hours hour) 295 | (now-with-dows dow) 296 | (now-with-months month))) 297 | 298 | (defn next-date 299 | "takes an org.joda.time.DateTime representing now and a cron expression and returns the next org.joda.time.DateTime to occur for that cron exp. 300 | If it is not possible to parse the cron expression then nil is returned. 301 | 302 | If you provide the value of now as a DateTime in a timezone, it will return the cron calculated within that timezone. 303 | To view the value in UTC simply convert it to the UTC timezone. 304 | 305 | Alternatively you can provide a timezone id such as \"Asia/Seoul\" (defaults to \"UTC\") as the final argument and you 306 | will get back the next date in UTC time for the cron expression calculated in that timezone. For example, you want the next UTC 307 | date for every day at midday in Seoul (next-date now \"0 0 12 * * *\" \"Asia/Seoul\"). 308 | 309 | The cron expressions attempt to follow BSD crontab by Paul Vixie with the addition of seconds in the first position and W can be used for both 310 | day of month and day of week. 311 | 312 | field allowed values 313 | ----- -------------- 314 | second 0-59 315 | minute 0-59 316 | hour 0-23 317 | day of month 1-31 L W 318 | month 1-12 (or names, see below) 319 | day of week 0-7 (0 or 7 is Sun, or use names) W 1L 2L 3L 4L 5L 6L 7L 320 | 321 | A field may be an asterisk (*), which always stands for ``first-last''. 322 | 323 | Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an ``hours'' entry specifies execution 324 | at hours 8, 9, 10 and 11. 325 | 326 | Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: ``1,2,5,9'', ``0-4,8-12''. 327 | 328 | Step values can be used in conjunction with ranges. Following a range with ``/'' specifies skips of the number's value through the range. For example, ``0-23/2'' can be 329 | used in the hours field to specify command execution every other hour (the alternative in the V7 standard is ``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also permitted after 330 | an asterisk, so if you want to say ``every two hours'', just use ``*/2''. 331 | 332 | Names can also be used for the ``month'' and ``day of week'' fields. Use the first three letters of the particular day or month (case does not matter). 333 | 334 | Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. 335 | If both fields are restricted (ie, are not *), the earliest time will be returned. 336 | For example, ``30 4 1,15 * 5'' would return the next date matching 4:30 am on the 1st and 15th of each month, plus every Friday. 337 | 338 | Instead of the first five fields, one of eight special strings may appear: 339 | 340 | string meaning 341 | ------ ------- 342 | @yearly Run once a year, \"0 0 0 1 1 *\". 343 | @annually (same as @yearly) 344 | @monthly Run once a month, \"0 0 0 1 * *\". 345 | @weekly Run once a week, \"0 0 0 * * 0\". 346 | @daily Run once a day, \"0 0 0 * * *\". 347 | @midnight (same as @daily) 348 | @hourly Run once an hour, \"0 0 * * * *\". 349 | " 350 | 351 | ([now cron] 352 | (if-let [{:keys [dom dow] :as cron-map} (make-cron-map cron)] 353 | (match [dom dow] 354 | [:star :star] (next-date-by-dom now cron-map) 355 | [_ :star] (next-date-by-dom now cron-map) 356 | [:star _] (next-date-by-dow now cron-map) 357 | :else (let [by-dom (next-date-by-dom now cron-map) 358 | by-dow (next-date-by-dow now cron-map)] 359 | (-> [by-dom by-dow] sort first))) 360 | nil)) 361 | 362 | ([now cron timezone] 363 | (if timezone 364 | (let [tz-now (t/to-time-zone now (t/time-zone-for-id timezone))] 365 | (t/to-time-zone (next-date tz-now cron) (t/time-zone-for-id "UTC"))) 366 | (next-date now cron)))) 367 | -------------------------------------------------------------------------------- /test/clj_cron_parse/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-cron-parse.core-test 2 | (:require [clj-cron-parse.core :refer :all] 3 | [midje.sweet :refer :all] 4 | [clj-time.core :as t])) 5 | 6 | (defchecker date [& date-args] 7 | (checker [actual] 8 | (let [d (apply t/date-time date-args)] 9 | (= d actual)))) 10 | 11 | (def now (t/date-time 2015 01 01 12 00 00 000)) 12 | (def nye (t/date-time 2014 12 31 12 00 00 000)) 13 | 14 | (time (facts "should find next date for cron expression" 15 | (next-date now "1 * * * * *") => (date 2015 01 01 12 00 01 000) 16 | (next-date (t/date-time 2022 05 24 10 59 00) "0 */5 * * * *") => (t/date-time 2022 05 24 11 00 00) 17 | (next-date now "* 1 * * * *") => (date 2015 01 01 12 01 00 000) 18 | (next-date now "* * 13 * * *") => (date 2015 01 01 13 00 00 000) 19 | (next-date now "* * * 10 * *") => (date 2015 01 10 12 00 00 000) 20 | (next-date now "* * * * 2 *") => (date 2015 02 01 12 00 00 000) 21 | (next-date now "11 12 13 14 10 *") => (date 2015 10 14 13 12 11 000) 22 | (next-date now "* * * * * 0") => (date 2015 01 04 12 00 00 000) 23 | (next-date now "* * * * * 3") => (date 2015 01 07 12 00 00 000) 24 | (next-date nye "* * 10 * * *") => (date 2015 01 01 10 00 00 000) 25 | (next-date now "1,2 * * * * *") => (date 2015 01 01 12 00 01 000) 26 | (next-date now "1-20 * * * * *") => (date 2015 01 01 12 00 01 000) 27 | (next-date now "*/2 * * * * *") => (date 2015 01 01 12 00 02 000) 28 | (next-date now "1-20/2 * * * * *") => (date 2015 01 01 12 00 01 000) 29 | (next-date (t/date-time 2015 01 01 12 00 04 000) "3-20/2 * * * * *") => (date 2015 01 01 12 00 05 000) 30 | (next-date now "* 1,2 * * * *") => (date 2015 01 01 12 01 00 000) 31 | (next-date now "* */2 * * * *") => (date 2015 01 01 12 00 00 000) 32 | (next-date now "* 1-20 * * * *") => (date 2015 01 01 12 01 00 000) 33 | (next-date now "* 1-20/2 * * * *") => (date 2015 01 01 12 01 00 000) 34 | (next-date (t/date-time 2015 01 01 12 05 00 000) "* 1-20/3 * * * *") => (date 2015 01 01 12 07 00 000) 35 | (next-date now "* * 1,2 * * *") => (date 2015 01 02 01 00 00 000) 36 | (next-date (t/date-time 2015 01 01 13 00 00 000) "* * */2 * * *") => (date 2015 01 01 14 00 00 000) 37 | (next-date (t/date-time 2015 01 01 21 00 00 000) "* * 1-20 * * *") => (date 2015 01 02 01 00 00 000) 38 | (next-date now "* * 1-20/2 * * *") => (date 2015 01 01 13 00 00 000) 39 | (next-date now "* * 11 1,2 * *") => (date 2015 01 02 11 00 00 000) 40 | (next-date now "* * * */2 * *") => (date 2015 01 02 12 00 00 000) 41 | (next-date now "* * 11 1-20 * *") => (date 2015 01 02 11 00 00 000) 42 | (next-date now "* * 11 1-20/2 * *") => (date 2015 01 03 11 00 00 000) 43 | (next-date now "* * * L * *") => (date 2015 01 31 12 00 00 000) 44 | (next-date now "* * * W * *") => (date 2015 01 02 12 00 00 000) 45 | (next-date (t/date-time 2015 01 02 12 00 00 000) "* * * W * *") => (date 2015 01 05 12 00 00 000) 46 | (next-date now "* * * * FEB *") => (date 2015 02 01 12 00 00 000) 47 | (next-date now "* * * * feb *") => (date 2015 02 01 12 00 00 000) 48 | (next-date now "* * * * 2 *") => (date 2015 02 01 12 00 00 000) 49 | (next-date now "* * * * 2,3 *") => (date 2015 02 01 12 00 00 000) 50 | (next-date now "* * * * */2 *") => (date 2015 02 01 12 00 00 000) 51 | (next-date now "* * * * 2-11 *") => (date 2015 02 01 12 00 00 000) 52 | (next-date now "* * * * 2-11/3 *") => (date 2015 02 01 12 00 00 000) 53 | (next-date (t/date-time 2015 03 01 12 00 00 000) "* * * * 1-11/3 *") => (date 2015 04 01 12 00 00 000) 54 | (next-date now "* * * * * 1,2") => (date 2015 01 05 12 00 00 000) 55 | (next-date now "* * 11 * * 1-5") => (date 2015 01 02 11 00 00 000) 56 | (next-date now "* * 11 * * W") => (date 2015 01 02 11 00 00 000) 57 | (next-date now "* * * * * MON") => (date 2015 01 05 12 00 00 000) 58 | (next-date now "* * * * * mon") => (date 2015 01 05 12 00 00 000) 59 | (next-date now "* * * * * */2") => (date 2015 01 02 12 00 00 000) 60 | ;(next-date now "* * * * * 1-5/2") => (bit of an edge case I think) 61 | (next-date now "* * * * * 1L") => (date 2015 01 26 12 00 00 000) 62 | (next-date now "* * * * * 1L,2L") => (date 2015 01 26 12 00 00 000) 63 | (next-date now "* * * * * 2L") => (date 2015 01 27 12 00 00 000) 64 | (next-date now "* * * * * 3L") => (date 2015 01 28 12 00 00 000) 65 | (next-date now "* * * * * 4L") => (date 2015 01 29 12 00 00 000) 66 | (next-date now "* * * * * 5L") => (date 2015 01 30 12 00 00 000) 67 | (next-date now "* * * * * 6L") => (date 2015 01 31 12 00 00 000) 68 | (next-date now "* * * * * 7L") => (date 2015 01 25 12 00 00 000) 69 | (next-date now "* * * * * 6L,7L") => (date 2015 01 25 12 00 00 000) 70 | (next-date (t/date-time 2015 01 02 12 00 00 000) "* * 11 * * 1-5") => (date 2015 01 05 11 00 00 000) 71 | (next-date (t/date-time 2015 01 02 12 00 00 000) "* * 11 * * W") => (date 2015 01 05 11 00 00 000) 72 | (next-date now "@yearly") => (date 2016 01 01 00 00 00 000) 73 | (next-date now "@annually") => (date 2016 01 01 00 00 00 000) 74 | (next-date now "@monthly") => (date 2015 02 01 00 00 00 000) 75 | (next-date now "@weekly") => (date 2015 01 05 00 00 00 000) 76 | (next-date now "@daily") => (date 2015 01 02 00 00 00 000) 77 | (next-date now "@midnight") => (date 2015 01 02 00 00 00 000) 78 | (next-date now "@hourly") => (date 2015 01 01 13 00 00 000) 79 | (next-date (t/date-time 2015 04 22 06 22 29 000) "30 22 6 * * 3") => (date 2015 04 22 06 22 30 000) 80 | (next-date (t/date-time 2015 04 21 06 22 30 000) "30 22 6 * * 3") => (date 2015 04 22 06 22 30 000))) 81 | 82 | ;; TODO: close to new year, combinations, range/n for dow 83 | 84 | (facts "should return nil for an invalid cron expression" 85 | (next-date now "x * * * * *") => nil 86 | (next-date now "* x * * * *") => nil 87 | (next-date now "* * x * * *") => nil 88 | (next-date now "* * * x * *") => nil 89 | (next-date now "* * * * x *") => nil 90 | (next-date now "* * * * * x") => nil 91 | (next-date now "L * * * * *") => nil 92 | (next-date now "* L * * * *") => nil 93 | (next-date now "* * L * * *") => nil 94 | (next-date now "* * * 1L * *") => nil 95 | (next-date now "* * * * L *") => nil 96 | (next-date now "* * * * * L") => nil 97 | (next-date now "W * * * * *") => nil 98 | (next-date now "* W * * * *") => nil 99 | (next-date now "* * W * * *") => nil 100 | (next-date now "* * * * W *") => nil 101 | (next-date now "61 * * * * *") => nil 102 | (next-date now "* 61 * * * *") => nil 103 | (next-date now "* * 25 * * *") => nil 104 | (next-date now "* * * 32 * *") => nil 105 | (next-date now "* * * * 13 *") => nil 106 | (next-date now "* * * * * * *") => nil 107 | (next-date now "1,62 * * * * *") => nil 108 | (next-date now "* 1,62 * * * *") => nil 109 | (next-date now "* * 1,25 * * *") => nil 110 | (next-date now "* * * 1,32 * *") => nil 111 | (next-date now "* * * * 2,13 *") => nil 112 | (next-date now "* * * * * 1,8") => nil 113 | (next-date now "1-62 * * * * *") => nil 114 | (next-date now "* 1-62 * * * *") => nil 115 | (next-date now "* * 1-25 * * *") => nil 116 | (next-date now "* * * 1-32 * *") => nil 117 | (next-date now "* * * * 2-13 *") => nil 118 | (next-date now "* * * * * 1-8") => nil 119 | (next-date now "* * * * * 8L") => nil 120 | (next-date now "* * * * febx *") => nil 121 | (next-date now "* * * * * MONx") => nil 122 | (next-date now "s s") => nil 123 | (next-date now "") => nil) 124 | 125 | (facts "should provide next date calculated within a timezone" 126 | (next-date now "1 0 12 * * *" nil) => (date 2015 01 01 12 00 01 000) 127 | (next-date now "1 0 12 * * *" "UTC") => (date 2015 01 01 12 00 01 000) 128 | (next-date now "1 0 12 * * *" "Asia/Seoul") => (date 2015 01 02 03 00 01 000) 129 | (next-date (t/date-time 2015 01 01 12 00 02) "1 0 12 * * *" "America/Sao_Paulo") => (date 2015 01 01 14 00 01 000) 130 | (next-date (t/date-time 2015 01 01 12 00 02) "1 * * * * *" "America/Sao_Paulo") => (t/date-time 2015 01 01 12 01 01)) 131 | --------------------------------------------------------------------------------