Skip to content

Commit 4904958

Browse files
committed
revised PCAP Frame timestamp making process
1 parent 468bbcf commit 4904958

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
"IPDFF",
147147
"ipid",
148148
"ipsuite",
149+
"irat",
149150
"ISDN",
150151
"isidentifier",
151152
"ivar",

pcapkit/protocols/misc/pcap/frame.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from pcapkit.utilities.warnings import RegistryWarning, warn
3939

4040
if TYPE_CHECKING:
41+
from datetime import datetime as dt_type
4142
from decimal import Decimal
4243
from typing import IO, Any, DefaultDict, Optional, Type
4344

@@ -212,7 +213,8 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True, **kwargs
212213
_epch = _tsss + decimal.Decimal(_tsus) / 1_000_000_000
213214
else:
214215
_epch = _tsss + decimal.Decimal(_tsus) / 1_000_000
215-
_time = datetime.datetime.fromtimestamp(float(_epch))
216+
_irat = _epch.as_integer_ratio()
217+
_time = datetime.datetime.fromtimestamp(_irat[0] / _irat[1])
216218

217219
frame = Data_Frame(
218220
frame_info=Data_FrameInfo(
@@ -251,7 +253,7 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True, **kwargs
251253
return self._decode_next_layer(frame, self._ghdr.network, frame.len)
252254

253255
def make(self,
254-
timestamp: 'Optional[float | Decimal]' = None,
256+
timestamp: 'Optional[float | Decimal | int | dt_type]' = None,
255257
ts_sec: 'Optional[int]' = None,
256258
ts_usec: 'Optional[int]' = None,
257259
incl_len: 'Optional[int]' = None,
@@ -387,7 +389,7 @@ def _make_data(cls, data: 'Data_Frame') -> 'dict[str, Any]': # type: ignore[ove
387389
'packet': cls._make_payload(data),
388390
}
389391

390-
def _make_timestamp(self, timestamp: 'Optional[float | Decimal]' = None, ts_sec: 'Optional[int]' = None,
392+
def _make_timestamp(self, timestamp: 'Optional[float | Decimal | dt_type | int]' = None, ts_sec: 'Optional[int]' = None,
391393
ts_usec: 'Optional[int]' = None, nanosecond: 'bool' = False) -> 'tuple[int, int]':
392394
"""Make timestamp.
393395
@@ -407,13 +409,15 @@ def _make_timestamp(self, timestamp: 'Optional[float | Decimal]' = None, ts_sec:
407409
else:
408410
timestamp = decimal.Decimal(time.time())
409411
else:
412+
if isinstance(timestamp, datetime.datetime):
413+
timestamp = timestamp.timestamp()
410414
timestamp = decimal.Decimal(timestamp)
411415

412416
if ts_sec is None:
413417
ts_sec = int(timestamp)
414418

415419
if ts_usec is None:
416-
ts_usec = int(timestamp - ts_sec) * (1_000_000_000 if nanosecond else 1_000_000)
420+
ts_usec = int((timestamp - ts_sec) * (1_000_000_000 if nanosecond else 1_000_000))
417421

418422
return ts_sec, ts_usec
419423

0 commit comments

Comments
 (0)