Skip to content

Commit

Permalink
add support for java.time.Instant in date filter
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed May 30, 2024
1 parent 0f13d9d commit c15e760
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/selmer/filters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ map. The rest of the arguments are optional and are always strings."
@(resolve 'jsonista.core/write-value-as-string)
(catch Exception e
(fn [_]
(throw (ex-info "Supported JSON libraries: cheshire, clojure.data.json or
jsonista. Provide one of these on the classpath to enable
(throw (ex-info "Supported JSON libraries: cheshire, clojure.data.json or
jsonista. Provide one of these on the classpath to enable
the json filter." {} e))))))))))

(def valid-date-formats
Expand Down Expand Up @@ -71,6 +71,10 @@ map. The rest of the arguments are optional and are always strings."
(.atZone (ZoneId/systemDefault))
(.toLocalDateTime))

(instance? java.time.Instant d)
(-> (.atZone ^java.time.Instant d (ZoneId/systemDefault))
(.toLocalDateTime))

:else
(throw (IllegalArgumentException. (str d " is not a valid date format.")))))

Expand Down Expand Up @@ -234,10 +238,11 @@ map. The rest of the arguments are optional and are always strings."
(Locale/getDefault))]
(String/format locale fmt (into-array Object [n]))))

;;; Formats a date with default locale, expects an instance of DateTime (Joda Time) or Date.
;;; Formats a date with default locale. Supports a wide range of
;;; date types (incl. java.util.Date, java.time.Instant, java.sql.Date)
;;; The format can be a key from valid-date-formats or a manually defined format
;;; Look in
;;; http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
;;; https://docs.oracle.com/en/java/javase/21/docs/api//java.base/java/time/format/DateTimeFormatter.html
;;; for formatting help.
;;; You can also format time with this.
;;; An optional locale for formatting can be given as second parameter
Expand Down
19 changes: 17 additions & 2 deletions test/selmer/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,9 @@

(deftest filter-date
(let [date (java.util.Date.)
firstofmarch (java.util.Date. 114 2 1)]
date-inst (.toInstant date)
firstofmarch (java.util.Date. 114 2 1)
firstofmarch-inst (.toInstant firstofmarch)]
(is (= "" (render "{{d|date:\"yyyy-MM-dd\"}}" {:d nil})))
(is (= (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") date)
(render "{{f|date:\"yyyy-MM-dd HH:mm:ss\"}}" {:f date})))
Expand All @@ -883,7 +885,20 @@
(is (= "2014/3/1 00:00" (render "{{d|date:shortDateTime:zh}}" {:d firstofmarch})))
(is (= "2014年3月1日 00:00:00" (render "{{d|date:mediumDateTime:zh}}" {:d firstofmarch})))
(is (= "2014年3月1日" (render "{{d|date:longDate:zh}}" {:d firstofmarch})))
(is (= "2014 Mar 1" (render "{{d|date:longDate:en_US}}" {:d firstofmarch})))))
(is (= "2014 Mar 1" (render "{{d|date:longDate:en_US}}" {:d firstofmarch})))
(is (= (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") date)
(render "{{f|date:\"yyyy-MM-dd HH:mm:ss\"}}" {:f date-inst})))
(is (= (.format (java.text.SimpleDateFormat. "MMMM" (java.util.Locale. "fr")) firstofmarch)
(render "{{f|date:\"MMMM\":fr}}" {:f firstofmarch-inst})))
(is (= "00:00" (render "{{d|date:shortTime:en_US}}" {:d firstofmarch-inst})))
(is (= "00:00" (render "{{d|date:shortTime:zh}}" {:d firstofmarch-inst})))
(is (= "2014-03-01" (render "{{d|date:shortDate:en_US}}" {:d firstofmarch-inst})))
(is (= "2014/3/1" (render "{{d|date:shortDate:zh}}" {:d firstofmarch-inst})))
(is (= "2014-03-01 00:00" (render "{{d|date:shortDateTime:en_US}}" {:d firstofmarch-inst})))
(is (= "2014/3/1 00:00" (render "{{d|date:shortDateTime:zh}}" {:d firstofmarch-inst})))
(is (= "2014年3月1日 00:00:00" (render "{{d|date:mediumDateTime:zh}}" {:d firstofmarch-inst})))
(is (= "2014年3月1日" (render "{{d|date:longDate:zh}}" {:d firstofmarch-inst})))
(is (= "2014 Mar 1" (render "{{d|date:longDate:en_US}}" {:d firstofmarch-inst})))))

(deftest filter-hash-md5
(is (= "acbd18db4cc2f85cedef654fccc4a4d8"
Expand Down

0 comments on commit c15e760

Please sign in to comment.