From 885e18e86836d990a0d5aad15caf734608a5dfe1 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Thu, 30 May 2024 17:55:43 -0400 Subject: [PATCH] fix date formatter docs, added support for passing Instant --- src/selmer/filters.clj | 9 ++++++--- test/selmer/core_test.clj | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/selmer/filters.clj b/src/selmer/filters.clj index e4bf6da..239b28d 100644 --- a/src/selmer/filters.clj +++ b/src/selmer/filters.clj @@ -57,7 +57,7 @@ map. The rest of the arguments are optional and are always strings." (-> (.getTime ^java.sql.Time d) (Instant/ofEpochMilli) (LocalDateTime/ofInstant (ZoneId/systemDefault))) - + (instance? java.sql.Timestamp d) (-> (.getTime ^java.sql.Timestamp d) (Instant/ofEpochMilli) @@ -70,6 +70,9 @@ map. The rest of the arguments are optional and are always strings." (-> (.toInstant ^java.util.Date d) (.atZone (ZoneId/systemDefault)) (.toLocalDateTime)) + + (instance? java.time.Instant d) + (LocalDateTime/ofInstant d (ZoneId/systemDefault)) :else (throw (IllegalArgumentException. (str d " is not a valid date format."))))) @@ -234,10 +237,10 @@ 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, expects an instance of DateTime Date, or Instant. ;;; 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/javase/8/docs/api/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..cebd917 100644 --- a/test/selmer/core_test.clj +++ b/test/selmer/core_test.clj @@ -873,6 +873,8 @@ (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}))) + (is (= (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") date) + (render "{{f|date:\"yyyy-MM-dd HH:mm:ss\"}}" {:f (.toInstant date)}))) (is (= (.format (java.text.SimpleDateFormat. "MMMM" (java.util.Locale. "fr")) firstofmarch) (render "{{f|date:\"MMMM\":fr}}" {:f firstofmarch}))) (is (= "00:00" (render "{{d|date:shortTime:en_US}}" {:d firstofmarch})))