jsonclojureclojurescriptreagenttransit

How to decode, and then format, tagged LocalDateTime value


This is my Clojurescript function,

(defn message-list [messages]
  (println messages) ;; stmt#1
  [:ul.messages
   (for [{:keys [timestamp message name]} @messages]
     ^{:key timestamp}
     [:li
      [:time (.toLocaleString timestamp)] ;; stmt#2
      [:p message]
      [:p " - " name]])])

stmt#1 is printing,

#<Atom: [{:id 1, :name Adeel Ansari, :message Hello, from the other side., 
          :timestamp #object[Transit$TaggedValue [TaggedValue: LocalDateTime, 2020-01-13T18:19:50.552]]}]>

and stmt#2 is printing,

[TaggedValue: LocalDateTime, 2020-01-13T18:19:50.552]

Now, I would like to print it as, say, 13/01/2020 18:19; how should I go about it? I've no idea how to decode tagged-values.


Solution

  • You can obtain the value from the TaggedValue using .-rep, and then, you can parse that String using some library.

    For example you can parse the date, using cljc.java-time, like this:

    (let [tv (t/tagged-value "LocalDateTime" "2019-01-01T11:22:33.123")]
        (cljc.java-time.local-date-time/parse (.-rep tv))) => #object[LocalDateTime 2019-01-01T11:22:33.123]
    

    Or you can use Tick; then your code would look something like,

    (ns xx.yy.zz
      (:require ..
                [tick.locale-en-us]
                [tick.alpha.api :as t]
                ..
                ))
    ...
      (defn message-list [messages]
        ...
           [:li
            [:time (t/format (t/formatter "dd/MM/yyyy HH:mm") (t/parse (.-rep timestamp)))]   
            ...]
        ...)
    ...