File tree 3 files changed +19
-2
lines changed
3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ Changes for crate
4
4
5
5
Unreleased
6
6
==========
7
+ - Make ``datetime.time `` json serializable.
7
8
8
9
2025/01/30 2.0.0
9
10
================
Original file line number Diff line number Diff line change @@ -98,6 +98,8 @@ def json_encoder(obj: t.Any) -> t.Union[int, str]:
98
98
- Python's `dt.datetime` and `dt.date` types will be
99
99
serialized to `int` after converting to milliseconds
100
100
since epoch.
101
+ - Python's `dt.time` will be serialized to `str`, following
102
+ the ISO format.
101
103
102
104
https://github.com./ijl/orjson#default
103
105
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]:
113
115
delta .microseconds / 1000.0
114
116
+ (delta .seconds + delta .days * 24 * 3600 ) * 1000.0
115
117
)
118
+ if isinstance (obj , dt .time ):
119
+ return obj .isoformat ()
116
120
if isinstance (obj , dt .date ):
117
121
return calendar .timegm (obj .timetuple ()) * 1000
118
122
raise TypeError
Original file line number Diff line number Diff line change 18
18
# However, if you have executed another commercial license agreement
19
19
# with Crate these terms will supersede the license and you may use the
20
20
# software solely pursuant to the terms of the relevant commercial agreement.
21
-
21
+ import datetime
22
22
import datetime as dt
23
23
import json
24
24
import multiprocessing
@@ -164,7 +164,7 @@ def test_http_error_is_re_raised(self, request):
164
164
165
165
@patch (REQUEST )
166
166
def test_programming_error_contains_http_error_response_content (
167
- self , request
167
+ self , request
168
168
):
169
169
request .side_effect = Exception ("this shouldn't be raised" )
170
170
@@ -354,6 +354,18 @@ def test_uuid_serialization(self, request):
354
354
self .assertEqual (data ["args" ], [str (uid )])
355
355
client .close ()
356
356
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
+
357
369
@patch (REQUEST , fake_request (duplicate_key_exception ()))
358
370
def test_duplicate_key_error (self ):
359
371
"""
You can’t perform that action at this time.
0 commit comments