Skip to content

Instantly share code, notes, and snippets.

@buuhsmead
Last active May 15, 2016 18:58
Show Gist options
  • Save buuhsmead/4722eb4b81162341c518e6a745535791 to your computer and use it in GitHub Desktop.
Save buuhsmead/4722eb4b81162341c518e6a745535791 to your computer and use it in GitHub Desktop.
clojure rfc3339 simple date format
(def ^:private thread-local-utc-date-format
;; SimpleDateFormat is not thread-safe, so we use a ThreadLocal proxy for access.
;; http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4228335
(proxy [ThreadLocal] []
(initialValue []
(doto (java.text.SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss.SSS-00:00")
;; RFC3339 says to use -00:00 when the timezone is unknown (+00:00 implies a known GMT)
(.setTimeZone (java.util.TimeZone/getTimeZone "GMT"))))))
(defn write-date
"Write a java.util.Date using the IWriter protocol
as RFC3339 timestamp, always in UTC."
[^java.util.Date d w]
(let [utc-format (.get thread-local-utc-date-format)]
(writer/write w "#inst \"")
(writer/write w (.format utc-format d))
(writer/write w "\"")))
url: https://github.com/edn-format/data.edn/blob/master/src/org/edn_format/print/util.clj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment