/
extend.cljs
59 lines (53 loc) · 1.71 KB
/
extend.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(ns cljs-time.extend
"### Optional namespace to extend goog.date.* DateTime types
Equality of goog.date.* DateTime types works differently to
clj-time/Joda's equality. Optionally require this namespace
to extend cljs.core/IEquiv protocol for:
* goog.date.Date
* goog.date.DateTime
* goog.date.UtcDateTime"
(:import
[goog.date Date]
[goog.date DateTime]
[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)
adj-offset (+ offset 720)
type-offset (* type-hash 10000)]
(+ ms-shifted adj-offset type-offset)))
(extend-type goog.date.Date
IEquiv
(-equiv [o other]
(and (instance? goog.date.Date other)
(.equals o other)))
IHash
(-hash [this]
(hash-parts 1 (.getTime this) (.getTimezoneOffset this)))
IComparable
(-compare [o other]
(time/compare-local-dates o other)))
(extend-type goog.date.DateTime
IEquiv
(-equiv [o other]
(and (instance? goog.date.DateTime other)
(identical? (.getTime o) (.getTime other))
(identical? (.getTimezoneOffset o) (.getTimezoneOffset other))))
IHash
(-hash [this]
(hash-parts 2 (.getTime this) (.getTimezoneOffset this)))
IComparable
(-compare [o other]
(- (.getTime o) (.getTime other))))
(extend-type goog.date.UtcDateTime
IEquiv
(-equiv [o other]
(and (instance? goog.date.UtcDateTime other)
(identical? (.getTime o) (.getTime other))
(identical? (.getTimezoneOffset o) (.getTimezoneOffset other))))
IHash
(-hash [this]
(hash-parts 3 (.getTime this) (.getTimezoneOffset this)))
IComparable
(-compare [o other]
(- (.getTime o) (.getTime other))))