Skip to content

Make datetime.time json serializable, to string ISO format. #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changes for crate

Unreleased
==========
- Make ``datetime.time`` json serializable.

2025/01/30 2.0.0
================
Expand Down
4 changes: 4 additions & 0 deletions src/crate/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def json_encoder(obj: t.Any) -> t.Union[int, str]:
- Python's `dt.datetime` and `dt.date` types will be
serialized to `int` after converting to milliseconds
since epoch.
- Python's `dt.time` will be serialized to `str`, following
the ISO format.

https://github.com./ijl/orjson#default
https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#type-timestamp
Expand All @@ -113,6 +115,8 @@ def json_encoder(obj: t.Any) -> t.Union[int, str]:
delta.microseconds / 1000.0
+ (delta.seconds + delta.days * 24 * 3600) * 1000.0
)
if isinstance(obj, dt.time):
return obj.isoformat()
if isinstance(obj, dt.date):
return calendar.timegm(obj.timetuple()) * 1000
raise TypeError
Expand Down
13 changes: 12 additions & 1 deletion tests/client/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.

import datetime as dt
import json
import multiprocessing
Expand Down Expand Up @@ -354,6 +353,18 @@ def test_uuid_serialization(self, request):
self.assertEqual(data["args"], [str(uid)])
client.close()

@patch(REQUEST, autospec=True)
def test_time_serialization(self, request):
client = Client(servers="localhost:4200")
request.return_value = fake_response(200)

obj = dt.datetime.now().time()
client.sql("insert into my_table (str_col) values (?)", (obj,))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really testing that the time format is correct and understood by CrateDB as it will just take the given string and stores it.

Suggested change
client.sql("insert into my_table (str_col) values (?)", (obj,))
client.sql("SELECT ?::TIMETZ AS t_tz", (obj,))

Additionally, this would test the decoding of a TIMETZ type.


data = json.loads(request.call_args[1]["data"])
self.assertEqual(data["args"], [str(obj)])
client.close()

@patch(REQUEST, fake_request(duplicate_key_exception()))
def test_duplicate_key_error(self):
"""
Expand Down