Skip to content

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

2 participants