Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c77c110

Browse filesBrowse files
committed
Make datetime.time json serializable, to string ISO format.
1 parent 2d32537 commit c77c110
Copy full SHA for c77c110

File tree

Expand file treeCollapse file tree

3 files changed

+19
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-2
lines changed

‎CHANGES.rst

Copy file name to clipboardExpand all lines: CHANGES.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changes for crate
44

55
Unreleased
66
==========
7+
- Make ``datetime.time`` json serializable.
78

89
2025/01/30 2.0.0
910
================

‎src/crate/client/http.py

Copy file name to clipboardExpand all lines: src/crate/client/http.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

‎tests/client/test_http.py

Copy file name to clipboardExpand all lines: tests/client/test_http.py
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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
2222
import datetime as dt
2323
import json
2424
import 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
"""

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.