diff --git a/project.clj b/project.clj index f828107..7d804f8 100644 --- a/project.clj +++ b/project.clj @@ -1,10 +1,10 @@ -(defproject clj-cron-parse "0.1.5-SNAPSHOT" +(defproject org.clojars.quartet/clj-cron-parse "0.1.6-SNAPSHOT" :description "A Clojure library for using cron expressions" :url "https://github.com/shmish111/clj-cron-parse" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.6.0"] - [clj-time "0.9.0"] + [clj-time "0.12.0"] [org.clojure/core.match "0.3.0-alpha4"]] :profiles {:dev {:dependencies [[midje "1.6.3"]] :plugins [[lein-cljfmt "0.1.10"] diff --git a/src/clj_cron_parse/core.clj b/src/clj_cron_parse/core.clj index bf4fd82..ccebee8 100644 --- a/src/clj_cron_parse/core.clj +++ b/src/clj_cron_parse/core.clj @@ -191,12 +191,21 @@ (filter #(>= % now)) first)) +(defn next-date-time + [now as] + (->> as + (filter #(or (t/equal? % now) + (t/after? % now))) + first)) + (defn now-with-seconds [now sec] (match sec - ([& xs] :seq) (if-let [ns (next-val (t/second (t/plus now (t/seconds 1))) xs)] - (t/plus now (t/seconds (- ns (t/second now)))) - (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now))))) + ([& xs] :seq) + (let [this-minute (t/to-time-zone (t/floor now t/minute) (.getZone now))] + (or (next-date-time (t/plus now (t/seconds 1)) + (map #(t/plus this-minute (t/seconds %)) xs)) + (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now)))))) :else now)) (defn now-with-minutes diff --git a/test/clj_cron_parse/core_test.clj b/test/clj_cron_parse/core_test.clj index 76e9178..b26ee91 100644 --- a/test/clj_cron_parse/core_test.clj +++ b/test/clj_cron_parse/core_test.clj @@ -76,7 +76,12 @@ (next-date now "@midnight") => (date 2015 01 02 00 00 00 000) (next-date now "@hourly") => (date 2015 01 01 13 00 00 000) (next-date (t/date-time 2015 04 22 06 22 29 000) "30 22 6 * * 3") => (date 2015 04 22 06 22 30 000) - (next-date (t/date-time 2015 04 21 06 22 30 000) "30 22 6 * * 3") => (date 2015 04 22 06 22 30 000))) + (next-date (t/date-time 2015 04 21 06 22 30 000) "30 22 6 * * 3") => (date 2015 04 22 06 22 30 000) + (next-date (t/date-time 2015 04 21 06 00 59 000) "0 * * * * *") => (date 2015 04 21 06 01 00 000) + (next-date (t/date-time 2015 04 21 06 59 00 000) "* 0 * * * *") => (date 2015 04 21 07 00 00 000) + (next-date (t/date-time 2015 04 21 23 00 00 000) "* * 0 * * *") => (date 2015 04 22 00 00 00 000) + (next-date (t/date-time 2015 04 30 00 00 00 000) "* * * 1 * *") => (date 2015 05 01 00 00 00 000) + (next-date (t/date-time 2015 04 21 23 59 59 000) "0 * * * * *") => (date 2015 04 22 00 00 00 000))) ;; TODO: close to new year, combinations, range/n for dow @@ -126,4 +131,4 @@ (next-date now "1 0 12 * * *" "UTC") => (date 2015 01 01 12 00 01 000) (next-date now "1 0 12 * * *" "Asia/Seoul") => (date 2015 01 02 03 00 01 000) (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) - (next-date (t/date-time 2015 01 01 12 00 02) "1 * * * * *" "America/Sao_Paulo") => (t/date-time 2015 01 01 12 01 01)) \ No newline at end of file + (next-date (t/date-time 2015 01 01 12 00 02) "1 * * * * *" "America/Sao_Paulo") => (t/date-time 2015 01 01 12 01 01))