Skip to content

Commit

Permalink
Merge pull request #93 from henryw374/master
Browse files Browse the repository at this point in the history
addressing issue #92
  • Loading branch information
andrewmcveigh committed Mar 30, 2017
2 parents cb6f963 + bd1d8cb commit 4fb58f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
16 changes: 13 additions & 3 deletions src/cljs_time/core.cljs
Expand Up @@ -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))
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 3 additions & 9 deletions src/cljs_time/extend.cljs
Expand Up @@ -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)
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions test/cljs_time/core_test.cljs
Expand Up @@ -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)
Expand Down

0 comments on commit 4fb58f5

Please sign in to comment.