forked from dm3/clojure.java-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_java8.clj
More file actions
94 lines (74 loc) · 3.75 KB
/
pre_java8.clj
File metadata and controls
94 lines (74 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(ns java-time.pre-java8
(:require [java-time
[convert :as jt.c]
[local :as jt.l]
[temporal :as jt.t]
[defconversion :refer [conversion!]]]))
(defn ^java.util.Date java-date
"Creates a `java.util.Date` out of any combination of arguments valid for
`java-time/instant` or the Instant itself.
A `java.util.Date` represents an instant in time. It's a direct analog of the
`java.time.Instant` type introduced in the JSR-310. Please consider using the
`java.time.Instant` (through `java-time/instant`) directly."
([] (java.util.Date/from (jt.t/instant)))
([a] (java.util.Date/from (jt.t/instant a)))
([a b] (java.util.Date/from (jt.t/instant a b))))
(defn- arities [type ctor n-args]
(for [i (range (inc n-args))]
(let [arg-vec (vec (take i (repeatedly gensym)))
type-ctor (symbol (name type) "valueOf")]
`(~arg-vec (~type-ctor (~ctor ~@arg-vec))))))
(defmacro defsqldate [type name ctor n-args doc]
(let [fn-name (with-meta name {:tag type})]
`(defn ~fn-name ~doc
~@(arities type ctor n-args))))
(conversion! java.sql.Date java.time.LocalDate
(fn [^java.sql.Date dt]
(.toLocalDate dt)))
(conversion! java.sql.Timestamp java.time.LocalDateTime
(fn [^java.sql.Timestamp dt]
(.toLocalDateTime dt)))
(conversion! java.sql.Time java.time.LocalTime
(fn [^java.sql.Time dt]
(.toLocalTime dt)))
(defsqldate java.sql.Date sql-date jt.l/local-date 3
"Creates a `java.sql.Date` out of any combination of arguments valid for
`java-time/local-date` or the `LocalDate` itself.
Please consider using the JSR-310 Java Time types instead of `java.sql.Date`
if your drivers support them.
Even though `java.sql.Date` extends a `java.util.Date`, it's supposed to be
used as a local date (no time component or timezone) for the purposes of
conversion from/to native JDBC driver DATE types.")
(defsqldate java.sql.Timestamp sql-timestamp jt.l/local-date-time 7
"Creates a `java.sql.Timestamp` in the local timezone out of any combination
of arguments valid for `java-time/local-date-time` or the `LocalDateTime`
itself.
The `sql-timestamp` constructor function does not support `Timestamp`
construction from an `Instant` or a long millis value. Please use
`instant->sql-timestamp` for this purpose.
Please consider using the JSR-310 Java Time types instead of
`java.sql.Timestamp` if your drivers support them.
`java.sql.Timestamp` is a version of a `java.util.Date` supposed to be used
as a local date-time (no timezone) for the purposes of conversion from/to native
JDBC driver TIMESTAMP types.")
(defn instant->sql-timestamp
"Creates a `java.sql.Timestamp` from the provided `instant-or-millis` - a
millisecond numeric time value or something convertible to an `Instant`.
Please consider using the JSR-310 Java Time types instead of
`java.sql.Timestamp` if your drivers support them.
`java.sql.Timestamp` is a version of a `java.util.Date` supposed to be used
as a local date-time (no timezone) for the purposes of conversion from/to native
JDBC driver TIMESTAMP types."
[instant-or-millis]
(if (number? instant-or-millis)
(java.sql.Timestamp. (long instant-or-millis))
(java.sql.Timestamp/from (jt.t/instant instant-or-millis))))
(defsqldate java.sql.Time sql-time jt.l/local-time 3
"Creates a `java.sql.Time` out of any combination of arguments valid for
`java-time/local-time` (except the nanos constructor) or the `LocalTime`
itself.
Please consider using the JSR-310 Java Time types instead of `java.sql.Time`
if your drivers support them.
Even though `java.sql.Time` extends a `java.util.Date`, it's supposed to be
used as a local time (no date component or timezone) for the purposes of
conversion from/to native JDBC driver TIME types.")