not sure if this is the right place to ask, but does anyone have a better way of converting PGIntervals (intervals from postgres database) to seconds than this?
(defn pginterval->seconds [^org.postgresql.util.PGInterval interval]
(+ (.getSeconds interval)
(* 60 (.getMinutes interval))
(* 60 60 (.getHours interval))
(* 60 60 24 (.getDays interval))
(* 60 60 24 365.25 (.getYears interval))))
it feels a bit long windedSomething like
(let [d (java.util.Date. 0)]
(.add interval d)
(/ (.getTime d) 1000))
But note that none of the approaches is correct. An interval has separate fields for different time units for a reason. At the very least, the year part is crucial because of leap years - different years have different lengths and so the same interval would have a different effective length when applied to different dates.ok. I think I have an anchor date in the record I can use and from that I can get a java.util.Date for the start and the end, convert them to java.time.Instant objects and do the date arithmetic from there (that I think will be close enough for what I'm doing in this instance)
You don't need Instant to subtract two dates to get seconds?..
I see. Also note that there's add(Calendar). Might be more convenient in your case, dunno.
possibly. I think the general approach of - add the interval to some kind of java.util.Date to get a start and end date and then go from there was the mental flip I needed
(mentioning the leap years helped with that)
TIL: https://cljdoc.org/d/com.github.seancorfield/honeysql/2.6.1161/api/honey.sql.pg-ops#-%3E%3E, which helped me translate some sql