Skip to content

Commit f1ae79e

Browse files
committed
hwm
1 parent c3b265e commit f1ae79e

File tree

2 files changed

+127
-74
lines changed

2 files changed

+127
-74
lines changed

src/java_time/temporal.clj renamed to src/java_time/temporal.cljc

Lines changed: 55 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,57 @@
55
[java-time.properties :as jt.p]
66
[java-time.format :as jt.f]
77
[java-time.clock :as jt.clock]
8-
[java-time.defconversion :refer (deffactory conversion!)])
9-
(:import [java.time.temporal Temporal TemporalAccessor ValueRange
10-
TemporalField TemporalUnit TemporalAmount ChronoField IsoFields]
11-
[java.time.format DateTimeFormatter]
12-
[java.time.chrono Chronology]
13-
[java.time DateTimeException Clock
14-
Period Duration MonthDay DayOfWeek Month Year
15-
ZoneOffset Instant]))
16-
17-
(def writable-range-property-fns
18-
{:with-min-value (fn [p] (jt.c/with-value p (jt.c/min-value p)))
19-
:with-largest-min-value (fn [p] (jt.c/with-value p (jt.c/largest-min-value p)))
20-
:with-smallest-max-value (fn [p] (jt.c/with-value p (jt.c/smallest-max-value p)))
21-
:with-max-value (fn [p] (jt.c/with-value p (jt.c/max-value p)))})
22-
23-
(defmacro value-property [java-type range-field &
24-
{:keys [with-value-fn-sym get-value-fn-sym]
25-
:or {with-value-fn-sym 'of
26-
get-value-fn-sym 'getValue}}]
27-
(let [java-type-arg (with-meta (gensym) {:tag java-type})]
28-
`(do
29-
(extend-type ~java-type
30-
jt.c/ReadableProperty
31-
(value [d#]
32-
(. d# ~get-value-fn-sym))
33-
34-
jt.c/WritableProperty
35-
(with-value [_# v#]
36-
(. ~java-type ~with-value-fn-sym v#)))
37-
38-
(extend ~java-type
39-
jt.c/ReadableRangeProperty
40-
(assoc jt.c/readable-range-property-fns
41-
:range (fn [~java-type-arg]
42-
(.range ~java-type-arg ~range-field)))
43-
44-
jt.c/WritableRangeProperty
45-
writable-range-property-fns))))
46-
47-
(value-property DayOfWeek ChronoField/DAY_OF_WEEK)
48-
(value-property Month ChronoField/MONTH_OF_YEAR)
49-
(value-property Year ChronoField/YEAR_OF_ERA)
50-
(value-property ZoneOffset ChronoField/OFFSET_SECONDS
51-
:with-value-fn-sym ofTotalSeconds
52-
:get-value-fn-sym getTotalSeconds)
53-
54-
(jt.u/when-threeten-extra
55-
(import [org.threeten.extra AmPm DayOfMonth DayOfYear Quarter YearQuarter])
56-
(value-property DayOfMonth ChronoField/DAY_OF_MONTH)
57-
(value-property DayOfYear ChronoField/DAY_OF_YEAR))
8+
[java-time.temporal-macros :as jt.tm :include-macros true]
9+
#?(:clj
10+
[java-time.defconversion :refer (deffactory conversion!)]))
11+
#?(:clj
12+
(:import [java.time.temporal Temporal TemporalAccessor ValueRange
13+
TemporalField TemporalUnit TemporalAmount ChronoField IsoFields]
14+
[java.time.format DateTimeFormatter]
15+
[java.time.chrono Chronology]
16+
[java.time DateTimeException Clock
17+
Period Duration MonthDay DayOfWeek Month Year
18+
ZoneOffset Instant])))
19+
20+
21+
#?(:cljs
22+
(do
23+
(def Temporal (.. js/JSJoda -Temporal))
24+
(def TemporalAccessor (.. js/JSJoda -TemporalAccessor))
25+
(def ValueRange (.. js/JSJoda -ValueRange))
26+
(def TemporalField (.. js/JSJoda -TemporalField))
27+
(def TemporalUnit (.. js/JSJoda -TemporalUnit))
28+
(def TemporalAmount (.. js/JSJoda -TemporalAmount))
29+
(def ChronoField (.. js/JSJoda -ChronoField))
30+
(def IsoFields (.. js/JSJoda -IsoFields))
31+
(def DateTimeFormatter (.. js/JSJoda -DateTimeFormatter))
32+
;(def Chronology (.. js/JSJoda -Chronology))
33+
(def DateTimeException (.. js/JSJoda -DateTimeException))
34+
(def Clock (.. js/JSJoda -Clock))
35+
(def Period (.. js/JSJoda -Period))
36+
(def Duration (.. js/JSJoda -Duration))
37+
(def MonthDay (.. js/JSJoda -MonthDay))
38+
(def DayOfWeek (.. js/JSJoda -DayOfWeek))
39+
(def Month (.. js/JSJoda -Month))
40+
(def Year (.. js/JSJoda -Year))
41+
(def ZoneOffset (.. js/JSJoda -ZoneOffset))
42+
(def Instant (.. js/JSJoda -Instant))
43+
44+
(def ResolverStyle (.. js/JSJoda -ResolverStyle))))
45+
46+
47+
(jt.tm/value-property DayOfWeek (jt.u/static-prop ChronoField 'DAY_OF_WEEK))
48+
(jt.tm/value-property Month (jt.u/static-prop ChronoField 'MONTH_OF_YEAR))
49+
(jt.tm/value-property Year (jt.u/static-prop ChronoField 'YEAR_OF_ERA))
50+
(jt.tm/value-property ZoneOffset (jt.u/static-prop ChronoField 'OFFSET_SECONDS)
51+
:with-value-fn-sym ofTotalSeconds
52+
:get-value-fn-sym getTotalSeconds)
53+
54+
#?(:clj
55+
(jt.u/when-threeten-extra
56+
(import [org.threeten.extra AmPm DayOfMonth DayOfYear Quarter YearQuarter])
57+
(jt.tm/value-property DayOfMonth ChronoField/DAY_OF_MONTH)
58+
(jt.tm/value-property DayOfYear ChronoField/DAY_OF_YEAR)))
5859

5960
;;;;; FIELD PROPERTY
6061

@@ -113,26 +114,6 @@
113114
(alter-meta! #'->TemporalFieldProperty assoc :private true)
114115
(alter-meta! #'map->TemporalFieldProperty assoc :private true)
115116

116-
(defmacro field-property [java-type has-range?]
117-
(let [java-type-arg (with-meta (gensym) {:tag java-type})]
118-
`(do
119-
(extend ~java-type
120-
jt.c/ReadableProperty
121-
{:value (fn [~java-type-arg]
122-
(get-long-property-value (.o ~java-type-arg)
123-
(.field ~java-type-arg)))})
124-
125-
~(when has-range?
126-
`(extend ~java-type
127-
jt.c/ReadableRangeProperty
128-
(assoc jt.c/readable-range-property-fns
129-
:range (fn [~java-type-arg]
130-
(get-field-property-range (.o ~java-type-arg)
131-
(.field ~java-type-arg))))
132-
133-
jt.c/WritableRangeProperty
134-
writable-range-property-fns)))))
135-
136117
(field-property DayOfWeekFieldProperty true)
137118
(field-property MonthFieldProperty true)
138119
(field-property MonthDayFieldProperty true)
@@ -176,9 +157,9 @@
176157
res (transient {})]
177158
(if f
178159
(recur (first r) (rest r)
179-
(if (jt.c/supports? o f)
180-
(assoc! res k f)
181-
res))
160+
(if (jt.c/supports? o f)
161+
(assoc! res k f)
162+
res))
182163
(persistent! res)))))
183164

184165
jt.c/HasProperties
@@ -273,7 +254,7 @@
273254
(loop [u u, us us, res (transient {})]
274255
(if u
275256
(recur (first us) (rest us)
276-
(assoc! res (jt.p/unit-key u) u))
257+
(assoc! res (jt.p/unit-key u) u))
277258
(persistent! res)))))
278259

279260
jt.c/HasProperties

src/java_time/temporal_macros.cljc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
(ns java-time.temporal-macros
2+
(:require [java-time.core :as jt.c])
3+
#?(:clj
4+
(:import [java.time.temporal Temporal TemporalAccessor ValueRange
5+
TemporalField TemporalUnit TemporalAmount ChronoField IsoFields]
6+
[java.time.format DateTimeFormatter]
7+
[java.time.chrono Chronology]
8+
[java.time DateTimeException Clock
9+
Period Duration MonthDay DayOfWeek Month Year
10+
ZoneOffset Instant])))
11+
12+
(def writable-range-property-fns
13+
{:with-min-value (fn [p] (jt.c/with-value p (jt.c/min-value p)))
14+
:with-largest-min-value (fn [p] (jt.c/with-value p (jt.c/largest-min-value p)))
15+
:with-smallest-max-value (fn [p] (jt.c/with-value p (jt.c/smallest-max-value p)))
16+
:with-max-value (fn [p] (jt.c/with-value p (jt.c/max-value p)))})
17+
18+
(defmacro value-property [java-type range-field &
19+
{:keys [with-value-fn-sym get-value-fn-sym]
20+
:or {with-value-fn-sym 'of
21+
get-value-fn-sym 'getValue}}]
22+
(let [java-type-arg (with-meta (gensym) {:tag java-type})]
23+
`(do
24+
(extend-type ~java-type
25+
jt.c/ReadableProperty
26+
(jt.c/value [d#]
27+
(. d# ~get-value-fn-sym))
28+
29+
jt.c/WritableProperty
30+
(jt.c/with-value [_# v#]
31+
(. ~java-type ~with-value-fn-sym v#)))
32+
33+
(extend-type ~java-type
34+
jt.c/ReadableRangeProperty
35+
(jt.c/min-value [~'p] (.getMinimum ^ValueRange (range ~'p)))
36+
(jt.c/largest-min-value [~'p] (.getLargestMinimum ^ValueRange (range ~'p)))
37+
(jt.c/smallest-max-value [~'p] (.getSmallestMaximum ^ValueRange (range ~'p)))
38+
(jt.c/max-value [~'p] (.getMaximum ^ValueRange (range ~'p)))
39+
(jt.c/range [~java-type-arg]
40+
(.range ~java-type-arg ~range-field))
41+
42+
jt.c/WritableRangeProperty
43+
(jt.c/with-min-value [~'p] (jt.c/with-value ~'p (jt.c/min-value ~'p)))
44+
(jt.c/with-largest-min-value [~'p] (jt.c/with-value ~'p (jt.c/largest-min-value ~'p)))
45+
(jt.c/with-smallest-max-value [~'p] (jt.c/with-value ~'p (jt.c/smallest-max-value ~'p)))
46+
(jt.c/with-max-value [~'p] (jt.c/with-value ~'p (jt.c/max-value ~'p)))))))
47+
48+
49+
50+
(defmacro field-property [java-type has-range?]
51+
(let [java-type-arg (with-meta (gensym) {:tag java-type})]
52+
`(do
53+
(extend-type ~java-type
54+
jt.c/ReadableProperty
55+
(value [~java-type-arg]
56+
(get-long-property-value (.o ~java-type-arg)
57+
(.field ~java-type-arg))))
58+
59+
~(when has-range?
60+
`(extend-type ~java-type
61+
jt.c/ReadableRangeProperty
62+
(jt.c/min-value [~'p] (.getMinimum ^ValueRange (range ~'p)))
63+
(jt.c/largest-min-value [~'p] (.getLargestMinimum ^ValueRange (range ~'p)))
64+
(jt.c/smallest-max-value [~'p] (.getSmallestMaximum ^ValueRange (range ~'p)))
65+
(jt.c/max-value [~'p] (.getMaximum ^ValueRange (range ~'p)))
66+
(jt.c/range [~java-type-arg]
67+
(get-field-property-range (.o ~java-type-arg)
68+
(.field ~java-type-arg)))
69+
70+
71+
jt.c/WritableRangeProperty
72+
writable-range-property-fns)))))

0 commit comments

Comments
 (0)