This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
units.cljc
124 lines (90 loc) · 2.84 KB
/
units.cljc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(ns onyx.windowing.units)
(defmulti standard-units-for
(fn [unit] unit))
(defmethod standard-units-for :milliseconds
[unit] :milliseconds)
(defmethod standard-units-for :seconds
[unit] :milliseconds)
(defmethod standard-units-for :minutes
[unit] :milliseconds)
(defmethod standard-units-for :hours
[unit] :milliseconds)
(defmethod standard-units-for :days
[unit] :milliseconds)
(defmethod standard-units-for :elements
[unit] :elements)
(defmulti to-standard-units
(fn [v unit] unit))
(defmethod to-standard-units :milliseconds
[v unit] v)
(defmethod to-standard-units :seconds
[v unit]
(to-standard-units (* 1000 v) :milliseconds))
(defmethod to-standard-units :minutes
[v unit]
(to-standard-units (* 60 v) :seconds))
(defmethod to-standard-units :hours
[v unit]
(to-standard-units (* 60 v) :minutes))
(defmethod to-standard-units :days
[v unit]
(to-standard-units (* 24 v) :hours))
(defmethod to-standard-units :elements
[v unit] v)
;;; Pluralized versions for readability
(defmethod standard-units-for :millisecond
[unit] (standard-units-for :milliseconds))
(defmethod standard-units-for :second
[unit] (standard-units-for :seconds))
(defmethod standard-units-for :minute
[unit] (standard-units-for :minutes))
(defmethod standard-units-for :hour
[unit] (standard-units-for :hours))
(defmethod standard-units-for :day
[unit] (standard-units-for :days))
(defmethod standard-units-for :element
[unit] (standard-units-for :elements))
(defmethod to-standard-units :millisecond
[v unit] (to-standard-units v :milliseconds))
(defmethod to-standard-units :second
[v unit] (to-standard-units v :seconds))
(defmethod to-standard-units :minute
[v unit] (to-standard-units v :minutes))
(defmethod to-standard-units :hour
[v unit] (to-standard-units v :hours))
(defmethod to-standard-units :day
[v unit] (to-standard-units v :days))
(defmethod to-standard-units :element
[v unit] (to-standard-units v :elements))
(defprotocol ICoerceKey
(coerce-key [_ units]))
#?(:clj
(extend-type java.sql.Timestamp
ICoerceKey
(coerce-key [this units]
(to-standard-units (.getTime this) units))))
#?(:clj
(extend-type java.util.Date
ICoerceKey
(coerce-key [this units]
(to-standard-units (.getTime this) units))))
#?(:cljs
(extend-type js/Date
ICoerceKey
(coerce-key [this units]
(to-standard-units (.getTime this) units))))
#?(:clj
(extend-type java.lang.Long
ICoerceKey
(coerce-key [this units]
(to-standard-units this units))))
#?(:clj
(extend-type java.lang.Integer
ICoerceKey
(coerce-key [this units]
(to-standard-units this units))))
#?(:cljs
(extend-type number
ICoerceKey
(coerce-key [this units]
(to-standard-units this units))))