Skip to content

Commit f868a56

Browse files
Samuel BFkeszybz
authored andcommitted
Adding timezone information to datetimes from systemd-journal
1 parent 1abac3c commit f868a56

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

systemd/journal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def _convert_monotonic(m):
5151
def _convert_source_monotonic(s):
5252
return _datetime.timedelta(microseconds=int(s))
5353

54+
_LOCAL_TIMEZONE = _datetime.datetime.now().astimezone().tzinfo
5455

5556
def _convert_realtime(t):
56-
return _datetime.datetime.fromtimestamp(t / 1000000)
57-
57+
return _datetime.datetime.fromtimestamp(t / 1000000, _LOCAL_TIMEZONE)
5858

5959
def _convert_timestamp(s):
60-
return _datetime.datetime.fromtimestamp(int(s) / 1000000)
60+
return _datetime.datetime.fromtimestamp(int(s) / 1000000, _LOCAL_TIMEZONE)
6161

6262

6363
def _convert_trivial(x):

systemd/test/test_journal.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,18 @@ def test_reader_convert_entry(tmpdir):
290290
'x2' : ['YYY', 'YYY'],
291291
'y2' : [b'\200\200', b'\200\201']}
292292

293+
def test_reader_convert_timestamps(tmpdir):
294+
j = journal.Reader(path=tmpdir.strpath)
295+
296+
val = j._convert_field('_SOURCE_REALTIME_TIMESTAMP', 1641651559324187)
297+
assert val.tzinfo is not None
298+
299+
val = j._convert_field('__REALTIME_TIMESTAMP', 1641651559324187)
300+
assert val.tzinfo is not None
301+
302+
val = j._convert_field('COREDUMP_TIMESTAMP', 1641651559324187)
303+
assert val.tzinfo is not None
304+
293305
def test_seek_realtime(tmpdir):
294306
j = journal.Reader(path=tmpdir.strpath)
295307

0 commit comments

Comments
 (0)