Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/aware-utc-datetime'
Browse files Browse the repository at this point in the history
  • Loading branch information
segevfiner committed Apr 17, 2024
2 parents 77d5c7f + 79e75f9 commit 6c79e18
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cypcap/_cypcap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cypcap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import socket
from datetime import datetime
from datetime import datetime, timezone
import copy

import dpkt
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 6c79e18

Please sign in to comment.