Convert naive/local datetimes to UTC in TestResult and Step serialization#116
Convert naive/local datetimes to UTC in TestResult and Step serialization#116
Conversation
Adds _ensure_utc() helper to _grpc_conversion.py that converts naive datetimes (treated as local time) to UTC before passing to hightime_datetime_to_protobuf(). Uses it in to_protobuf() for both TestResult and Step so that datetime.now() works without error. Co-authored-by: csjall <4081815+csjall@users.noreply.github.com>
There was a problem hiding this comment.
@copilot Ideally we should update the hightime_datetime_to_protobuf method in protobuf library to ensure UTC. This way each and every Python client which uses hightime_datetime_to_protobuf should not need to implement _ensure_utc.
There was a problem hiding this comment.
That's a good long-term suggestion. However, ni-protobuf-types is a separate externally-published package (ni.protobuf.types v1.1.0) installed as a dependency — it lives in its own repository and would require its own release cycle to update. Since we can't change it from here, keeping _ensure_utc() in this library is the right approach. If the upstream fix is desired, it should be tracked as a separate issue/PR in the ni-protobuf-types repository.
TestResultandStepreject naive datetimes at serialization time becausehightime_datetime_to_protobufrequires UTC. Users providingdatetime.now()get a crypticValueError: The tzinfo must be datetime.timezone.utc.Changes
_grpc_conversion.py— Added_ensure_utc()helper that callsastimezone(timezone.utc), which correctly handles both naive datetimes (treated as local time) and tz-aware datetimes_test_result.py,_step.py— Import and apply_ensure_utc()into_protobuf()before passing datetimes tohightime_datetime_to_protobuf()Example
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.