mosquito / cysystemd

systemd wrapper on Cython
Apache License 2.0
101 stars 21 forks source link

Journal Reader reports date with incorrect timezone. #60

Closed alex-zywicki closed 9 months ago

alex-zywicki commented 11 months ago

I have an application that uses cysystemd.JournalReader to read and report systemd log entries. I noticed that the timestamps were off by 4 hours (I am GMT-4).

After looking into the issue we concluded that changing our usage from

entry.date.timestamp()

to

entry.date.replace(tzinfo=timezone.utc).timestamp()

resolved the issue.

I did some reading into the cysystemd code and noticed that you attempt to correct for this here https://github.com/mosquito/cysystemd/blob/master/cysystemd/reader.pyx#L248-L249 But do so unsuccessfully because datetime.replace returns a new object that you do not capture.

The line in question could be updated to date = datetime.fromtimestamp(self.get_realtime_sec(), timezone.utc) to resolve the issue. Or the two liner version would be

        date = datetime.utcfromtimestamp(self.get_realtime_sec())
        date = date.replace(tzinfo=timezone.utc)