File tree Expand file tree Collapse file tree 3 files changed +19
-2
lines changed Expand file tree Collapse file tree 3 files changed +19
-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-
21+ import datetime
2222import datetime as dt
2323import json
2424import multiprocessing
@@ -164,7 +164,7 @@ def test_http_error_is_re_raised(self, request):
164164
165165 @patch (REQUEST )
166166 def test_programming_error_contains_http_error_response_content (
167- self , request
167+ self , request
168168 ):
169169 request .side_effect = Exception ("this shouldn't be raised" )
170170
@@ -354,6 +354,18 @@ def test_uuid_serialization(self, request):
354354 self .assertEqual (data ["args" ], [str (uid )])
355355 client .close ()
356356
357+ @patch (REQUEST , autospec = True )
358+ def test_time_serialization (self , request ):
359+ client = Client (servers = "localhost:4200" )
360+ request .return_value = fake_response (200 )
361+
362+ obj = datetime .datetime .now ().time ()
363+ client .sql ("insert into my_table (str_col) values (?)" , (obj ,))
364+
365+ data = json .loads (request .call_args [1 ]["data" ])
366+ self .assertEqual (data ["args" ], [str (obj )])
367+ client .close ()
368+
357369 @patch (REQUEST , fake_request (duplicate_key_exception ()))
358370 def test_duplicate_key_error (self ):
359371 """
You can’t perform that action at this time.
0 commit comments