1717import datetime
1818import time
1919import logging
20- from typing import List , BinaryIO , Generator , Union
20+ from typing import List , BinaryIO , Generator , Union , Tuple , Optional , cast
2121
2222from ..message import Message
2323from ..util import len2dlc , dlc2len , channel2int
2424from ..typechecking import StringPathLike
2525from .generic import FileIOMessageWriter , MessageReader
2626
2727
28+ TSystemTime = Tuple [int , int , int , int , int , int , int , int ]
29+
30+
2831class BLFParseError (Exception ):
2932 """BLF file could not be parsed correctly."""
3033
@@ -97,11 +100,11 @@ class BLFParseError(Exception):
97100TIME_ONE_NANS = 0x00000002
98101
99102
100- def timestamp_to_systemtime (timestamp ) :
103+ def timestamp_to_systemtime (timestamp : float ) -> TSystemTime :
101104 if timestamp is None or timestamp < 631152000 :
102105 # Probably not a Unix timestamp
103- return ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
104- t = datetime .datetime .fromtimestamp (timestamp )
106+ return 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
107+ t = datetime .datetime .fromtimestamp (round ( timestamp , 3 ) )
105108 return (
106109 t .year ,
107110 t .month ,
@@ -110,11 +113,11 @@ def timestamp_to_systemtime(timestamp):
110113 t .hour ,
111114 t .minute ,
112115 t .second ,
113- int ( round ( t .microsecond / 1000.0 )) ,
116+ t .microsecond // 1000 ,
114117 )
115118
116119
117- def systemtime_to_timestamp (systemtime ) :
120+ def systemtime_to_timestamp (systemtime : TSystemTime ) -> float :
118121 try :
119122 t = datetime .datetime (
120123 systemtime [0 ],
@@ -125,7 +128,7 @@ def systemtime_to_timestamp(systemtime):
125128 systemtime [6 ],
126129 systemtime [7 ] * 1000 ,
127130 )
128- return time . mktime ( t . timetuple ()) + systemtime [ 7 ] / 1000.0
131+ return t . timestamp ()
129132 except ValueError :
130133 return 0
131134
@@ -154,8 +157,8 @@ def __init__(self, file: Union[StringPathLike, BinaryIO]) -> None:
154157 self .file_size = header [10 ]
155158 self .uncompressed_size = header [11 ]
156159 self .object_count = header [12 ]
157- self .start_timestamp = systemtime_to_timestamp (header [14 :22 ])
158- self .stop_timestamp = systemtime_to_timestamp (header [22 :30 ])
160+ self .start_timestamp = systemtime_to_timestamp (cast ( TSystemTime , header [14 :22 ]) )
161+ self .stop_timestamp = systemtime_to_timestamp (cast ( TSystemTime , header [22 :30 ]) )
159162 # Read rest of header
160163 self .file .read (header [1 ] - FILE_HEADER_STRUCT .size )
161164 self ._tail = b""
@@ -405,8 +408,12 @@ def __init__(
405408 raise BLFParseError ("Unexpected file format" )
406409 self .uncompressed_size = header [11 ]
407410 self .object_count = header [12 ]
408- self .start_timestamp = systemtime_to_timestamp (header [14 :22 ])
409- self .stop_timestamp = systemtime_to_timestamp (header [22 :30 ])
411+ self .start_timestamp : Optional [float ] = systemtime_to_timestamp (
412+ cast (TSystemTime , header [14 :22 ])
413+ )
414+ self .stop_timestamp : Optional [float ] = systemtime_to_timestamp (
415+ cast (TSystemTime , header [22 :30 ])
416+ )
410417 # Jump to the end of the file
411418 self .file .seek (0 , 2 )
412419 else :
0 commit comments