Fork me on GitHub
#sql
<
2020-01-16
>
murtaza5204:01:55

(jt/format (jt/instant->sql-timestamp (jt/truncate-to (jt/instant (* 1578830944 1000)) :days))) => "2020-01-12 03:00:00.0" I am trying to convert a given unix time in seconds to a sql date string. In the above code I am truncating the hours part, but when I convert it into string it shows an hour part, what am I doing wrong ?

murtaza5204:01:12

jt is java-time ns

seancorfield04:01:00

Timezones, I would assume.

seancorfield04:01:44

Instants are UTC. Converting to a SQL timestamp is local time.

seancorfield04:01:37

When I run that code, I see different hours because I'm in a different TZ to you

user=> (jt/format (jt/instant->sql-timestamp (jt/truncate-to (jt/instant (* 1578830944 1000)) :days)))
"2020-01-11 16:00:00.0"

murtaza5205:01:55

@seancorfield thanks, had missed that.

murtaza5207:01:37

just fyi, this worked with timezone offset-

(jt/format
 (jt/sql-timestamp
  (jt/truncate-to
   (java.time.LocalDateTime/ofInstant (jt/instant (* 1578830944 1000)) (java.time.ZoneOffset/ofTotalSeconds 7200))
   :days))) 

seancorfield20:01:37

@murtaza52 Bear in mind that will work for your machine on your timezone -- be careful about running that code on other machines that may be on different timezones.

seancorfield20:01:03

We have all our servers set to UTC and we have our databases set to UTC. Timezones are a giant pain to deal with.

4