Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions can/io/asc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,41 +116,50 @@ def _extract_header(self) -> None:

@staticmethod
def _datetime_to_timestamp(datetime_string: str) -> float:
# ugly locale independent solution
month_map = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"May": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sep": 9,
"Oct": 10,
"Nov": 11,
"Dec": 12,
"Mär": 3,
"Mai": 5,
"Okt": 10,
"Dez": 12,
"jan": 1,
"feb": 2,
"mar": 3,
"apr": 4,
"may": 5,
"jun": 6,
"jul": 7,
"aug": 8,
"sep": 9,
"oct": 10,
"nov": 11,
"dec": 12,
"mär": 3,
"mai": 5,
"okt": 10,
"dez": 12,
}
for name, number in month_map.items():
datetime_string = datetime_string.replace(name, str(number).zfill(2))

datetime_formats = (
"%m %d %I:%M:%S.%f %p %Y",
"%m %d %I:%M:%S %p %Y",
"%m %d %H:%M:%S.%f %Y",
"%m %d %H:%M:%S %Y",
"%m %d %H:%M:%S.%f %p %Y",
"%m %d %H:%M:%S %p %Y",
)

datetime_string_parts = datetime_string.split(" ", 1)
month = datetime_string_parts[0].strip().lower()

try:
datetime_string_parts[0] = f"{month_map[month]:02d}"
except KeyError:
raise ValueError(f"Unsupported month abbreviation: {month}") from None
datetime_string = " ".join(datetime_string_parts)

for format_str in datetime_formats:
try:
return datetime.strptime(datetime_string, format_str).timestamp()
except ValueError:
continue

raise ValueError(f"Incompatible datetime string {datetime_string}")
raise ValueError(f"Unsupported datetime format: '{datetime_string}'")

def _extract_can_id(self, str_can_id: str, msg_kwargs: dict[str, Any]) -> None:
if str_can_id[-1:].lower() == "x":
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/2009.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved datetime parsing and added support for “double-defined” datetime strings (such as, e.g., `"30 15:06:13.191 pm 2017"`) for ASCReader class.
34 changes: 34 additions & 0 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,40 @@ def test_write(self):

self.assertEqual(expected_file.read_text(), actual_file.read_text())

@parameterized.expand(
[
(
"May 27 04:09:35.000 pm 2014",
datetime(2014, 5, 27, 16, 9, 35, 0).timestamp(),
),
(
"Mai 27 04:09:35.000 pm 2014",
datetime(2014, 5, 27, 16, 9, 35, 0).timestamp(),
),
(
"Apr 28 10:44:52.480 2022",
datetime(2022, 4, 28, 10, 44, 52, 480000).timestamp(),
),
(
"Sep 30 15:06:13.191 2017",
datetime(2017, 9, 30, 15, 6, 13, 191000).timestamp(),
),
(
"Sep 30 15:06:13.191 pm 2017",
datetime(2017, 9, 30, 15, 6, 13, 191000).timestamp(),
),
(
"Sep 30 15:06:13.191 am 2017",
datetime(2017, 9, 30, 15, 6, 13, 191000).timestamp(),
),
]
)
def test_datetime_to_timestamp(
self, datetime_string: str, expected_timestamp: float
):
timestamp = can.ASCReader._datetime_to_timestamp(datetime_string)
self.assertAlmostEqual(timestamp, expected_timestamp)


class TestBlfFileFormat(ReaderWriterTest):
"""Tests can.BLFWriter and can.BLFReader.
Expand Down