Skip to content

Commit cff0581

Browse files
committed
fix(asc): fix relative timestamp roundtrip for 3+ messages
ASCReader was treating all timestamps as cumulative offsets from start_time, ignoring the timestamps_format value. When reading a file written with timestamps_format="relative" (per-event deltas) and relative_timestamp=False, the reader now accumulates deltas into start_time instead of adding each delta independently. Without this fix, a 3-message roundtrip would produce: msg3: 0.7 + 100.0 = 100.7 (wrong, expected 101.0) Also strengthen test_write_relative_timestamp_roundtrip to use 3 messages, exposing the bug that was masked by the 2-message case.
1 parent 4fe6c13 commit cff0581

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

can/io/asc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ def __iter__(self) -> Generator[Message, None, None]:
295295
msg_kwargs: dict[str, float | bool | int] = {}
296296
try:
297297
_timestamp, channel, rest_of_message = line.split(None, 2)
298-
timestamp = float(_timestamp) + self.start_time
298+
if self.timestamps_format == "relative" and not self.relative_timestamp:
299+
self.start_time += float(_timestamp)
300+
timestamp = self.start_time
301+
else:
302+
timestamp = float(_timestamp) + self.start_time
299303
msg_kwargs["timestamp"] = timestamp
300304
if channel == "CANFD":
301305
msg_kwargs["is_fd"] = True

test/logformats_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ def test_write_relative_timestamp_roundtrip(self):
707707
"""Messages written with relative format round-trip to their original timestamps."""
708708
msgs = [
709709
can.Message(timestamp=100.0, arbitration_id=0x1, data=b"\x01"),
710-
can.Message(timestamp=100.5, arbitration_id=0x2, data=b"\x02"),
710+
can.Message(timestamp=100.3, arbitration_id=0x2, data=b"\x02"),
711+
can.Message(timestamp=101.0, arbitration_id=0x3, data=b"\x03"),
711712
]
712713

713714
with can.ASCWriter(self.test_file_name, timestamps_format="relative") as writer:
@@ -719,7 +720,8 @@ def test_write_relative_timestamp_roundtrip(self):
719720

720721
self.assertEqual(len(result), len(msgs))
721722
self.assertAlmostEqual(result[0].timestamp, 100.0, places=3)
722-
self.assertAlmostEqual(result[1].timestamp, 100.5, places=3)
723+
self.assertAlmostEqual(result[1].timestamp, 100.3, places=3)
724+
self.assertAlmostEqual(result[2].timestamp, 101.0, places=3)
723725

724726
def test_write_relative_timestamps_are_per_event_deltas(self):
725727
"""With timestamps_format='relative', each written timestamp is a delta from the

0 commit comments

Comments
 (0)