From 4f69312bef889ce39e456669524615e40ff6e1ae Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Tue, 17 Nov 2026 17:34:48 +0100 Subject: [PATCH] Parse native Windows and Rust timestamp precision on Python 3.10 --- tests/windows_update_timestamps.py | 26 ++++++++++++++++++++++++++ tools/windows_update_scope.py | 13 ++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/windows_update_timestamps.py diff --git a/tests/windows_update_timestamps.py b/tests/windows_update_timestamps.py new file mode 100644 index 0000000..24bf947 --- /dev/null +++ b/tests/windows_update_timestamps.py @@ -0,0 +1,26 @@ +"""Windows/Rust timestamps must also parse on the Python 3.10 operator host.""" +from datetime import datetime, timezone +from pathlib import Path +import sys +import unittest +sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tools')) +from windows_update_scope import stamp + +class TimestampFormats(unittest.TestCase): + def test_platform_fractional_precision(self): + for fraction in ('', '.4', '.490', '.490042', '.4900421', '.490042100'): + with self.subTest(fraction=fraction): + value=stamp('2026-11-17T16:32:32'+fraction+'Z') + self.assertEqual(value.second,32) + self.assertEqual(value.microsecond, int((fraction[1:]+'000000')[:6])) + self.assertEqual(value.utcoffset().total_seconds(),0) + + def test_offset_and_comparison(self): + self.assertEqual(stamp('2026-11-17T17:32:32.490042100+01:00'), + datetime(2026,11,17,16,32,32,490042,tzinfo=timezone.utc)) + + def test_malformed_and_unqualified_are_rejected(self): + for value in ('2026-11-17T16:32:32','2026-11-17T16:32:32.4900421000Z','invalid'): + with self.subTest(value=value), self.assertRaises(ValueError):stamp(value) + +if __name__=='__main__':unittest.main() diff --git a/tools/windows_update_scope.py b/tools/windows_update_scope.py index 71bc8b2..9faaa6f 100644 --- a/tools/windows_update_scope.py +++ b/tools/windows_update_scope.py @@ -35,7 +35,13 @@ def utc(): def stamp(value): - result = datetime.fromisoformat(value.replace('Z', '+00:00')) + # Windows emits 100 ns timestamps and Rust may emit nanoseconds. Python + # 3.10 accepts only millisecond or microsecond fractional precision. + match = re.fullmatch(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})', value) + require(match is not None, 'A timezone-qualified timestamp is required') + fraction = (match[2] or '').ljust(6, '0')[:6] + offset = '+00:00' if match[3] == 'Z' else match[3] + result = datetime.fromisoformat(match[1] + '.' + fraction + offset) require(result.tzinfo is not None, 'Timestamp lacks timezone') return result @@ -279,6 +285,11 @@ def local(): data.update(agent_sha256=plan['agent_sha256'],helper_binary_sha256=plan['helper_sha256']) with os.fdopen(os.open(casefile,os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600),'wb') as dst: dst.write(encode(data));dst.flush();os.fsync(dst.fileno()) + directory=os.open(state_dir,os.O_RDONLY|os.O_DIRECTORY) + try: + os.fsync(directory) + finally: + os.close(directory) emit({'preparing_window':data['case_id'],'expires_at':data['expires_at']}) emit(remote_server('prepare',data)) try: