Skip to content

Commit c3b265e

Browse files
committed
some more bits..
1 parent 812e1eb commit c3b265e

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
(ns java-time.chrono
22
(:require [java-time.core :as jt.c])
3-
(:import [java.time.chrono ChronoPeriod ChronoLocalDate ChronoLocalDateTime ChronoZonedDateTime]
4-
[java.time.temporal TemporalAmount]))
3+
#?(:clj (:import [java.time.chrono ChronoPeriod ChronoLocalDate ChronoLocalDateTime ChronoZonedDateTime]
4+
[java.time.temporal TemporalAmount])))
5+
6+
7+
#?(:cljs
8+
(do
9+
(def ChronoPeriod (.. js/JSJoda -Period))
10+
(def ChronoLocalDate (.. js/JSJoda -ChronoLocalDate))
11+
(def ChronoLocalDateTime (.. js/JSJoda -ChronoLocalDateTime))
12+
(def ChronoZonedDateTime (.. js/JSJoda -ChronoZonedDateTime))
13+
))
514

615
(defn- ^ChronoPeriod cp-plus [^ChronoPeriod cp, ^TemporalAmount o]
716
(.plus cp o))

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
(ns java-time.clock
2-
(:require [java-time.core :as jt.c])
3-
(:import [java.time Clock Instant]))
2+
(:require [java-time.core :as jt.c]
3+
[java-time.util :as jt.u :include-macros true])
4+
#?(:clj (:import [java.time Clock Instant])))
45

56
(def ^:dynamic ^Clock *clock* nil)
67

7-
(defn make [f]
8+
9+
#?(:cljs
10+
(do
11+
(def Clock (.. js/JSJoda -Clock))
12+
(def Instant (.. js/JSJoda -Instant))))
13+
14+
(defn make [f]
815
(if *clock*
916
(f *clock*)
10-
(f (Clock/systemDefaultZone))))
17+
(f (jt.u/static-call Clock 'systemDefaultZone))))
1118

1219
(defn with-clock-fn
1320
"Executes the given function in the scope of the provided clock. All the

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

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
(ns java-time.properties
22
(:require [java-time.core :as jt.c]
33
[java-time.util :as jt.u])
4-
(:import [java.time.temporal
5-
TemporalField IsoFields ChronoField JulianFields
6-
TemporalUnit ChronoUnit]))
4+
#?(:clj (:import [java.time.temporal
5+
TemporalField IsoFields ChronoField JulianFields
6+
TemporalUnit ChronoUnit])))
7+
8+
#?(:cljs
9+
(do
10+
(def TemporalField (.. js/JSJoda -TemporalField))
11+
(def IsoFields (.. js/JSJoda -IsoFields))
12+
(def ChronoField (.. js/JSJoda -ChronoField))
13+
(def TemporalUnit (.. js/JSJoda -TemporalUnit))
14+
(def ChronoUnit (.. js/JSJoda -ChronoUnit))
15+
;(def JulianFields (.. js/JSJoda -JulianFields))
16+
))
717

818
(defn- property->key [p]
919
(keyword (jt.u/dashize (str p))))
@@ -31,10 +41,14 @@
3141
;;;;;;;;; UNIT
3242

3343
(def iso-units
34-
(vals (jt.u/get-static-fields-of-type IsoFields TemporalUnit)))
44+
(vals
45+
#?(:clj (jt.u/get-static-fields-of-type IsoFields TemporalUnit)
46+
:cljs (jt.u/get-fields-matching IsoFields #".*YEARS")))) ;todo
3547

3648
(def chrono-units
37-
(vals (jt.u/get-static-fields-of-type ChronoUnit TemporalUnit)))
49+
(vals
50+
#?(:clj (jt.u/get-static-fields-of-type ChronoUnit TemporalUnit)
51+
:cljs (jt.u/get-fields-matching ChronoUnit #".*"))))
3852

3953
(def predefined-units
4054
(concat iso-units chrono-units))
@@ -68,7 +82,8 @@
6882
(defn ^TemporalUnit get-unit-checked [o]
6983
(if-let [u (get-unit o)]
7084
u
71-
(throw (NullPointerException. (str "No temporal unit found for " o "!")))))
85+
(throw (#?(:clj NullPointerException.
86+
:cljs js/Error.) (str "No temporal unit found for " o "!")))))
7287

7388
(defn unit-key [o]
7489
(cond (keyword? o)
@@ -80,13 +95,19 @@
8095
;;;;;;;;; FIELD
8196

8297
(def iso-fields
83-
(vals (jt.u/get-static-fields-of-type IsoFields TemporalField)))
98+
(vals
99+
#?(:clj (jt.u/get-static-fields-of-type IsoFields TemporalField)
100+
:cljs (jt.u/get-fields-matching IsoFields #".*"))))
84101

85102
(def julian-fields
86-
(vals (jt.u/get-static-fields-of-type JulianFields TemporalField)))
103+
(vals
104+
#?(:clj (jt.u/get-static-fields-of-type JulianFields TemporalField)
105+
:cljs {})))
87106

88107
(def chrono-fields
89-
(vals (jt.u/get-static-fields-of-type ChronoField TemporalField)))
108+
(vals
109+
#?(:clj (jt.u/get-static-fields-of-type ChronoField TemporalField)
110+
:cljs (jt.u/get-fields-matching ChronoField #".*"))))
90111

91112
(def predefined-fields
92113
(concat iso-fields chrono-fields julian-fields))
@@ -106,10 +127,18 @@
106127
(supports? [f t]
107128
(.isSupportedBy f ^TemporalAccessor t)))
108129

109-
(extend TemporalField
130+
(extend-type TemporalField
110131
jt.c/ReadableRangeProperty
111-
(assoc jt.c/readable-range-property-fns
112-
:range (fn [^TemporalField f] (.range f))))
132+
(range [^TemporalField k]
133+
(.range k))
134+
(min-value [p]
135+
((jt.c/readable-range-property-fns :min-value) p))
136+
(largest-min-value [p]
137+
((jt.c/readable-range-property-fns :largest-min-value) p))
138+
(smallest-max-value [p]
139+
((jt.c/readable-range-property-fns :smallest-max-value) p))
140+
(max-value [p]
141+
((jt.c/readable-range-property-fns :max-value) p)))
113142

114143
(defn field?
115144
"True if this is a `TemporalField`."
@@ -160,7 +189,15 @@
160189
(supports? [k t]
161190
(jt.c/supports? (or (get-field k) (get-unit k)) t)))
162191

163-
(extend clojure.lang.Keyword
192+
(extend-type clojure.lang.Keyword
164193
jt.c/ReadableRangeProperty
165-
(assoc jt.c/readable-range-property-fns
166-
:range (fn [k] (jt.c/range (get-field k)))))
194+
(range [k]
195+
(jt.c/range (get-field k)))
196+
(min-value [p]
197+
((jt.c/readable-range-property-fns :min-value) p))
198+
(largest-min-value [p]
199+
((jt.c/readable-range-property-fns :largest-min-value) p))
200+
(smallest-max-value [p]
201+
((jt.c/readable-range-property-fns :smallest-max-value) p))
202+
(max-value [p]
203+
((jt.c/readable-range-property-fns :max-value) p)))

0 commit comments

Comments
 (0)