Skip to content

Instantly share code, notes, and snippets.

@jcf
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcf/e093a1223b024cebcab7 to your computer and use it in GitHub Desktop.
Save jcf/e093a1223b024cebcab7 to your computer and use it in GitHub Desktop.
clj-time: Parse datetime string with many formats, and original time zone intact
(require '[clj-time.core :as t])
(require '[clj-time.format :as f])
(defn parse-date [s]
(.. (f/formatter (t/default-time-zone)
"YYYY-MM-dd'T'HH:mm:ss.SSSZZ"
"YYYY-MM-dd'T'HH:mm:ssZZ"
"YYYY-MM-dd HH:mm:ss ZZ"
"EEE MMM dd YYYY HH:mm:ss zZ '('z')'")
withOffsetParsed
(parseDateTime s)))
(defn- tz [year month day hour mins secs zone]
(t/from-time-zone (t/date-time year month day hour mins secs)
(t/time-zone-for-offset zone)))
(defn- date-formats []
(let [pst-time (tz 2015 2 13 14 52 32 -8)]
{"2014-11-12 12:13:14 +0400" (tz 2014 11 12 12 13 14 4)
"2014-11-12T12:13:14+0200" (tz 2014 11 12 12 13 14 2)
"2014-11-12T12:13:14.000Z" (tz 2014 11 12 12 13 14 0)
"2014-11-12T12:13:14Z" (tz 2014 11 12 12 13 14 0)
"Mon Feb 09 2015 14:52:32 GMT+0000 (GMT)" (tz 2015 2 9 14 52 32 0)
"Fri Feb 13 2015 14:52:32 PST-0800 (PST)" pst-time}))
(deftest test-parse-date
(doseq [[s t] (date-formats)]
(is (= (parse-date s) t))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment