Skip to content

Commit

Permalink
Fix seek_realtime to work with timezone aware date
Browse files Browse the repository at this point in the history
`strftime("%s")` is not in the official python documentation but in my system (ubuntu 18.04 python 3.6.9) it is not aware of the object timezone and will return the wrong value if the timezone is specified and is not the system local one.

There are multiple ways to ensure a python `datetime.datetime` is in local timezone, the easiest (with python 3.3+) is to call `.astimezone()` If one wants to support earlier versions of python an extra dependency might be needed like `dateutil.tz.tzlocal()`.
  • Loading branch information
leolchat authored and keszybz committed Nov 12, 2020
1 parent d08f8dd commit 4c9a241
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion systemd/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def seek_realtime(self, realtime):
>>> j.seek_realtime(yesterday)
"""
if isinstance(realtime, _datetime.datetime):
realtime = int(float(realtime.strftime("%s.%f")) * 1000000)
realtime = int(float(realtime.astimezone().strftime("%s.%f")) * 1000000)
elif not isinstance(realtime, int):
realtime = int(realtime * 1000000)
return super(Reader, self).seek_realtime(realtime)
Expand Down

0 comments on commit 4c9a241

Please sign in to comment.