Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit 4c655a0

Browse files
author
Romain Dartigues
committed
ISO format all date/time objects by default
* the `datefmt` parameter is intended to be used by logging.Formatter.formatTime and not for the serialization * seconds are lost in the serialization of time and datetimes instances
1 parent 679474d commit 4c655a0

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
lines changed

src/pythonjsonlogger/jsonlogger.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,8 @@ def __init__(self, *args, **kwargs):
6767
if not self.json_encoder and not self.json_default:
6868
def _default_json_handler(obj):
6969
'''Prints dates in ISO format'''
70-
if isinstance(obj, datetime.datetime):
71-
if obj.year < 1900:
72-
# strftime do not work with date < 1900
73-
return obj.isoformat()
74-
return obj.strftime(self.datefmt or '%Y-%m-%dT%H:%M')
75-
elif isinstance(obj, datetime.date):
70+
if isinstance(obj, (datetime.date, datetime.time)):
7671
return obj.isoformat()
77-
elif isinstance(obj, datetime.time):
78-
return obj.strftime('%H:%M')
7972
elif istraceback(obj):
8073
tb = ''.join(traceback.format_tb(obj))
8174
return tb.strip()

tests/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ def testJsonDefaultEncoder(self):
123123
"otherdatetimeagain": datetime.datetime(1900, 1, 1)}
124124
self.logger.info(msg)
125125
logJson = json.loads(self.buffer.getvalue())
126-
self.assertEqual(logJson.get("adate"), "1999-12-31T23:59")
126+
self.assertEqual(logJson.get("adate"), "1999-12-31T23:59:00")
127127
self.assertEqual(logJson.get("otherdate"), "1789-07-14")
128128
self.assertEqual(logJson.get("otherdatetime"), "1789-07-14T23:59:00")
129129
self.assertEqual(logJson.get("otherdatetimeagain"),
130-
"1900-01-01T00:00")
130+
"1900-01-01T00:00:00")
131131

132132
def testJsonCustomDefault(self):
133133
def custom(o):

0 commit comments

Comments
 (0)