Skip to content

Commit afa20e7

Browse files
committed
revised PCAP Frame timestamp handling
1 parent 383bc32 commit afa20e7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

pcapkit/protocols/misc/pcap/frame.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True, **kwargs
209209
_ilen = schema.incl_len
210210
_olen = schema.orig_len
211211

212-
if self._nsec:
213-
_epch = _tsss + decimal.Decimal(_tsus) / 1_000_000_000
214-
else:
215-
_epch = _tsss + decimal.Decimal(_tsus) / 1_000_000
212+
with decimal.localcontext(prec=64):
213+
if self._nsec:
214+
_epch = _tsss + decimal.Decimal(_tsus) / 1_000_000_000
215+
else:
216+
_epch = _tsss + decimal.Decimal(_tsus) / 1_000_000
216217
_irat = _epch.as_integer_ratio()
217218
_time = datetime.datetime.fromtimestamp(_irat[0] / _irat[1])
218219

@@ -403,15 +404,16 @@ def _make_timestamp(self, timestamp: 'Optional[float | Decimal | dt_type | int]'
403404
Second and microsecond/nanosecond value of timestamp.
404405
405406
"""
406-
if timestamp is None:
407-
if py37 and nanosecond:
408-
timestamp = decimal.Decimal(time.time_ns()) / 1_000_000_000
407+
with decimal.localcontext(prec=64):
408+
if timestamp is None:
409+
if py37 and nanosecond:
410+
timestamp = decimal.Decimal(time.time_ns()) / 1_000_000_000
411+
else:
412+
timestamp = decimal.Decimal(time.time())
409413
else:
410-
timestamp = decimal.Decimal(time.time())
411-
else:
412-
if isinstance(timestamp, datetime.datetime):
413-
timestamp = timestamp.timestamp()
414-
timestamp = decimal.Decimal(timestamp)
414+
if isinstance(timestamp, datetime.datetime):
415+
timestamp = timestamp.timestamp()
416+
timestamp = decimal.Decimal(timestamp)
415417

416418
if ts_sec is None:
417419
ts_sec = int(timestamp)

0 commit comments

Comments
 (0)