38
38
from pcapkit .utilities .warnings import RegistryWarning , warn
39
39
40
40
if TYPE_CHECKING :
41
+ from datetime import datetime as dt_type
41
42
from decimal import Decimal
42
43
from typing import IO , Any , DefaultDict , Optional , Type
43
44
@@ -212,7 +213,8 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True, **kwargs
212
213
_epch = _tsss + decimal .Decimal (_tsus ) / 1_000_000_000
213
214
else :
214
215
_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 ])
216
218
217
219
frame = Data_Frame (
218
220
frame_info = Data_FrameInfo (
@@ -251,7 +253,7 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True, **kwargs
251
253
return self ._decode_next_layer (frame , self ._ghdr .network , frame .len )
252
254
253
255
def make (self ,
254
- timestamp : 'Optional[float | Decimal]' = None ,
256
+ timestamp : 'Optional[float | Decimal | int | dt_type ]' = None ,
255
257
ts_sec : 'Optional[int]' = None ,
256
258
ts_usec : 'Optional[int]' = None ,
257
259
incl_len : 'Optional[int]' = None ,
@@ -387,7 +389,7 @@ def _make_data(cls, data: 'Data_Frame') -> 'dict[str, Any]': # type: ignore[ove
387
389
'packet' : cls ._make_payload (data ),
388
390
}
389
391
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 ,
391
393
ts_usec : 'Optional[int]' = None , nanosecond : 'bool' = False ) -> 'tuple[int, int]' :
392
394
"""Make timestamp.
393
395
@@ -407,13 +409,15 @@ def _make_timestamp(self, timestamp: 'Optional[float | Decimal]' = None, ts_sec:
407
409
else :
408
410
timestamp = decimal .Decimal (time .time ())
409
411
else :
412
+ if isinstance (timestamp , datetime .datetime ):
413
+ timestamp = timestamp .timestamp ()
410
414
timestamp = decimal .Decimal (timestamp )
411
415
412
416
if ts_sec is None :
413
417
ts_sec = int (timestamp )
414
418
415
419
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 ) )
417
421
418
422
return ts_sec , ts_usec
419
423
0 commit comments