File tree 3 files changed +17
-1
lines changed
3 files changed +17
-1
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
-
22
21
import datetime as dt
23
22
import json
24
23
import multiprocessing
@@ -354,6 +353,18 @@ def test_uuid_serialization(self, request):
354
353
self .assertEqual (data ["args" ], [str (uid )])
355
354
client .close ()
356
355
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
+
357
368
@patch (REQUEST , fake_request (duplicate_key_exception ()))
358
369
def test_duplicate_key_error (self ):
359
370
"""
You can’t perform that action at this time.
0 commit comments