Fork me on GitHub
#sql
<
2018-01-22
>
Will16:01:00

How do you guys use dates in clojure? I want a symbol to represent a timestamp a couple months before current date to pass into a query

seancorfield17:01:34

@josmith2016 I've used a number of libraries over the years. I still quite like date-clj -- https://github.com/stackoverflow/date-clj -- but it only traffics in java.util.Date so we use clj-time -- https://github.com/clj-time/clj-time -- which is the de factor standard for Joda Time (for UTC-based time). We're switching over to clojure.java-time -- https://github.com/dm3/clojure.java-time -- which is a great wrapper for Java Time (using the native Java libraries makes sens if you're on Java 8 or 9).

seancorfield17:01:48

All of them support date arithmetic.

seancorfield18:01:02

Example using clojure.java-time:

boot.user=> (require '[java-time :as jt])
nil
boot.user=> (-> (jt/zoned-date-time) (jt/minus (jt/months 2)) (jt/java-date))
#inst "2017-11-22T18:01:47.451-00:00"
boot.user=> 

Will19:01:14

Thank you @seancorfield. Also I was wondering if I have 2 different databases I’m connecting to, say db1 and db2. If I run a query on db1, using the jdbc/query db1 command, then I query the second using jdbc/query db2, then I query the first one again the same way. What happens? Does it reuse the connection from the first query? Does it open and close a connection to db1 then db2 then db1 again? If it doesn’t use the same connection, is this something the connection pooling would fix?

seancorfield19:01:20

When you say "different databases" do you mean different schemas within the same database instance, or genuinely separate database instances?

seancorfield19:01:02

For the former, assuming user permissions allow you to select from both schemas, you can use a single connection for both db1 and db2 queries. For the latter, each database instance would have its own set of connections.

seancorfield19:01:01

As for reusing connections against any particular database, read http://clojure-doc.org/articles/ecosystem/java_jdbc/reusing_connections.html -- connections are only going to be reused if you set things up that way.

Will19:01:18

I mean 2 genuinely separate database instances, that answered my question thanks!

seancorfield19:01:19

So you'll have a connection pool set up for db1 and a separate connection pool set up for db2.