Skip to content

Instantly share code, notes, and snippets.

Last active Sep 23, 2018
What would you like to do?
Transit / edn date/datetime serialisers
(ns metosin.common.edn
(:require [clj-time.format :as f])
(:require [cljs-time.format :as f]
#+clj (:import [org.joda.time DateTime LocalDate]))
;; #DateTime tagging
(def iso8601-formatter (f/formatters :date-time))
(def date-formatter (f/formatters :date))
(defn- datetime->reader-str [d]
(str "#DateTime \"" (f/unparse iso8601-formatter d) \"))
(defn- date->reader-str [d]
(str "#Date \"" (f/unparse-local-date date-formatter d) \"))
(defn- reader-str->datetime [s]
(f/parse iso8601-formatter s))
(defn- reader-str->date [s]
(f/parse-local-date date-formatter s))
(defmethod print-dup DateTime [^DateTime d out]
(.write out (datetime->reader-str d)))
(defmethod print-method DateTime [^DateTime d out]
(.write out (datetime->reader-str d)))
(defmethod print-dup LocalDate [^LocalDate d out]
(.write out (date->reader-str d)))
(defmethod print-method LocalDate [^LocalDate d out]
(.write out (date->reader-str d)))
(alter-var-root #'*data-readers* assoc
'DateTime #'sampo.edn/reader-str->datetime
'Date #'sampo.edn/reader-str->date))
(cljs.reader/register-tag-parser! "DateTime" reader-str->datetime)
(cljs.reader/register-tag-parser! "Date" reader-str->date)
(extend-protocol IPrintWithWriter
(-pr-writer [d out opts]
(-write out (datetime->reader-str d)))
(-pr-writer [d out opts]
(-write out (date->reader-str d)))))
(ns metosin.transit.dates
"Transit readers and writers for JodaTime and
Supports two types:
- DateTime (org.joda.time.DateTime,
- LocalDate (org.joda.time.LocalDate,
Represents DateTimes in RFC 3339 format: yyyy-mm-ddTHH:MM:SS.sssZ.
RFC 3339 format is an specific profile of ISO 8601 DateTime format.
Some consideration has been made to provide performant read
implemenation for ClojureScript."
(:require [cognitect.transit :as transit]
[clojure.string :as string]
#?@(:cljs [[goog.string :as gs]]))
#?(:clj (:import [org.joda.time])))
#?(:clj (set! *warn-on-reflection* true))
(def DateTime #?(:clj org.joda.time.DateTime, :cljs
(def LocalDate #?(:clj org.joda.time.LocalDate, :cljs
(defn write-date-time
"Represent DateTime in RFC3339 format string."
#?(:clj (.toString (.withZone ^org.joda.time.DateTime d (org.joda.time.DateTimeZone/forID "UTC")))
:cljs (str (.getUTCFullYear d)
"-" (gs/padNumber (inc (.getUTCMonth d)) 2)
"-" (gs/padNumber (.getUTCDate d) 2)
"T" (gs/padNumber (.getUTCHours d) 2)
":" (gs/padNumber (.getUTCMinutes d) 2)
":" (gs/padNumber (.getUTCSeconds d) 2)
"." (gs/padNumber (.getUTCMilliseconds d) 3)
(defn read-date-time
"Read RFC3339 string to DateTime."
#?(:clj (org.joda.time.DateTime/parse s)
:cljs ( s)))
(defn write-local-date
"Represent Date in YYYY-MM-DD format."
#?(:clj (.toString ^org.joda.time.LocalDate x)
:cljs (.toIsoString x true false)))
(defn read-local-date
"Read Date in YYYY-MM-DD format."
#?(:clj (org.joda.time.LocalDate/parse x)
:cljs (let [[_ y m d] (re-find #"(\d{4})-(\d{2})-(\d{2})" x)]
( (long y) (dec (long m)) (long d)))))
(def writers
{DateTime (transit/write-handler (constantly "DateTime") write-date-time)
LocalDate (transit/write-handler (constantly "Date") write-local-date)})
(def readers
{"DateTime" (transit/read-handler read-date-time)
"Date" (transit/read-handler read-local-date)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment