Skip to content

Commit 319a9f2

Browse files
authored
Fix timestamp offset bug in BLFWriter (#1921)
Fix timestamp offset bug in BLFWriter In the BLF files, the frame timestamp are stored as a delta from the "start time". The "start time" stored in the file is the first frame timestamp rounded to 3 decimals. When we calculate the timestamp delta for each frame, we were previously calculating it using the non-rounded "start time". This new code, use the "start time" as the first frame timestamp truncated to 3 decimals.
1 parent 6c9d59c commit 319a9f2

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

can/io/blf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,10 @@ def _add_object(self, obj_type, data, timestamp=None):
530530
if timestamp is None:
531531
timestamp = self.stop_timestamp or time.time()
532532
if self.start_timestamp is None:
533-
self.start_timestamp = timestamp
533+
# Save start timestamp using the same precision as the BLF format
534+
# Truncating to milliseconds to avoid rounding errors when calculating
535+
# the timestamp difference
536+
self.start_timestamp = int(timestamp * 1000) / 1000
534537
self.stop_timestamp = timestamp
535538
timestamp = int((timestamp - self.start_timestamp) * 1e9)
536539
header_size = OBJ_HEADER_BASE_STRUCT.size + OBJ_HEADER_V1_STRUCT.size

test/data/example_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def sort_messages(messages):
160160

161161
TEST_MESSAGES_ERROR_FRAMES = sort_messages(
162162
[
163-
Message(is_error_frame=True),
163+
Message(is_error_frame=True, timestamp=TEST_TIME),
164164
Message(is_error_frame=True, timestamp=TEST_TIME + 0.170),
165165
Message(is_error_frame=True, timestamp=TEST_TIME + 17.157),
166166
]

0 commit comments

Comments
 (0)