diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 608024f3..def29a0f 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -503,12 +503,12 @@ def export_record(var, eds): origFileInfo = { # just set some defaults "CreationDate": defmtime.strftime("%m-%d-%Y"), - "CreationTime": defmtime.strftime("%I:%m%p"), + "CreationTime": defmtime.strftime("%I:%M%p"), "EdsVersion": 4.2, } file_info.setdefault("ModificationDate", defmtime.strftime("%m-%d-%Y")) - file_info.setdefault("ModificationTime", defmtime.strftime("%I:%m%p")) + file_info.setdefault("ModificationTime", defmtime.strftime("%I:%M%p")) for k, v in origFileInfo.items(): file_info.setdefault(k, v) diff --git a/test/test_eds_timestamps.py b/test/test_eds_timestamps.py new file mode 100644 index 00000000..c440b139 --- /dev/null +++ b/test/test_eds_timestamps.py @@ -0,0 +1,42 @@ +"""File timestamps must preserve the clock's minutes, not the month.""" +from configparser import RawConfigParser +from datetime import datetime +from io import StringIO +from unittest.mock import patch + +import pytest + +from canopen.objectdictionary import ObjectDictionary +from canopen.objectdictionary.eds import export_dcf, export_eds + + +@pytest.mark.parametrize('exporter', [export_eds, export_dcf]) +@pytest.mark.parametrize('hour,minute,expected', [(15, 45, '03:45PM'), (0, 7, '12:07AM')]) +def test_generated_file_times(exporter, hour, minute, expected): + class Clock(datetime): + @classmethod + def utcnow(cls): + return cls(2026, 11, 18, hour, minute) + + output = StringIO() + with patch('datetime.datetime', Clock): + # Give each export its own metadata dictionary. + exporter(ObjectDictionary(), output, {}) + result = RawConfigParser() + result.read_string(output.getvalue()) + assert result['FileInfo']['CreationTime'] == expected + assert result['FileInfo']['ModificationTime'] == expected + assert result['FileInfo']['CreationDate'] == '11-18-2026' + assert result['FileInfo']['ModificationDate'] == '11-18-2026' + + +@pytest.mark.parametrize('exporter', [export_eds, export_dcf]) +def test_explicit_file_times_are_preserved(exporter): + output = StringIO() + exporter(ObjectDictionary(), output, { + 'CreationTime': '06:29AM', 'ModificationTime': '08:53PM', + }) + result = RawConfigParser() + result.read_string(output.getvalue()) + assert result['FileInfo']['CreationTime'] == '06:29AM' + assert result['FileInfo']['ModificationTime'] == '08:53PM'