unittest-xml-reporting icon indicating copy to clipboard operation
unittest-xml-reporting copied to clipboard

Add time zone to reported timestamp

Open suo opened this issue 3 years ago • 1 comments

When used on a timezone-unaware datetime, Python's datetime.isoformat() method produces a timestamp without a timezone designator. While this is technically allowed by ISO8601, many ISO8601 parsers expect a timezone designator of some sort.

This PR adds an astimezone() call before creating the timestamp. When used on a timezone-unaware datetime (as the one here is), it will assume the datetime is relative to the system timezone and append that to the timestamp. This is the correct behavior in this instance.

This change will probably be a no-op for most people, as fromisoformat() and other ISO8601-compliant parsers will parse the new timestamp just fine. This change would be a breaking change for anyone relying on the timestamp to be timezone-unaware.

Please let me know if you have ideas for testing! We use xmlrunner in PyTorch in our CI, and our CI runners are all over the place geographically, so it would be useful for us to have a timezone-aware timestamp.

suo avatar Mar 18 '22 21:03 suo

One further note: this change is incompatible with Python 3.5 or lower, since it relies on the following behavior from astimezone:

Changed in version 3.6: The astimezone() method can now be called on naive instances that are presumed to represent system local time.

It looks like this project does not support 3.5 or lower, but just wanted to note for completeness.

suo avatar Mar 18 '22 21:03 suo