Skip to content

Commit 8932543

Browse files
author
gabriel
committed
support deltatime
1 parent 7b4340d commit 8932543

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

jupyter_client/jsonutil.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import warnings
1010
from binascii import b2a_base64
1111
from collections.abc import Iterable
12-
from datetime import date, datetime
12+
from datetime import date, datetime, timedelta
1313
from typing import Any, Union
1414

1515
from dateutil.parser import isoparse as _dateutil_parse
@@ -126,6 +126,9 @@ def json_default(obj: Any) -> Any:
126126
if isinstance(obj, numbers.Real):
127127
return float(obj)
128128

129+
if isinstance(obj, timedelta):
130+
return str(obj)
131+
129132
raise TypeError("%r is not JSON serializable" % obj)
130133

131134

@@ -192,5 +195,8 @@ def json_clean(obj: Any) -> Any:
192195
if isinstance(obj, datetime | date):
193196
return obj.strftime(ISO8601)
194197

198+
if isinstance(obj, timedelta):
199+
return str(obj)
200+
195201
# we don't understand it, it's probably an unserializable object
196202
raise ValueError("Can't clean for JSON: %r" % obj)

tests/test_jsonutil.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def test_json_default():
180180
(MyFloat(), 3.14),
181181
(MyInt(), 389),
182182
(datetime.date(2025, 4, 8), "2025-04-08"),
183+
(datetime.timedelta(days=2, minutes=1, seconds=10), "2 days, 0:01:10"),
183184
]
184185

185186
for val, jval in pairs:

0 commit comments

Comments
 (0)