Skip to content

Commit f6c34c7

Browse files
author
Vadim Platonov
committed
pre-java8: add instant->sql-timestamp
1 parent e844263 commit f6c34c7

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* `zone-id?` predicate
88
* `set-clock!` - sets the mocked clock value to the supplied instant
99
* `when-joda-time-loaded` - macro which runs code when Joda-Time is on the classpath
10+
* `instant->sql-timestamp` - produce a `java.sql.Timestamp` from an Instant-like object
1011

1112
## 0.3.1
1213

src/java_time.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
format formatter]
6565

6666
[java-time.pre-java8
67-
java-date sql-date sql-timestamp sql-time]
67+
java-date sql-date sql-timestamp instant->sql-timestamp sql-time]
6868

6969
[java-time.interval
7070
move-start-to move-end-to move-start-by move-end-by

src/java_time/pre_java8.clj

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns java-time.pre-java8
22
(:require [java-time
3+
[convert :as jt.c]
34
[local :as jt.l]
45
[temporal :as jt.t]
56
[defconversion :refer [conversion!]]]))
@@ -40,7 +41,7 @@
4041

4142
(defsqldate java.sql.Date sql-date jt.l/local-date 3
4243
"Creates a `java.sql.Date` out of any combination of arguments valid for
43-
`java-time/local-date` or the LocalDate itself.
44+
`java-time/local-date` or the `LocalDate` itself.
4445
4546
Please consider using the JSR-310 Java Time types instead of `java.sql.Date`
4647
if your drivers support them.
@@ -50,8 +51,13 @@
5051
conversion from/to native JDBC driver DATE types.")
5152

5253
(defsqldate java.sql.Timestamp sql-timestamp jt.l/local-date-time 7
53-
"Creates a `java.sql.Timestamp` out of any combination of arguments valid for
54-
`java-time/local-date-time` or the LocalDateTime itself.
54+
"Creates a `java.sql.Timestamp` in the local timezone out of any combination
55+
of arguments valid for `java-time/local-date-time` or the `LocalDateTime`
56+
itself.
57+
58+
The `sql-timestamp` constructor function does not support `Timestamp`
59+
construction from an `Instant` or a long millis value. Please use
60+
`instant->sql-timestamp` for this purpose.
5561
5662
Please consider using the JSR-310 Java Time types instead of
5763
`java.sql.Timestamp` if your drivers support them.
@@ -60,9 +66,24 @@
6066
as a local date-time (no timezone) for the purposes of conversion from/to native
6167
JDBC driver TIMESTAMP types.")
6268

69+
(defn instant->sql-timestamp
70+
"Creates a `java.sql.Timestamp` from the provided `instant-or-millis` - a
71+
millisecond numeric time value or something convertible to an `Instant`.
72+
73+
Please consider using the JSR-310 Java Time types instead of
74+
`java.sql.Timestamp` if your drivers support them.
75+
76+
`java.sql.Timestamp` is a version of a `java.util.Date` supposed to be used
77+
as a local date-time (no timezone) for the purposes of conversion from/to native
78+
JDBC driver TIMESTAMP types."
79+
[instant-or-millis]
80+
(if (number? instant-or-millis)
81+
(java.sql.Timestamp. (long instant-or-millis))
82+
(java.sql.Timestamp/from (jt.t/instant instant-or-millis))))
83+
6384
(defsqldate java.sql.Time sql-time jt.l/local-time 3
6485
"Creates a `java.sql.Time` out of any combination of arguments valid for
65-
`java-time/local-time` (except the nanos constructor) or the LocalTime
86+
`java-time/local-time` (except the nanos constructor) or the `LocalTime`
6687
itself.
6788
6889
Please consider using the JSR-310 Java Time types instead of `java.sql.Time`

test/java_time_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,13 +676,18 @@
676676
(is (= (java.sql.Timestamp/valueOf (j/local-date-time 2000 10 5 20 30 40))
677677
(j/sql-timestamp 2000 10 5 20 30 40)
678678
(j/sql-timestamp (j/local-date-time 2000 10 5 20 30 40))))
679+
(is (= (java.sql.Timestamp/from (j/instant 1))
680+
(java.sql.Timestamp. 1)
681+
(j/instant->sql-timestamp (j/instant 1))
682+
(j/instant->sql-timestamp 1)))
679683
(is (= (java.sql.Time/valueOf (j/local-time 20 30 40))
680684
(j/sql-time 20 30 40)
681685
(j/sql-time (j/local-time 20 30 40))))
682686

683687
(is (= (j/local-date 2000 10 5) (j/local-date (j/sql-date 2000 10 5))))
684688
(is (= (j/local-date-time 2000 10 5 20 30 40 1000)
685689
(j/local-date-time (j/sql-timestamp 2000 10 5 20 30 40 1000))))
690+
(is (= (j/instant 1) (j/instant (j/instant->sql-timestamp 1))))
686691
(is (= (j/local-time 20 30 40) (j/local-time (j/sql-time 20 30 40)))))
687692

688693
(testing "from java.util Date types"

0 commit comments

Comments
 (0)