diff --git a/src/cljs_time/core.cljs b/src/cljs_time/core.cljs index 3b61d28..ddc9d13 100644 --- a/src/cljs_time/core.cljs +++ b/src/cljs_time/core.cljs @@ -177,6 +177,16 @@ expected."} (when-let [f (period-fns k)] (f date' operator v)))) date'))) +(defn- compare-local-dates [o other] + (let [yo (.getYear o) + yother (.getYear other) + dayo (.getDayOfYear o) + dayother (.getDayOfYear other)] + (cond + (not= yo yother) (- yo yother) + (not= dayo dayother) (- dayo dayother) + :else 0))) + (extend-protocol DateTimeProtocol goog.date.UtcDateTime (year [this] (.getYear this)) @@ -235,9 +245,9 @@ expected."} (minute [this] nil) (second [this] nil) (milli [this] nil) - (equal? [this that] (== (.getTime this) (.getTime that))) - (after? [this that] (> (.getTime this) (.getTime that))) - (before? [this that] (< (.getTime this) (.getTime that))) + (equal? [this that] (.equals this that)) + (after? [this that] (pos? (compare-local-dates this that))) + (before? [this that] (neg? (compare-local-dates this that))) (plus- [this period] ((period-fn period) + this)) (minus- [this period] ((period-fn period) - this)) (first-day-of-the-month- [this] diff --git a/src/cljs_time/extend.cljs b/src/cljs_time/extend.cljs index 82fad4f..df3b377 100644 --- a/src/cljs_time/extend.cljs +++ b/src/cljs_time/extend.cljs @@ -11,7 +11,8 @@ (:import [goog.date Date] [goog.date DateTime] - [goog.date UtcDateTime])) + [goog.date UtcDateTime]) + (:require [cljs-time.core :as time])) (defn hash-parts [type-hash ms-since-epoch offset] (let [ms-shifted (* ms-since-epoch 100000) @@ -29,14 +30,7 @@ (hash-parts 1 (.getTime this) (.getTimezoneOffset this))) IComparable (-compare [o other] - (let [yo (.getYear o) - yother (.getYear other) - dayo (.getDayOfYear o) - dayother (.getDayOfYear other)] - (cond - (not= yo yother) (- yo yother) - (not= dayo dayother) (- dayo dayother) - :else 0)))) + (time/compare-local-dates o other))) (extend-type goog.date.DateTime IEquiv diff --git a/test/cljs_time/core_test.cljs b/test/cljs_time/core_test.cljs index e10379c..1a3e9b9 100644 --- a/test/cljs_time/core_test.cljs +++ b/test/cljs_time/core_test.cljs @@ -162,21 +162,28 @@ (let [d (date-time 1918 11 11)] (is (= 1 (day-of-week d))))) +(def local-date-x (local-date 2017 3 29)) +(def local-date-x-with-later-time (doto (goog.date.Date.) + (.setTime (+ 1 (.getTime local-date-x))))) + (deftest test-equal? (is (equal? (date-time 2013 01 01 01) (date-time 2013 01 01 01))) (is (equal? (date-time 1987) (date-time 1987))) (is (not (equal? (date-time 1986) (date-time 1987)))) - (is (not (equal? (date-time 1987) (date-time 1986))))) + (is (not (equal? (date-time 1987) (date-time 1986)))) + (is (equal? local-date-x local-date-x-with-later-time))) (deftest test-after? (is (after? (date-time 1987) (date-time 1986))) (is (not (after? (date-time 1986) (date-time 1987)))) - (is (not (after? (date-time 1986) (date-time 1986))))) + (is (not (after? (date-time 1986) (date-time 1986)))) + (is (not (after? local-date-x-with-later-time local-date-x)))) (deftest test-before? (is (before? (date-time 1986) (date-time 1987))) (is (not (before? (date-time 1987) (date-time 1986)))) - (is (not (before? (date-time 1986) (date-time 1986))))) + (is (not (before? (date-time 1986) (date-time 1986)))) + (is (not (before? local-date-x local-date-x-with-later-time)))) (deftest test-periods (is (= (date-time 1986 10 14 4 3 2 1)