Skip to content

Commit 673d6f3

Browse files
author
Vadim Platonov
committed
make threeten-extra dependency optional
1 parent 87e038b commit 673d6f3

File tree

4 files changed

+130
-72
lines changed

4 files changed

+130
-72
lines changed

README.md

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ Three next days?
144144
#object[java.time.LocalDate "2015-09-30"])
145145
```
146146

147-
When is the next working day?
147+
When is the first Monday in month?
148148

149149
```clj
150-
(adjust now :next-working-day)
151-
=> #object[java.time.LocalDate "2015-09-28"]
150+
(adjust now :first-in-month :monday)
151+
=> #object[java.time.LocalDate "2015-09-07"]
152152
```
153153

154154
Date with some of its fields truncated:
@@ -263,8 +263,33 @@ Any date which can be converted to an instant, can also be converted to pre-Java
263263
=> #inst "2015-09-27T22:00:00.000000000-00:00"
264264
```
265265

266-
Bonus! if you have Joda Time on the classpath, you can seamlessly convert from
267-
Joda Time to Java Time types:
266+
#### Three-Ten Extra
267+
268+
If you add an optional `[org.threeten/threeten-extra "0.9"]` dependency to the
269+
project, you will get an `Interval`, `AmPm`, `DayOfMonth`, `DayOfYear`,
270+
`Quarter` and `YearQuarter` data types as well as a couple more adjusters.
271+
272+
An interval can be constructed from two entities that can be converted to
273+
instants:
274+
275+
```clj
276+
(interval (offset-date-time 2015 1 1 +0) (zoned-date-time 2016 1 1 "UTC"))
277+
=> #<org.threeten.extra.Interval 2015-01-01T00:00:00Z/2016-01-01T00:00:00Z>
278+
279+
(move-start-by *1 (duration 5 :days))
280+
=> #<org.threeten.extra.Interval 2015-01-06T00:00:00Z/2016-01-01T00:00:00Z>
281+
282+
(move-end-by *1 (duration 5 :days))
283+
=> #<org.threeten.extra.Interval 2015-01-06T00:00:00Z/2016-01-06T00:00:00Z>
284+
285+
(contains? *1 (offset-date-time 2015 1 1 +0))
286+
=> false
287+
```
288+
289+
#### Joda-Time
290+
291+
Bonus! if you have Joda Time on the classpath (either directly, or via
292+
`clj-time`), you can seamlessly convert from Joda Time to Java Time types:
268293

269294
```clj
270295
(java-time.repl/show-path org.joda.time.DateTime java.time.OffsetTime)
@@ -403,3 +428,24 @@ of the identical conversion logic. However, the flexibility comes with a cost:
403428
both on the performance and the conversion path chosen for certain arguments.
404429

405430
Hopefully, the performance issue will be resolved in the future...
431+
432+
You can play with the conversion graph using the following helpers:
433+
434+
```clj
435+
(java-time.repl/show-path org.joda.time.DateTime java.time.OffsetTime)
436+
=> {:cost 2.0,
437+
:path [[#<java_time.graph.Types@15e43c24 [org.joda.time.DateTime]>
438+
#<java_time.graph.Types@78a2235c [java.time.Instant java.time.ZoneId]>]
439+
[#<java_time.graph.Types@6d8ded1a [java.time.Instant java.time.ZoneId]>
440+
#<java_time.graph.Types@5360f6ae [java.time.OffsetTime]>]]}
441+
442+
(java-time.repl/show-graph)
443+
=> {1
444+
{org.threeten.extra.DayOfYear
445+
[[#object[java_time.graph.Types "[java.lang.Number]"]
446+
#object[java_time.graph.Conversion "Cost:1.0"]]],
447+
java.lang.Number
448+
[[#object[java_time.graph.Types "[java.time.Instant]"]
449+
#object[java_time.graph.Conversion "Cost:1.0"]]
450+
...
451+
```

dependency-test/project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(defproject test/no-deps "0.1.0-SNAPSHOT"
22
:description "Clojure.Java-Time with no external dependencies"
3-
:dependencies [[clojure.java-time "0.1.0-SNAPSHOT" :exclusions [org.threeten/threeten-extra]]
3+
:dependencies [[clojure.java-time "0.1.0-SNAPSHOT"]
44
[org.clojure/clojure "1.7.0"]])

project.clj

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55
:url "http://opensource.org/licenses/MIT"}
66
:scm {:name "git"
77
:url "http://github.com/dm3/clojure.java-time"}
8-
:dependencies [[org.threeten/threeten-extra "0.9"]
9-
[clj-tuple "0.2.2"]]
8+
:dependencies [[clj-tuple "0.2.2"]]
109
:profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0"]
11-
[org.clojure/test.check "0.5.8"]
1210
[criterium "0.4.2"]
1311
[com.taoensso/timbre "4.1.4"]
1412
[org.clojure/tools.namespace "0.2.11"]
15-
[joda-time/joda-time "2.9"]]
13+
[joda-time/joda-time "2.9"]
14+
[org.threeten/threeten-extra "0.9"]]
1615
:plugins [[codox "0.8.13"]]
1716
:codox {:include [java-time]}
1817
:source-paths ["dev"]
1918
:global-vars {*warn-on-reflection* true}}
20-
:1.8 {:dependencies [[org.clojure/clojure "1.8.0-RC1"]]}}
21-
:aliases {"test-all" ["with-profile" "dev,default:dev,1.6,default:dev,1.8,default" "test"]}
19+
:1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}
20+
:1.7-three-ten-joda {:dependencies [[org.clojure/clojure "1.7.0"]
21+
[org.threeten/threeten-extra "0.9"]
22+
[joda-time/joda-time "2.9"]]}
23+
:1.8 {:dependencies [[org.clojure/clojure "1.8.0-RC3"]]}
24+
:1.8-three-ten-joda {:dependencies [[org.clojure/clojure "1.8.0-RC3"]
25+
[org.threeten/threeten-extra "0.9"]
26+
[joda-time/joda-time "2.9"]]}}
27+
:aliases {"test-all" ["do"
28+
["with-profile" "1.7:1.8:1.7-three-ten-joda:1.8-three-ten-joda" "test"]]}
2229
:deploy-repositories [["clojars" {:url "https://clojars.org/repo"
2330
:sign-releases false}]])

test/java_time_test.clj

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,7 @@
293293
(j/period 0 :months)
294294
(j/period 0 :days)))
295295

296-
(is (j/period? (j/period))))
297-
298-
(testing "interval"
299-
(is (= (j/interval "1970-01-01T00:00:00Z/1970-01-01T00:00:01Z")
300-
(j/interval 0 1000)
301-
(j/interval (j/offset-date-time 1970 1 1 +0)
302-
(j/offset-date-time 1970 1 1 0 0 1 +0))))))
296+
(is (j/period? (j/period)))))
303297

304298
(deftest operations
305299
(testing "duration"
@@ -382,53 +376,9 @@
382376
(testing "minus"
383377
(is (= (j/day-of-week :monday)
384378
(j/minus (j/day-of-week 6) 5)
385-
(j/minus (j/day-of-week 6) (j/days 5))))))
386-
387-
(testing "interval"
388-
(is (= (j/interval 5000 10000)
389-
(j/move-end-by (j/interval 5000 6000) (j/seconds 4))
390-
(j/move-start-by (j/interval 0 10000) (j/seconds 5))
391-
(j/move-end-to (j/interval 5000 6000) 10000)
392-
(j/move-start-to (j/interval 0 10000) 5000)))
393-
394-
(is (= (j/instant 0) (j/start (j/interval 0 1000))))
395-
(is (= (j/instant 1000) (j/end (j/interval 0 1000))))
396-
397-
(testing "contains"
398-
(is (j/contains? (j/interval 0 1000) 500))
399-
(is (not (j/contains? (j/interval 0 1000) 1500)))
400-
(is (j/contains? (j/interval 0 1000) (j/interval 100 900)))
401-
(is (j/contains? (j/interval 0 1000) (j/interval 0 900)))
402-
(is (j/contains? (j/interval 0 1000) (j/interval 0 1000)))
403-
(is (j/contains? (j/interval 0 1000) (j/interval 1000 1000)))
404-
(is (not (j/contains? (j/interval 0 1000) (j/interval 1000 1001)))))
405-
406-
(testing "overlaps"
407-
(is (j/overlaps? (j/interval 0 1000) (j/interval 0 500)))
408-
(is (j/overlaps? (j/interval 0 1000) (j/interval 0 1500)))
409-
(is (j/overlaps? (j/interval 500 1000) (j/interval 0 1500)))
410-
(is (not (j/overlaps? (j/interval 0 1000) (j/interval 1500 2000))))
411-
412-
(is (= (j/interval 500 1000) (j/overlap (j/interval 500 1000) (j/interval 0 1500))))
413-
(is (nil? (j/overlap (j/interval 0 1000) (j/interval 1500 2000)))))
414-
415-
(testing "abuts"
416-
(is (j/abuts? (j/interval 0 1000) (j/interval 1000 2000)))
417-
(is (not (j/abuts? (j/interval 0 1000) (j/interval 900 2000)))))
418-
419-
(testing "gap"
420-
(is (= (j/interval 1000 2000) (j/gap (j/interval 0 1000) (j/interval 2000 3000))))
421-
(is (nil? (j/gap (j/interval 0 1000) (j/interval 500 1500)))))))
379+
(j/minus (j/day-of-week 6) (j/days 5)))))))
422380

423381
(deftest ordering
424-
(testing "interval"
425-
(is (j/before? (j/interval 1000 2000) (j/instant 5000)))
426-
(is (not (j/before? (j/interval 1000 5000) (j/instant 5000))))
427-
(is (j/before? (j/interval 1000 5000) (j/interval 5001 6000)))
428-
429-
(is (j/after? (j/interval 1000 5000) (j/instant 100)))
430-
(is (not (j/after? (j/interval 1000 5000) (j/instant 2000))))
431-
(is (j/after? (j/interval 1000 5000) (j/interval 100 999))))
432382

433383
(testing "times"
434384
(is (j/after? (j/local-date-time clock) (j/minus (j/local-date-time clock) (j/days 5))))
@@ -559,17 +509,17 @@
559509

560510
(deftest adjuster-test
561511
(testing "predefined adjusters"
562-
(is (= (j/adjust (j/local-date 2015 1 1) :next-working-day)
563-
(j/local-date 2015 1 2)))
564-
565512
(is (= (j/adjust (j/local-date 2015 1 1) :first-in-month :monday)
566513
(j/local-date 2015 1 5)))
567514

568515
(is (= (j/adjust (j/local-date 2015 1 1) :day-of-week-in-month 1 :monday)
569516
(j/local-date 2015 1 5)))
570517

571518
(is (= (j/adjust (j/local-date 2015 1 1) :day-of-week-in-month 2 :monday)
572-
(j/local-date 2015 1 12))))
519+
(j/local-date 2015 1 12)))
520+
521+
(is (= (j/adjust (j/local-date 2015 1 1) :first-day-of-next-year)
522+
(j/local-date 2016 1 1))))
573523

574524
(testing "functions as adjusters"
575525
(is (= (j/adjust (j/local-date 2015 1 1) j/plus (j/days 1))
@@ -624,10 +574,7 @@
624574
(is (= [2015 1 1] (j/as (j/local-date 2015 1 1) :year :month-of-year :day-of-month))))
625575

626576
(testing "throws"
627-
(is (thrown? Exception (j/as (j/local-time 0) :year))))
628-
629-
(testing "interval"
630-
(is (= 1 (j/as (j/interval (j/instant 0) (j/instant 1)) :millis))))))
577+
(is (thrown? Exception (j/as (j/local-time 0) :year))))))
631578

632579
(deftest legacy-conversion
633580
(testing "converts through instant"
@@ -643,6 +590,64 @@
643590
(testing "from java.util Date types"
644591
(is (= (j/zone-id "UTC") (j/zone-id (java.util.TimeZone/getTimeZone "UTC"))))))
645592

593+
(jt.u/when-threeten-extra
594+
(testing "adjusters"
595+
(is (= (j/adjust (j/local-date 2015 1 1) :next-working-day)
596+
(j/local-date 2015 1 2))))
597+
598+
(testing "interval"
599+
(is (= (j/interval "1970-01-01T00:00:00Z/1970-01-01T00:00:01Z")
600+
(j/interval 0 1000)
601+
(j/interval (j/offset-date-time 1970 1 1 +0)
602+
(j/offset-date-time 1970 1 1 0 0 1 +0))))
603+
604+
(is (= 1 (j/as (j/interval (j/instant 0) (j/instant 1)) :millis))))
605+
606+
(testing "operations"
607+
(is (= (j/interval 5000 10000)
608+
(j/move-end-by (j/interval 5000 6000) (j/seconds 4))
609+
(j/move-start-by (j/interval 0 10000) (j/seconds 5))
610+
(j/move-end-to (j/interval 5000 6000) 10000)
611+
(j/move-start-to (j/interval 0 10000) 5000)))
612+
613+
(is (= (j/instant 0) (j/start (j/interval 0 1000))))
614+
(is (= (j/instant 1000) (j/end (j/interval 0 1000))))
615+
616+
(testing "contains"
617+
(is (j/contains? (j/interval 0 1000) 500))
618+
(is (not (j/contains? (j/interval 0 1000) 1500)))
619+
(is (j/contains? (j/interval 0 1000) (j/interval 100 900)))
620+
(is (j/contains? (j/interval 0 1000) (j/interval 0 900)))
621+
(is (j/contains? (j/interval 0 1000) (j/interval 0 1000)))
622+
(is (j/contains? (j/interval 0 1000) (j/interval 1000 1000)))
623+
(is (not (j/contains? (j/interval 0 1000) (j/interval 1000 1001)))))
624+
625+
(testing "overlaps"
626+
(is (j/overlaps? (j/interval 0 1000) (j/interval 0 500)))
627+
(is (j/overlaps? (j/interval 0 1000) (j/interval 0 1500)))
628+
(is (j/overlaps? (j/interval 500 1000) (j/interval 0 1500)))
629+
(is (not (j/overlaps? (j/interval 0 1000) (j/interval 1500 2000))))
630+
631+
(is (= (j/interval 500 1000) (j/overlap (j/interval 500 1000) (j/interval 0 1500))))
632+
(is (nil? (j/overlap (j/interval 0 1000) (j/interval 1500 2000)))))
633+
634+
(testing "abuts"
635+
(is (j/abuts? (j/interval 0 1000) (j/interval 1000 2000)))
636+
(is (not (j/abuts? (j/interval 0 1000) (j/interval 900 2000)))))
637+
638+
(testing "gap"
639+
(is (= (j/interval 1000 2000) (j/gap (j/interval 0 1000) (j/interval 2000 3000))))
640+
(is (nil? (j/gap (j/interval 0 1000) (j/interval 500 1500))))))
641+
642+
(testing "ordering"
643+
(is (j/before? (j/interval 1000 2000) (j/instant 5000)))
644+
(is (not (j/before? (j/interval 1000 5000) (j/instant 5000))))
645+
(is (j/before? (j/interval 1000 5000) (j/interval 5001 6000)))
646+
647+
(is (j/after? (j/interval 1000 5000) (j/instant 100)))
648+
(is (not (j/after? (j/interval 1000 5000) (j/instant 2000))))
649+
(is (j/after? (j/interval 1000 5000) (j/interval 100 999)))))
650+
646651
(jt.u/when-joda
647652

648653
(def joda-clock (j/fixed-clock "2015-11-26T10:20:30.040Z" "UTC"))

0 commit comments

Comments
 (0)