forked from dm3/clojure.java-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties.clj
More file actions
161 lines (120 loc) · 4.04 KB
/
properties.clj
File metadata and controls
161 lines (120 loc) · 4.04 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(ns java-time.properties
(:require [java-time.core :as jt.c]
[java-time.util :as jt.u]
[java-time.fields :as jt.f]
[java-time.units :as jt.units])
(:import [java.time.temporal TemporalField TemporalUnit TemporalAccessor Temporal]
(clojure.lang Keyword)))
(defn- property->key [p]
(keyword (jt.u/dashize (str p))))
(defn- ->map [xs]
(zipmap (->> xs (map property->key))
xs))
;;;;;;;;; Field/Unit groups
(deftype FieldGroup [group-id field-map]
jt.c/HasFields
(fields [_] field-map)
(field* [_ k] (k field-map))
Object
(toString [_] group-id))
(deftype UnitGroup [group-id unit-map]
jt.c/HasUnits
(units [_] unit-map)
(unit* [_ k] (k unit-map))
Object
(toString [_] group-id))
;;;;;;;;; UNIT
(defonce iso-units
(vals jt.units/iso))
(defonce chrono-units
(vals jt.units/chrono))
(def predefined-units
(concat iso-units chrono-units))
(def unit-groups
{:predefined (->map predefined-units)
:iso (->map iso-units)
:chrono (->map chrono-units)})
(def ^:dynamic *units* (UnitGroup. :predefined (:predefined unit-groups)))
(extend-type TemporalUnit
jt.c/KnowsTimeBetween
(time-between [u t1 t2]
(.between u ^Temporal t1, ^Temporal t2))
jt.c/Supporting
(supports? [u t]
(.isSupportedBy u ^Temporal t)))
(defn unit?
"True if this is a `TemporalUnit`."
[o] (instance? TemporalUnit o))
(defn ^TemporalUnit get-unit [o]
(cond (unit? o) o
(keyword? o)
(jt.c/unit* *units* o)))
(defn ^TemporalUnit get-unit-checked [o]
(or (get-unit o)
(throw (NullPointerException. (str "No temporal unit found for " o "!")))))
(defn unit-key [o]
(cond (keyword? o)
o
(unit? o)
(property->key o)))
;;;;;;;;; FIELD
(defonce iso-fields (vals jt.f/iso))
(defonce julian-fields (vals jt.f/julian))
(defonce chrono-fields (vals jt.f/chrono))
(defonce predefined-fields
(concat iso-fields chrono-fields julian-fields))
;; There is another implementation of fields - WeekFields, which is dynamic
(def field-groups
{:predefined (->map predefined-fields)
:iso (->map iso-fields)
:julian (->map julian-fields)
:chrono (->map chrono-fields)})
(def ^:dynamic *fields* (FieldGroup. :predefined (:predefined field-groups)))
(extend-type TemporalField
jt.c/Supporting
(supports? [f t]
(.isSupportedBy f ^TemporalAccessor t)))
(extend TemporalField
jt.c/ReadableRangeProperty
(assoc jt.c/readable-range-property-fns
:range (fn [^TemporalField f] (.range f))))
(defn field?
"True if this is a `TemporalField`."
[o] (instance? TemporalField o))
(defn ^TemporalField get-field [o]
(cond (field? o)
o
(keyword? o)
(jt.c/field* *fields* o)))
(defn field-key [o]
(cond (keyword? o)
o
(field? o)
(property->key o)))
(defn ^java.time.temporal.TemporalUnit unit
"Returns a `TemporalUnit` for the given key `k` or extracts the field from
the given temporal `entity`.
You can see predefined units via [[java-time.repl/show-units]].
If you want to make your own custom TemporalUnits resolvable, you need to rebind the
`java-time.properties/*units*` to a custom `java_time.properties.UnitGroup`."
([k] (get-unit k))
([entity k] (jt.c/unit* entity k)))
(defn ^java.time.temporal.TemporalUnit field
"Returns a `TemporalField` for the given key `k` or extracts the field from
the given temporal `entity`.
You can see predefined fields via [[java-time.repl/show-fields]].
If you want to make your own custom TemporalFields resolvable, you need to rebind the
`java-time.properties/*fields*` to a custom `java_time.properties.FieldGroup`."
([k] (get-field k))
([entity k] (jt.c/field* entity k)))
(extend-type Keyword
jt.c/KnowsTimeBetween
(time-between [k t1 t2]
(jt.c/time-between (or (get-field k) (get-unit k)) t1 t2))
jt.c/Supporting
(supports? [k t]
(jt.c/supports? (or (get-field k) (get-unit k)) t)))
(extend Keyword
jt.c/ReadableRangeProperty
(assoc jt.c/readable-range-property-fns
:range (fn [k] (jt.c/range (get-field k)))))