From 8934798d64ea95f0877f5997f7fb52f54db35bd3 Mon Sep 17 00:00:00 2001 From: Joshua Thayer Date: Tue, 18 Oct 2016 22:12:50 -0700 Subject: [PATCH 1/5] Initial 59 second bugfix --- src/clj_cron_parse/core.clj | 37 +++++++++++++++++++++---------- test/clj_cron_parse/core_test.clj | 4 +++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/clj_cron_parse/core.clj b/src/clj_cron_parse/core.clj index bf4fd82..1a973be 100644 --- a/src/clj_cron_parse/core.clj +++ b/src/clj_cron_parse/core.clj @@ -1,5 +1,7 @@ (ns clj-cron-parse.core - (:require [clojure.string :as s] + (:require + [clojure.tools.trace :as trace] + [clojure.string :as s] [clj-time.core :as t] [clojure.core.match :refer [match]] [clj-time.predicates :as pr])) @@ -191,13 +193,24 @@ (filter #(>= % now)) first)) -(defn now-with-seconds +(trace/deftrace next-date-time + [now as] + (->> as + (filter #(or (= % now) + (t/after? % now))) + first)) + +(trace/deftrace 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))))) - :else now)) + ([& xs] :seq) + (let [this-minute (apply t/date-time ((juxt t/year t/month t/day t/hour t/minute) now)) + ns (next-date-time (t/plus now (t/seconds 1)) + (map #(t/plus this-minute (t/seconds %)) xs))] + (if ns + ns + (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now)))))) + :else now)) (defn now-with-minutes [now minute] @@ -349,12 +362,12 @@ ([now cron] (if-let [{:keys [dom dow] :as cron-map} (make-cron-map cron)] (match [dom dow] - [:star :star] (next-date-by-dom now cron-map) - [_ :star] (next-date-by-dom now cron-map) - [:star _] (next-date-by-dow now cron-map) - :else (let [by-dom (next-date-by-dom now cron-map) - by-dow (next-date-by-dow now cron-map)] - (-> [by-dom by-dow] sort first))) + [:star :star] (next-date-by-dom now cron-map) + [_ :star] (next-date-by-dom now cron-map) + [:star _] (next-date-by-dow now cron-map) + :else (let [by-dom (next-date-by-dom now cron-map) + by-dow (next-date-by-dow now cron-map)] + (-> [by-dom by-dow] sort first))) nil)) ([now cron timezone] (if timezone diff --git a/test/clj_cron_parse/core_test.clj b/test/clj_cron_parse/core_test.clj index 76e9178..c135f9d 100644 --- a/test/clj_cron_parse/core_test.clj +++ b/test/clj_cron_parse/core_test.clj @@ -9,10 +9,12 @@ (= d actual)))) (def now (t/date-time 2015 01 01 12 00 00 000)) +(def now2 (t/date-time 2015 01 01 12 00 59 000)) (def nye (t/date-time 2014 12 31 12 00 00 000)) (time (facts "should find next date for cron expression" (next-date now "1 * * * * *") => (date 2015 01 01 12 00 01 000) + (next-date now2 "0 * * * * *") => (date 2015 01 01 12 01 00 000) (next-date now "* 1 * * * *") => (date 2015 01 01 12 01 00 000) (next-date now "* * 13 * * *") => (date 2015 01 01 13 00 00 000) (next-date now "* * * 10 * *") => (date 2015 01 10 12 00 00 000) @@ -126,4 +128,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)) From 0c5735d7591182e728a28496bb6ead114f502a70 Mon Sep 17 00:00:00 2001 From: Joshua Thayer Date: Tue, 18 Oct 2016 23:09:59 -0700 Subject: [PATCH 2/5] bumps clj-time, adds 59-second tests --- project.clj | 2 +- src/clj_cron_parse/core.clj | 38 +++++++++++++++---------------- test/clj_cron_parse/core_test.clj | 9 +++++--- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/project.clj b/project.clj index f828107..dffa825 100644 --- a/project.clj +++ b/project.clj @@ -4,7 +4,7 @@ :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 1a973be..62e52a5 100644 --- a/src/clj_cron_parse/core.clj +++ b/src/clj_cron_parse/core.clj @@ -1,7 +1,5 @@ (ns clj-cron-parse.core - (:require - [clojure.tools.trace :as trace] - [clojure.string :as s] + (:require [clojure.string :as s] [clj-time.core :as t] [clojure.core.match :refer [match]] [clj-time.predicates :as pr])) @@ -193,24 +191,24 @@ (filter #(>= % now)) first)) -(trace/deftrace next-date-time +(defn next-date-time [now as] (->> as - (filter #(or (= % now) + (filter #(or (t/equal? % now) (t/after? % now))) first)) -(trace/deftrace now-with-seconds +(defn now-with-seconds [now sec] (match sec - ([& xs] :seq) - (let [this-minute (apply t/date-time ((juxt t/year t/month t/day t/hour t/minute) now)) - ns (next-date-time (t/plus now (t/seconds 1)) - (map #(t/plus this-minute (t/seconds %)) xs))] - (if ns - ns - (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now)))))) - :else now)) + ([& xs] :seq) + (let [this-minute (t/to-time-zone (t/floor now t/minute) (.getZone now)) + ns (next-date-time (t/plus now (t/seconds 1)) + (map #(t/plus this-minute (t/seconds %)) xs))] + (if ns + ns + (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now)))))) + :else now)) (defn now-with-minutes [now minute] @@ -362,12 +360,12 @@ ([now cron] (if-let [{:keys [dom dow] :as cron-map} (make-cron-map cron)] (match [dom dow] - [:star :star] (next-date-by-dom now cron-map) - [_ :star] (next-date-by-dom now cron-map) - [:star _] (next-date-by-dow now cron-map) - :else (let [by-dom (next-date-by-dom now cron-map) - by-dow (next-date-by-dow now cron-map)] - (-> [by-dom by-dow] sort first))) + [:star :star] (next-date-by-dom now cron-map) + [_ :star] (next-date-by-dom now cron-map) + [:star _] (next-date-by-dow now cron-map) + :else (let [by-dom (next-date-by-dom now cron-map) + by-dow (next-date-by-dow now cron-map)] + (-> [by-dom by-dow] sort first))) nil)) ([now cron timezone] (if timezone diff --git a/test/clj_cron_parse/core_test.clj b/test/clj_cron_parse/core_test.clj index c135f9d..b26ee91 100644 --- a/test/clj_cron_parse/core_test.clj +++ b/test/clj_cron_parse/core_test.clj @@ -9,12 +9,10 @@ (= d actual)))) (def now (t/date-time 2015 01 01 12 00 00 000)) -(def now2 (t/date-time 2015 01 01 12 00 59 000)) (def nye (t/date-time 2014 12 31 12 00 00 000)) (time (facts "should find next date for cron expression" (next-date now "1 * * * * *") => (date 2015 01 01 12 00 01 000) - (next-date now2 "0 * * * * *") => (date 2015 01 01 12 01 00 000) (next-date now "* 1 * * * *") => (date 2015 01 01 12 01 00 000) (next-date now "* * 13 * * *") => (date 2015 01 01 13 00 00 000) (next-date now "* * * 10 * *") => (date 2015 01 10 12 00 00 000) @@ -78,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 From 66b405f109d56254d1d2afc57e55bfa025d7f69f Mon Sep 17 00:00:00 2001 From: Joshua Thayer Date: Tue, 18 Oct 2016 23:34:29 -0700 Subject: [PATCH 3/5] minor refactor --- src/clj_cron_parse/core.clj | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/clj_cron_parse/core.clj b/src/clj_cron_parse/core.clj index 62e52a5..7703a01 100644 --- a/src/clj_cron_parse/core.clj +++ b/src/clj_cron_parse/core.clj @@ -202,12 +202,10 @@ [now sec] (match sec ([& xs] :seq) - (let [this-minute (t/to-time-zone (t/floor now t/minute) (.getZone now)) - ns (next-date-time (t/plus now (t/seconds 1)) - (map #(t/plus this-minute (t/seconds %)) xs))] - (if ns - ns - (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now)))))) + (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 From 9e04007ff956ecf9d094dd4ea984522691bbabe7 Mon Sep 17 00:00:00 2001 From: Joshua Thayer Date: Tue, 18 Oct 2016 23:45:05 -0700 Subject: [PATCH 4/5] fixes formatting issue --- src/clj_cron_parse/core.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clj_cron_parse/core.clj b/src/clj_cron_parse/core.clj index 7703a01..ccebee8 100644 --- a/src/clj_cron_parse/core.clj +++ b/src/clj_cron_parse/core.clj @@ -204,7 +204,7 @@ ([& 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)) + (map #(t/plus this-minute (t/seconds %)) xs)) (t/plus now (t/minutes 1) (t/seconds (- (first xs) (t/second now)))))) :else now)) From 00f9c9c10d936a4d58b96e5e20928bbce3a8b178 Mon Sep 17 00:00:00 2001 From: Anna Taberski Date: Wed, 26 Oct 2016 15:02:51 -0400 Subject: [PATCH 5/5] Update project.clj with quartet project name and versioning --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index dffa825..7d804f8 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(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"