diff options
author | Samuel BF <36460-samuelbf@users.noreply.framagit.org> | 2022-01-08 15:32:47 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-08-13 19:34:51 +0200 |
commit | f868a56b935b6152d611b22f7a5538f14dafb194 (patch) | |
tree | f3acf5c90329dff05ba06755f972e59319fb3148 | |
parent | 1abac3cfec02c71b25b47c6a1178d80b258d742d (diff) | |
download | python-systemd-f868a56b935b6152d611b22f7a5538f14dafb194.tar.gz |
Adding timezone information to datetimes from systemd-journal
-rw-r--r-- | systemd/journal.py | 6 | ||||
-rw-r--r-- | systemd/test/test_journal.py | 12 |
2 files changed, 15 insertions, 3 deletions
diff --git a/systemd/journal.py b/systemd/journal.py index 54e54ca..db4701b 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -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): diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py index 2f3cd3b..e6761ca 100644 --- a/systemd/test/test_journal.py +++ b/systemd/test/test_journal.py @@ -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) |