Skip to content

Commit ea98114

Browse files
committed
Print java.sql.Timestamps as #inst literals
1 parent 1e115f9 commit ea98114

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/lambdaisland/deep_diff/printer.clj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[arrangement.core]
88
[lambdaisland.deep-diff.diff :as diff])
99
(:import (java.text SimpleDateFormat)
10-
(java.util TimeZone)))
10+
(java.util TimeZone)
11+
(java.sql Timestamp)))
1112

1213
(defn print-deletion [printer expr]
1314
(let [no-color (assoc printer :print-color false)]
@@ -47,6 +48,18 @@
4748
'inst
4849
#(.format ^SimpleDateFormat (.get thread-local-utc-date-format) %)))
4950

51+
(def ^:private ^ThreadLocal thread-local-utc-timestamp-format
52+
(proxy [ThreadLocal] []
53+
(initialValue []
54+
(doto (SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss")
55+
(.setTimeZone (TimeZone/getTimeZone "GMT"))))))
56+
57+
(def ^:private print-timestamp
58+
(puget/tagged-handler
59+
'inst
60+
#(str (.format ^SimpleDateFormat (.get thread-local-utc-timestamp-format) %)
61+
(format ".%09d-00:00" (.getNanos ^Timestamp %)))))
62+
5063
(def ^:private print-handlers
5164
{'lambdaisland.deep_diff.diff.Deletion
5265
print-deletion
@@ -90,6 +103,9 @@
90103
'java.util.Date
91104
print-date
92105

106+
'java.sql.Timestamp
107+
print-timestamp
108+
93109
'java.util.UUID
94110
(puget/tagged-handler 'uuid str)})
95111

test/lambdaisland/deep_diff/printer_test.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns lambdaisland.deep-diff.printer-test
22
(:require [clojure.test :refer :all]
33
[lambdaisland.deep-diff.diff :as diff]
4-
[lambdaisland.deep-diff.printer :as printer]))
4+
[lambdaisland.deep-diff.printer :as printer])
5+
(:import (java.sql Timestamp)))
56

67
(defn- printed
78
[diff]
@@ -15,6 +16,12 @@
1516
(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"
1617
(printed (diff/diff #inst "2019-04-09T14:57:46.128-00:00"
1718
#inst "2019-04-10T14:57:46.128-00:00")))))
19+
20+
(testing "timestamp"
21+
(is (= "\u001B[31m-#inst \"1970-01-01T00:00:00.000000000-00:00\"\u001B[0m \u001B[32m+#inst \"1970-01-01T00:00:01.000000101-00:00\"\u001B[0m\n"
22+
(printed (diff/diff (Timestamp. 0)
23+
(doto (Timestamp. 1000) (.setNanos 101)))))))
24+
1825
(testing "uuid"
1926
(is (= "\u001B[31m-#uuid \"e41b325a-ce9d-4fdd-b51d-280d9c91314d\"\u001B[0m \u001B[32m+#uuid \"0400be9a-619f-4c6a-a735-6245e4955995\"\u001B[0m\n"
2027
(printed (diff/diff #uuid "e41b325a-ce9d-4fdd-b51d-280d9c91314d"

0 commit comments

Comments
 (0)