Skip to content

Adding timezone information to datetimes from systemd-journal #100

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

Merged
merged 1 commit into from
Aug 13, 2022
Merged
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
6 changes: 3 additions & 3 deletions systemd/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def _convert_monotonic(m):
def _convert_source_monotonic(s):
return _datetime.timedelta(microseconds=int(s))

_LOCAL_TIMEZONE = _datetime.datetime.now().astimezone().tzinfo

def _convert_realtime(t):
return _datetime.datetime.fromtimestamp(t / 1000000)

return _datetime.datetime.fromtimestamp(t / 1000000, _LOCAL_TIMEZONE)

def _convert_timestamp(s):
return _datetime.datetime.fromtimestamp(int(s) / 1000000)
return _datetime.datetime.fromtimestamp(int(s) / 1000000, _LOCAL_TIMEZONE)


def _convert_trivial(x):
Expand Down
12 changes: 12 additions & 0 deletions systemd/test/test_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ def test_reader_convert_entry(tmpdir):
'x2' : ['YYY', 'YYY'],
'y2' : [b'\200\200', b'\200\201']}

def test_reader_convert_timestamps(tmpdir):
j = journal.Reader(path=tmpdir.strpath)

val = j._convert_field('_SOURCE_REALTIME_TIMESTAMP', 1641651559324187)
assert val.tzinfo is not None

val = j._convert_field('__REALTIME_TIMESTAMP', 1641651559324187)
assert val.tzinfo is not None

val = j._convert_field('COREDUMP_TIMESTAMP', 1641651559324187)
assert val.tzinfo is not None

def test_seek_realtime(tmpdir):
j = journal.Reader(path=tmpdir.strpath)

Expand Down