Commit 59b2e43
authored
Fix/asc writer timestamps format (#2032)
* feat(asc): add timestamps_format parameter to ASCWriter
Allow callers to choose between 'absolute' (default, existing behaviour)
and 'relative' when creating an ASC log file. The value is written into
the 'base hex timestamps ...' header line so that other tools
(CANalyzer, CANoe, etc.) can interpret the file correctly.
Closes #2022
* docs: add changelog fragment for #2022
* style: apply black formatting to logformats_test.py
* fix(asc): apply timestamps_format to actual written timestamps, not just header
Previously, log_event() always subtracted self.started from every timestamp
regardless of timestamps_format, meaning "relative" mode only changed the
header line while writing identical data to "absolute" mode.
Per the ASC format specification:
- "absolute": each timestamp is an offset from the start of measurement
- "relative": each timestamp is a delta from the preceding event
Fix log_event() to compute per-event deltas when timestamps_format="relative",
and update self.last_timestamp after each event so the next delta is correct.
Also add two tests that verify the actual values written to the file differ
between the two modes (3-message uneven spacing exposes the distinction at msg3:
absolute writes 1.0, relative writes 0.7).
Update changelog fragment to describe the semantic difference accurately.
* fix(asc): address PR review feedback on timestamps_format
Reviewer feedback received (zariiii9003):
1. Use Literal type hint for timestamps_format parameter
- Changed: timestamps_format: str = "absolute"
- To: timestamps_format: Literal["absolute", "relative"] = "absolute"
- Added Literal to typing imports
2. Simplify log_event timestamp computation
- Moved monotonic clamp out of if/else blocks:
timestamp = max(timestamp, self.last_timestamp)
- Each branch now contains only simple arithmetic:
absolute: written_timestamp = timestamp - self.started
relative: written_timestamp = timestamp - self.last_timestamp
3. Use relative_timestamp=False in roundtrip tests
- Updated test_write_relative_timestamp_roundtrip and
test_write_absolute_timestamps_are_offsets_from_start to use
relative_timestamp=False so assertions verify original message
timestamps are recovered (100.0, 100.3, 101.0) rather than
file-stored offsets (0.0, 0.3, 1.0)
Additional issues found and fixed during review:
4. Removed outdated TODO comment in ASCReader
- Removed: "TODO - what is this used for? The ASC Writer only prints
absolute" — no longer accurate since ASCWriter now supports both
"absolute" and "relative" formats
5. Lowered assertAlmostEqual precision from places=5 to places=3
- The datetime triggerblock roundtrip (fromtimestamp -> strftime ->
strptime -> timestamp) only preserves millisecond precision due to
the ".NNN" format. places=5 (5 microseconds) is stricter than what
the format can guarantee; places=3 (0.5 ms) correctly reflects the
actual precision limit. Verified empirically: sub-millisecond
timestamps incur ~0.456 ms error which passes places=3 but fails
places=5.
6. Updated docstrings for both modified roundtrip tests to accurately
describe the new assertion semantics (original timestamp recovery)
* 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.
* fix(asc): use private _last_timestamp instead of mutating start_time
Use self._last_timestamp to accumulate relative timestamps in ASCReader
so that self.start_time remains unchanged and safe for external access.1 parent 74201b9 commit 59b2e43
3 files changed
Lines changed: 130 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| |||
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
| 297 | + | |
297 | 298 | | |
298 | 299 | | |
299 | 300 | | |
| |||
309 | 310 | | |
310 | 311 | | |
311 | 312 | | |
312 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
313 | 318 | | |
314 | 319 | | |
315 | 320 | | |
| |||
372 | 377 | | |
373 | 378 | | |
374 | 379 | | |
| 380 | + | |
375 | 381 | | |
376 | 382 | | |
377 | 383 | | |
| |||
384 | 390 | | |
385 | 391 | | |
386 | 392 | | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
387 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
388 | 409 | | |
389 | 410 | | |
390 | 411 | | |
| |||
394 | 415 | | |
395 | 416 | | |
396 | 417 | | |
| 418 | + | |
397 | 419 | | |
398 | 420 | | |
399 | 421 | | |
400 | 422 | | |
401 | | - | |
| 423 | + | |
402 | 424 | | |
403 | 425 | | |
404 | 426 | | |
| |||
445 | 467 | | |
446 | 468 | | |
447 | 469 | | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
452 | 481 | | |
453 | 482 | | |
454 | 483 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
687 | 688 | | |
688 | 689 | | |
689 | 690 | | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
690 | 779 | | |
691 | 780 | | |
692 | 781 | | |
| |||
0 commit comments