How to properly set a timezone in a clojure REPL?

Viewed 334

When running a query on a postgres database on a clojure REPL, the timestamps fields are presented in UTC and I need them to be in timezone America/Sao_Paulo (UTC-3)

So far I have tried the following on Intellij's REPL:

  • Set -Duser.timezone=America/Sao_Paulo inside the file idea.vmoptions (intellij's)
  • Add :jvm-opts ["-Duser.timezone=America/Sao_Paulo"] to project.clj
  • Add -Duser.timezone=America/Sao_Paulo in Intellij's REPL configuration
  • export JAVA_OPTS="-Duser.timezone=America/Sao_Paulo:$JAVA_OPTS" inside ~/.zshrc

and the following on Leiningen REPL:

  • Add :jvm-opts ["-Duser.timezone=America/Sao_Paulo"] to project.clj
  • export JAVA_OPTS="-Duser.timezone=America/Sao_Paulo:$JAVA_OPTS" inside ~/.zshrc

None worked so far!

Sample code

(ns experiments
  (:require [next.jdbc :as jdbc]))

(def db
  {:dbtype   "postgres"
   :dbname   "<dbname>"
   :host     "<host>"
   :port     5432
   :user     "<user>"
   :password "<pass>"})

(def ds (jdbc/get-datasource db))

(jdbc/execute! ds ["select current_timestamp"])
1 Answers

You did not mention any Postgres options. Please study this page carefully for info and options.

If the above does not solve your problem, it may be easiest to use java.time to do the conversion. I also have some helper/convenience functions available here. Unit tests show them in action, and the source code provides examples of java.time interop from clojure.

I would avoid the older Joda Time libraries as they are obsolete (replaced by java.time). I think that Java interop is the easiest & most straightforward way to access java.time.

Related