Parse native Windows and Rust timestamp precision on Python 3.10

This commit is contained in:
2026-11-17 17:34:48 +01:00
parent 8c39e79a79
commit 4f69312bef
2 changed files with 38 additions and 1 deletions
+26
View File
@@ -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()
+12 -1
View File
@@ -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: