Skip to content

Commit 9d6c5fd

Browse files
committed
json: handle timedelta type
1 parent c2a5e54 commit 9d6c5fd

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

journal_brief/format/json.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def format(self, entry):
4747
elif isinstance(value, journal.Monotonic):
4848
log.debug("Converting %s", field)
4949
value = value.timestamp.microseconds
50+
elif isinstance(value, datetime.timedelta):
51+
log.debug("Converting %s", field)
52+
value = value.total_seconds() * 1000000 # microseconds
5053
elif isinstance(value, bytes):
5154
log.debug("Converting %s", field)
5255
try:

tests/format/test_json.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ def test_monotonic(self):
6666
out = json.loads(formatter.format(entry))
6767
assert out['__MONOTONIC_TIMESTAMP'] == us
6868

69+
def test_timedelta(self):
70+
"""
71+
Should be in microseconds
72+
"""
73+
74+
sec = 1
75+
us = 700
76+
mono_ts = timedelta(0, sec, us)
77+
entry = {'__MONOTONIC_TIMESTAMP': mono_ts}
78+
formatter = get_formatter('json')
79+
out = json.loads(formatter.format(entry))
80+
diff = out['__MONOTONIC_TIMESTAMP'] - (sec * 1000000 + us)
81+
assert diff < 0.001
82+
6983
@pytest.mark.parametrize(('bdata', 'brepr'), [
7084
(b'abc', 'abc'),
7185
(b'\x82\xac', [0x82, 0xac]),

0 commit comments

Comments
 (0)