Skip to content
This repository was archived by the owner on Aug 30, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
15 changes: 12 additions & 3 deletions src/clj_cron_parse/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions test/clj_cron_parse/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
(next-date (t/date-time 2015 01 01 12 00 02) "1 * * * * *" "America/Sao_Paulo") => (t/date-time 2015 01 01 12 01 01))