File tree Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ Changes for crate
44
55Unreleased
66==========
7+ - Make ``datetime.time `` json serializable.
78
892025/01/30 2.0.0
910================
Original file line number Diff line number Diff line change @@ -98,6 +98,8 @@ def json_encoder(obj: t.Any) -> t.Union[int, str]:
9898 - Python's `dt.datetime` and `dt.date` types will be
9999 serialized to `int` after converting to milliseconds
100100 since epoch.
101+ - Python's `dt.time` will be serialized to `str`, following
102+ the ISO format.
101103
102104 https://github.com/ijl/orjson#default
103105 https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#type-timestamp
@@ -113,6 +115,8 @@ def json_encoder(obj: t.Any) -> t.Union[int, str]:
113115 delta .microseconds / 1000.0
114116 + (delta .seconds + delta .days * 24 * 3600 ) * 1000.0
115117 )
118+ if isinstance (obj , dt .time ):
119+ return obj .isoformat ()
116120 if isinstance (obj , dt .date ):
117121 return calendar .timegm (obj .timetuple ()) * 1000
118122 raise TypeError
Original file line number Diff line number Diff line change 1818# However, if you have executed another commercial license agreement
1919# with Crate these terms will supersede the license and you may use the
2020# software solely pursuant to the terms of the relevant commercial agreement.
21-
2221import datetime as dt
2322import json
2423import multiprocessing
@@ -164,7 +163,7 @@ def test_http_error_is_re_raised(self, request):
164163
165164 @patch (REQUEST )
166165 def test_programming_error_contains_http_error_response_content (
167- self , request
166+ self , request
168167 ):
169168 request .side_effect = Exception ("this shouldn't be raised" )
170169
@@ -354,6 +353,18 @@ def test_uuid_serialization(self, request):
354353 self .assertEqual (data ["args" ], [str (uid )])
355354 client .close ()
356355
356+ @patch (REQUEST , autospec = True )
357+ def test_time_serialization (self , request ):
358+ client = Client (servers = "localhost:4200" )
359+ request .return_value = fake_response (200 )
360+
361+ obj = dt .datetime .now ().time ()
362+ client .sql ("insert into my_table (str_col) values (?)" , (obj ,))
363+
364+ data = json .loads (request .call_args [1 ]["data" ])
365+ self .assertEqual (data ["args" ], [str (obj )])
366+ client .close ()
367+
357368 @patch (REQUEST , fake_request (duplicate_key_exception ()))
358369 def test_duplicate_key_error (self ):
359370 """
You can’t perform that action at this time.
0 commit comments