diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8559844..72c71cb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,11 @@ Added ^^^^^ * ``Dumper.flush``. +Changed +^^^^^^^ +* ``ts_utcdatetime`` is now a tz aware datetime. ``ts_datetime`` is still naive as getting the local + timezone and its availability is still problematic in Python's standard library. + Fixed ^^^^^ * Don't crash if Npcap is not installed or fails to load. diff --git a/cypcap/_cypcap.pyx b/cypcap/_cypcap.pyx index ee1e91d..8721dc5 100644 --- a/cypcap/_cypcap.pyx +++ b/cypcap/_cypcap.pyx @@ -336,8 +336,8 @@ cdef class Pkthdr: @property def ts_utcdatetime(self) -> datetime: - """Timestamp as a naive UTC datetime.""" - return datetime.utcfromtimestamp(self.ts) + """Timestamp as a UTC aware datetime.""" + return datetime.fromtimestamp(self.ts, tz=timezone.utc) @ts_utcdatetime.setter def ts_utcdatetime(self, ts_utcdatetime: datetime): diff --git a/tests/test_cypcap.py b/tests/test_cypcap.py index 5f259d4..83bd3d5 100644 --- a/tests/test_cypcap.py +++ b/tests/test_cypcap.py @@ -1,6 +1,6 @@ import os import socket -from datetime import datetime +from datetime import datetime, timezone import copy import dpkt @@ -97,9 +97,9 @@ def test_pkthdr_ts_datetime(): def test_pkthdr_ts_utcdatetime(): pkthdr = cypcap.Pkthdr(PKTHDR_TS, PKTHDR_LEN, PKTHDR_LEN) - assert pkthdr.ts_utcdatetime == datetime.utcfromtimestamp(PKTHDR_TS) + assert pkthdr.ts_utcdatetime == datetime.fromtimestamp(PKTHDR_TS, timezone.utc) - now = datetime.now() + now = datetime.now(timezone.utc) pkthdr.ts_utcdatetime = now assert pkthdr.ts_utcdatetime == now