Skip to content

Commit 8fffdee

Browse files
author
Vadim Platonov
committed
do not accept zone id/offset as part of constructor
1 parent 1b3b928 commit 8fffdee

File tree

4 files changed

+225
-111
lines changed

4 files changed

+225
-111
lines changed

README.md

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ the time of various events:
8181
* `LocalTime` - bus schedule, opening time of a shop
8282
* `LocalDateTime` - start of a competition
8383

84+
A local date/time can be created as you'd expect:
85+
86+
```clj
87+
(local-date 2015 10)
88+
=> #<java.time.LocalDate 2015-10-01>
89+
90+
(local-time 10)
91+
=> #<java.time.LocalTime 10:00>
92+
93+
(local-date-time 2015 10)
94+
=> #<java.time.LocalDateTime 2015-10-01T00:00>
95+
```
96+
8497
#### Zoned Dates
8598

8699
There are two types which deal with zones: `OffsetDateTime` and
@@ -89,6 +102,33 @@ You can think of the `Offset` time as a more concrete version of the `Zoned`
89102
time. For example, the same timezone can have different offsets throughout the
90103
year due to DST or governmental regulations.
91104

105+
```clj
106+
(offset-time 10)
107+
=> #<java.time.OffsetTime 10:00+01:00>
108+
109+
(offset-date-time 10)
110+
=> #<java.time.OffsetDateTime 2015-10-01T10:00+01:00>
111+
112+
(zoned-date-time 10)
113+
=> #<java.time.ZonedDateTime 2015-10-01T10:00+01:00[Europe/London]>
114+
```
115+
116+
Offset/Zone times only take the offset/zone as the last arguments for the
117+
maximum arity constructor. You can influence the zone/offset by using the
118+
`with-zone` or `with-offset` functions, like so:
119+
120+
```clj
121+
(with-zone (zoned-date-time 2015 10) "UTC")
122+
=> #<java.time.ZonedDateTime 2015-10-01T00:00Z[UTC]>
123+
124+
(with-zone-same-instant (zoned-date-time 2015 10) "UTC")
125+
=> #<java.time.ZonedDateTime 2015-09-30T23:00Z[UTC]>
126+
127+
(with-clock (system-clock "UTC")
128+
(zoned-date-time 2015 10))
129+
=> #<java.time.ZonedDateTime 2015-10-01T00:00Z[UTC]>
130+
```
131+
92132
#### Instant
93133

94134
An `Instant` is used to generate a time stamp representing machine time. It
@@ -97,8 +137,9 @@ milliseconds since epoch (`1970-01-01T00:00:00Z`). An instant is directly
97137
analogous to `java.util.Date`:
98138

99139
```clj
100-
user=> (java.time.Instant/now)
101-
#object[java.time.Instant 0x1c1ac77a "2015-09-26T05:25:48.667Z"]
140+
user=> (instant)
141+
#<java.time.Instant "2015-09-26T05:25:48.667Z">
142+
102143
user=> (java.util.Date.)
103144
#inst "2015-09-26T05:25:50.118-00:00"
104145
```
@@ -209,7 +250,7 @@ java-time> (as *1 :minutes)
209250
Format a date:
210251

211252
```clj
212-
(format "MM/dd" (zoned-date-time 2015 9 28 "UTC"))
253+
(format "MM/dd" (zoned-date-time 2015 9 28))
213254
=> "09/28"
214255
```
215256

@@ -224,46 +265,46 @@ Zoned date-times and offset date-times/times always take the zone/offset as the
224265
last argument. Offsets can be specified as float values:
225266

226267
```clj
227-
(j/offset-time +1.5)
228-
=> #<java.time.OffsetTime 10:16:08.000+01:30>
268+
(zone-offset +1.5)
269+
=> #<java.time.ZoneOffset +01:30>
229270

230-
(j/offset-time -1.5)
231-
=> #<java.time.OffsetTime 07:16:08.000-01:30>
271+
(zone-offset -1.5)
272+
=> #<java.time.ZoneOffset -01:30>
232273
```
233274

234275
#### Conversions
235276

236277
Time entities can be converted to other time entities if the target contains
237-
less information, e.g.:
278+
less information, e.g. (assuming we're in UTC timezone):
238279

239280
```clj
240-
(zoned-date-time (offset-date-time 2015 9 28 1 +0))
281+
(zoned-date-time (offset-date-time 2015 9 28 1))
241282
=> #object[java.time.ZonedDateTime "2015-09-28T01:00Z"]
242283

243-
(instant (offset-date-time 2015 9 28 1 +0))
284+
(instant (offset-date-time 2015 9 28 1))
244285
=> #object[java.time.Instant "2015-09-28T01:00:00Z"]
245286

246-
(offset-time (offset-date-time 2015 9 28 1 +0))
287+
(offset-time (offset-date-time 2015 9 28 1))
247288
=> #object[java.time.OffsetTime "01:00Z"]
248289

249-
(local-date-time (offset-date-time 2015 9 28 1 +0))
290+
(local-date-time (offset-date-time 2015 9 28 1))
250291
=> #object[java.time.LocalDateTime "2015-09-28T01:00"]
251292

252-
(local-time (offset-time 1 +0))
293+
(local-time (offset-time 1))
253294
=> #object[java.time.LocalTime 0x3a3cd6d5 "01:00"]
254295
```
255296

256297
Any date which can be converted to an instant, can also be converted to pre-Java
257298
8 date types:
258299

259300
```clj
260-
(to-java-date (zoned-date-time 2015 9 28 "UTC"))
301+
(to-java-date (zoned-date-time 2015 9 28))
261302
=> #inst "2015-09-27T22:00:00.000-00:00"
262303

263-
(to-sql-date (zoned-date-time 2015 9 28 "UTC"))
304+
(to-sql-date (zoned-date-time 2015 9 28))
264305
=> #inst "2015-09-27T22:00:00.000-00:00"
265306

266-
(to-sql-timestamp (zoned-date-time 2015 9 28 "UTC"))
307+
(to-sql-timestamp (zoned-date-time 2015 9 28))
267308
=> #inst "2015-09-27T22:00:00.000000000-00:00"
268309
```
269310

@@ -277,7 +318,7 @@ An interval can be constructed from two entities that can be converted to
277318
instants:
278319

279320
```clj
280-
(interval (offset-date-time 2015 1 1 +0) (zoned-date-time 2016 1 1 "UTC"))
321+
(interval (offset-date-time 2015 1 1) (zoned-date-time 2016 1 1))
281322
=> #<org.threeten.extra.Interval 2015-01-01T00:00:00Z/2016-01-01T00:00:00Z>
282323

283324
(move-start-by *1 (duration 5 :days))
@@ -286,7 +327,7 @@ instants:
286327
(move-end-by *1 (duration 5 :days))
287328
=> #<org.threeten.extra.Interval 2015-01-06T00:00:00Z/2016-01-06T00:00:00Z>
288329

289-
(contains? *1 (offset-date-time 2015 1 1 +0))
330+
(contains? *1 (offset-date-time 2015 1 1))
290331
=> false
291332
```
292333

@@ -426,10 +467,12 @@ graph underneath. This provides for a very flexible way of defining the
426467
conversions while avoiding huge conditional statements and multiple definitions
427468
of the identical conversion logic. However, the flexibility comes with a cost:
428469

429-
1. The first call to a constructor will take around ~100ms as it will try to
470+
1. The first call to a constructor will take a _long_ time as it will try to
430471
find a path in the conversion graph. Subsequent calls will reuse the path.
431472
2. It's not trivial to evaluate the impact of adding and removing conversions
432473
both on the performance and the conversion path chosen for certain arguments.
474+
3. You might get nonsensical results for some of the paths in the graph that
475+
you might expect would make sense.
433476

434477
Hopefully, the performance issue will be resolved in the future...
435478

src/java_time.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
available-zone-ids zone-id zone-offset
4141
offset-date-time offset-time zoned-date-time
4242
system-clock fixed-clock offset-clock tick-clock
43-
zoned-date-time? offset-date-time? offset-time?]
43+
zoned-date-time? offset-date-time? offset-time?
44+
with-zone-same-instant with-offset with-offset-same-instant]
4445

4546
[java-time.convert
4647
as-map convert-amount to-java-date to-sql-date to-sql-timestamp

0 commit comments

Comments
 (0)