Skip to content

Commit

Permalink
fix date formatter docs, added support for passing Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogthos committed May 30, 2024
1 parent d1a40e5 commit 885e18e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/selmer/filters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.")))))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/selmer/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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})))
Expand Down

0 comments on commit 885e18e

Please sign in to comment.