Skip to content

Commit fe93afb

Browse files
committed
Print GregorianCalendars as #inst literals
1 parent ea98114 commit fe93afb

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/lambdaisland/deep_diff/printer.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
#(str (.format ^SimpleDateFormat (.get thread-local-utc-timestamp-format) %)
6161
(format ".%09d-00:00" (.getNanos ^Timestamp %)))))
6262

63+
(def ^:private print-calendar
64+
(puget/tagged-handler
65+
'inst
66+
#(let [formatted (format "%1$tFT%1$tT.%1$tL%1$tz" %)
67+
offset-minutes (- (.length formatted) 2)]
68+
(str (subs formatted 0 offset-minutes)
69+
":"
70+
(subs formatted offset-minutes)))))
71+
6372
(def ^:private print-handlers
6473
{'lambdaisland.deep_diff.diff.Deletion
6574
print-deletion
@@ -103,6 +112,9 @@
103112
'java.util.Date
104113
print-date
105114

115+
'java.util.GregorianCalendar
116+
print-calendar
117+
106118
'java.sql.Timestamp
107119
print-timestamp
108120

test/lambdaisland/deep_diff/printer_test.clj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
(:require [clojure.test :refer :all]
33
[lambdaisland.deep-diff.diff :as diff]
44
[lambdaisland.deep-diff.printer :as printer])
5-
(:import (java.sql Timestamp)))
5+
(:import (java.sql Timestamp)
6+
(java.util Date
7+
GregorianCalendar
8+
TimeZone)))
69

710
(defn- printed
811
[diff]
@@ -11,6 +14,11 @@
1114
(printer/format-doc printer)
1215
(printer/print-doc printer)))))
1316

17+
(defn- calendar
18+
[date]
19+
(doto (GregorianCalendar. (TimeZone/getTimeZone "GMT"))
20+
(.setTime date)))
21+
1422
(deftest print-doc-test
1523
(testing "date"
1624
(is (= "\u001B[31m-#inst \"2019-04-09T14:57:46.128-00:00\"\u001B[0m \u001B[32m+#inst \"2019-04-10T14:57:46.128-00:00\"\u001B[0m\n"
@@ -22,6 +30,10 @@
2230
(printed (diff/diff (Timestamp. 0)
2331
(doto (Timestamp. 1000) (.setNanos 101)))))))
2432

33+
(testing "calendar"
34+
(is (= "\u001B[31m-#inst \"1970-01-01T00:00:00.000+00:00\"\u001B[0m \u001B[32m+#inst \"1970-01-01T00:00:01.001+00:00\"\u001B[0m\n"
35+
(printed (diff/diff (calendar (Date. 0)) (calendar (Date. 1001)))))))
36+
2537
(testing "uuid"
2638
(is (= "\u001B[31m-#uuid \"e41b325a-ce9d-4fdd-b51d-280d9c91314d\"\u001B[0m \u001B[32m+#uuid \"0400be9a-619f-4c6a-a735-6245e4955995\"\u001B[0m\n"
2739
(printed (diff/diff #uuid "e41b325a-ce9d-4fdd-b51d-280d9c91314d"

0 commit comments

Comments
 (0)