diff --git a/can/io/asc.py b/can/io/asc.py index deb7d429e..56f5b4338 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -371,8 +371,8 @@ def __init__( self.channel = channel # write start of file header - start_time = self._format_header_datetime(datetime.now()) - self.file.write(f"date {start_time}\n") + self.start_time = self._format_header_datetime(datetime.now()) + self.file.write(f"date {self.start_time}\n") self.file.write("base hex timestamps absolute\n") self.file.write("internal events logged\n") @@ -393,6 +393,14 @@ def _format_header_datetime(self, dt: datetime) -> str: def stop(self) -> None: # This is guaranteed to not be None since we raise ValueError in __init__ if not self.file.closed: + if self.started is not None: + formatted_date = self._format_header_datetime( + datetime.fromtimestamp(self.started) + ) + self.file.seek(0) + self.file.write(f"date {formatted_date}\n") + else: + logger.warning("No messages logged; 'started' timestamp is None.") self.file.write("End TriggerBlock\n") super().stop()