Fork me on GitHub
#sql
<
2021-12-09
>
rovanion16:12:03

Anyone happen to have a java.time.ZonedDateTime to next.jdbc adaptor for postgres lying around? I've found one for the old JDBC but not been able to adapt (heh) it: https://github.com/metabase/metabase/blob/master/src/metabase/db/jdbc_protocols.clj#L60-L62

rovanion17:12:41

Just found another of your comments that led me to this:

(extend-protocol next.jdbc.prepare/SettableParameter
  java.time.ZonedDateTime
  (set-parameter [^java.time.ZonedDateTime zdt ^java.sql.PreparedStatement stmt ^long idx]
    (.setTimestamp stmt idx (java.sql.Timestamp/from (jt/instant zdt)))))

rovanion17:12:10

That's the exact same code.

rovanion17:12:41

The only thing I was wondering about that snippet was the type hint. v isn't an Instant, it's a ZonedDateTime right?

rovanion17:12:49

Question I really should answer by doing some proper testing: The timezone is carried over into the SQL timestamp right?

dcj00:12:35

IIRC my code translates between ZDTs on the Clojure side and timestamptz in Postgres. Over the years, I have discovered all kinds of ways to mess up time: • Your local JVM will likely pick up your local timzeone unless you take pains to avoid that • Postgres server may have it's own tz settting. Best to carefully check all this

dcj00:12:02

On the way out of the db, I do something like this:

(extend-protocol result-set/ReadableColumn

  java.sql.Timestamp

  (read-column-by-label ^java.time.ZonedDateTime [^java.sql.Timestamp v _]
    (time/zoned-date-time (time/instant v) "UTC"))
  (read-column-by-index ^java.time.ZonedDateTime [^java.sql.Timestamp v _2 _3]
    (time/zoned-date-time (time/instant v) "UTC"))

rovanion16:12:11

Java-time wouldn't load into my JVM :/

rovanion17:12:05

A restart of the JVM fixed that.