Skip to content

Commit

Permalink
fixes #308 by adding support for java.time.Instant in date filter (#309)
Browse files Browse the repository at this point in the history
* cognitect test runner is no longer used

Signed-off-by: Sean Corfield <[email protected]>

* ignore calva repl file

Signed-off-by: Sean Corfield <[email protected]>

* add support for java.time.Instant in date filter

Signed-off-by: Sean Corfield <[email protected]>

---------

Signed-off-by: Sean Corfield <[email protected]>
Co-authored-by: Dmitri Sotnikov <[email protected]>
  • Loading branch information
seancorfield and yogthos authored May 30, 2024
1 parent a44e014 commit 2c2be85
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pom.xml.asc
.portal
test/readme.clj
.idea
*.iml
*.iml
.calva/repl.calva-repl
5 changes: 2 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
:test ; for testing
{:extra-paths ["test"]
:extra-deps {;; test dependencies
lambdaisland/kaocha {:mvn/version "1.75.1190"}
lambdaisland/kaocha {:mvn/version "1.75.1190"}
criterium/criterium {:mvn/version "0.4.6"}}
:main-opts ["-m" "kaocha.runner"]
:exec-fn cognitect.test-runner.api/test}}}
:main-opts ["-m" "kaocha.runner"]}}}
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 @@ -74,6 +74,10 @@ map. The rest of the arguments are optional and are always strings."
(instance? java.time.Instant d)
(LocalDateTime/ofInstant d (ZoneId/systemDefault))

(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 @@ -237,10 +241,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 Date, or Instant.
;;; 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
;;; https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.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 @@ -885,7 +887,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 2c2be85

Please sign in to comment.