Skip to content

Commit

Permalink
revised PCAP Frame timestamp making process
Browse files Browse the repository at this point in the history
  • Loading branch information
JarryShaw committed May 2, 2023
1 parent 468bbcf commit 4904958
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"IPDFF",
"ipid",
"ipsuite",
"irat",
"ISDN",
"isidentifier",
"ivar",
Expand Down
12 changes: 8 additions & 4 deletions pcapkit/protocols/misc/pcap/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from pcapkit.utilities.warnings import RegistryWarning, warn

if TYPE_CHECKING:
from datetime import datetime as dt_type
from decimal import Decimal
from typing import IO, Any, DefaultDict, Optional, Type

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

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

def make(self,
timestamp: 'Optional[float | Decimal]' = None,
timestamp: 'Optional[float | Decimal | int | dt_type]' = None,
ts_sec: 'Optional[int]' = None,
ts_usec: 'Optional[int]' = None,
incl_len: 'Optional[int]' = None,
Expand Down Expand Up @@ -387,7 +389,7 @@ def _make_data(cls, data: 'Data_Frame') -> 'dict[str, Any]': # type: ignore[ove
'packet': cls._make_payload(data),
}

def _make_timestamp(self, timestamp: 'Optional[float | Decimal]' = None, ts_sec: 'Optional[int]' = None,
def _make_timestamp(self, timestamp: 'Optional[float | Decimal | dt_type | int]' = None, ts_sec: 'Optional[int]' = None,
ts_usec: 'Optional[int]' = None, nanosecond: 'bool' = False) -> 'tuple[int, int]':
"""Make timestamp.
Expand All @@ -407,13 +409,15 @@ def _make_timestamp(self, timestamp: 'Optional[float | Decimal]' = None, ts_sec:
else:
timestamp = decimal.Decimal(time.time())
else:
if isinstance(timestamp, datetime.datetime):
timestamp = timestamp.timestamp()
timestamp = decimal.Decimal(timestamp)

if ts_sec is None:
ts_sec = int(timestamp)

if ts_usec is None:
ts_usec = int(timestamp - ts_sec) * (1_000_000_000 if nanosecond else 1_000_000)
ts_usec = int((timestamp - ts_sec) * (1_000_000_000 if nanosecond else 1_000_000))

return ts_sec, ts_usec

Expand Down

0 comments on commit 4904958

Please sign in to comment.