Skip to content

Commit 73afe11

Browse files
author
Tian-Jionglu
committed
Fix ASC Reader start time
to get correct `absolute` timestamp
1 parent d9b9a19 commit 73afe11

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

can/io/asc.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(
6969
# TODO - what is this used for? The ASC Writer only prints `absolute`
7070
self.timestamps_format: Optional[str] = None
7171
self.internal_events_logged = False
72+
self._extract_header()
7273

7374
def _extract_header(self) -> None:
7475
for _line in self.file:
@@ -110,9 +111,18 @@ def _extract_header(self) -> None:
110111

111112
if events_match:
112113
self.internal_events_logged = events_match.group("no_events") is None
113-
break
114+
continue
114115

115-
break
116+
if trigger_match := ASC_TRIGGER_REGEX.match(line):
117+
datetime_str = trigger_match.group("datetime_string")
118+
self.start_time = (
119+
0.0
120+
if self.timestamps_format == "relative"
121+
else self._datetime_to_timestamp(datetime_str)
122+
)
123+
# attribute `start_timestamp` for unified as BLFReader
124+
self.start_timestamp = self.start_time
125+
break
116126

117127
@staticmethod
118128
def _datetime_to_timestamp(datetime_string: str) -> float:
@@ -260,20 +270,10 @@ def _process_fd_can_frame(self, line: str, msg_kwargs: Dict[str, Any]) -> Messag
260270
return Message(**msg_kwargs)
261271

262272
def __iter__(self) -> Generator[Message, None, None]:
263-
self._extract_header()
264273

265274
for _line in self.file:
266275
line = _line.strip()
267276

268-
if trigger_match := ASC_TRIGGER_REGEX.match(line):
269-
datetime_str = trigger_match.group("datetime_string")
270-
self.start_time = (
271-
0.0
272-
if self.relative_timestamp
273-
else self._datetime_to_timestamp(datetime_str)
274-
)
275-
continue
276-
277277
if not ASC_MESSAGE_REGEX.match(line):
278278
# line might be a comment, chip status,
279279
# J1939 message or some other unsupported event
@@ -282,7 +282,7 @@ def __iter__(self) -> Generator[Message, None, None]:
282282
msg_kwargs: Dict[str, Union[float, bool, int]] = {}
283283
try:
284284
_timestamp, channel, rest_of_message = line.split(None, 2)
285-
timestamp = float(_timestamp) + self.start_time
285+
timestamp = float(_timestamp) + self.start_timestamp
286286
msg_kwargs["timestamp"] = timestamp
287287
if channel == "CANFD":
288288
msg_kwargs["is_fd"] = True

0 commit comments

Comments
 (0)