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

Conversation

Samuel-BF
Copy link

Currently, converters for timestamp fields (__REALTIME_TIMESTAMP, _SOURCE_REALTIME_TIMESTAMP, COREDUMPTIMESTAMP) produce datetimes object without timezone, losing information.

This PR fixes that.

Without this commit :

>>> from systemd import journal
>>> j = journal.Reader(flags = journal.CURRENT_USER)
>>> j.seek_tail()
>>> j.get_previous().isoformat()
>>> j.get_previous()['__REALTIME_TIMESTAMP'].isoformat()
'2022-01-08T20:44:23.000494'

And with this commit :

>>> j.get_previous()['__REALTIME_TIMESTAMP'].isoformat()
'2022-01-08T20:44:23.000494+01:00'

Reference: datetime.fromtimestamp :

If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

@keszybz
Copy link
Member

keszybz commented Aug 13, 2022

Sorry for the delay.

I'm looking at this, initially I wasn't sure if it's appropriate to attach the local time zone. After all, the timestamp that is stored in the journal is a timezone-less absolute timestamp. If anything, it'd be fair to say that it's UTC. But datetime.datetime.fromtimestamp does convert the timestamp to a local time, so, indeed, the resulting timestamp is in the local time zone. This begs the question why datetime.datetime.fromtimestamp does the conversion in this way: i.e. it actually selects a specific timezone to do the conversion, but then discards this information. I think it's a bug in datetime.datetime.fromtimestamp. But as far as a I know python, it'd take ages to get it fixed there, so applying this workaround here is appropriate.

@keszybz keszybz merged commit f868a56 into systemd:main Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants