diff --git a/src/selmer/filters.clj b/src/selmer/filters.clj index e4bf6da..54d7f24 100644 --- a/src/selmer/filters.clj +++ b/src/selmer/filters.clj @@ -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 @@ -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."))))) @@ -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 diff --git a/test/selmer/core_test.clj b/test/selmer/core_test.clj index b47d47b..68d3a01 100644 --- a/test/selmer/core_test.clj +++ b/test/selmer/core_test.clj @@ -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}))) @@ -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"