-
-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-46614: Allow datetime.isoformat to use "Z" UTC designator #32041
base: main
Are you sure you want to change the base?
Conversation
d339da9
to
eb03e32
Compare
Add an optional parameter to datetime.isoformat() and time.isoformat() called use_utc_designator. If provided and True and the object is associated with a tzinfo and its tzname is exactly "UTC", the formatted UTC offset will be "Z" rather than an offset from UTC.
eb03e32
to
03fbca3
Compare
tz = self._tzstr() | ||
if tz: | ||
s += tz | ||
if use_utc_designator and "UTC" == self.tzname(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor thing: this is a strange style for a comparison in Python. The mistake of if something = oops_assigned_it
doesn’t exist, so the natural order if self.tzname() == "UTC"
can be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. I'm not gonna polish this further until the datetime maintainers weigh in on whether they want to pursue this approach at all, but if they agree to this approach I'll make this change.
Any thoughts, @pganssle? Would this resolve https://bugs.python.org/issue46614 to your satisfaction? |
@godlygeek Sorry for the delay. I've left some more bikeshedd-y comments on https://bugs.python.org/issue46614, but I think broadly speaking this is probably the right approach. A few thoughts in addition to the bikeshedding:
|
Possibly, though I don't think it's necessary, since as you say,
I'm inclined to think that if someone creates a timezone that self-reports its name as "UTC" but has a non-zero offset from UTC, having it show up as "Z" in
Since this will be opt-in rather than a default behavior, I wouldn't worry much about the performance implications of a single extra method call. This seems like a premature optimization to me, but it wouldn't be tough to implement if you think it's justified. |
I recompiled from scratch and when I run datetimetester.py I get: test_gaps (Lib.test.datetimetester.ZoneInfoTest[Africa/Harare]) ... ok ERROR: test_divide_and_round (Lib.test.datetimetester.TestModule) Traceback (most recent call last): FAIL: test_compat_unpickle (Lib.test.datetimetester.TestDateTimeTZ) Traceback (most recent call last): FAIL: test_compat_unpickle (Lib.test.datetimetester.TestTimeTZ) Traceback (most recent call last): Ran 1754 tests in 8.046s I cant say whether this is due to your changes or not especially when I see |
See this CPython pull request and the linked discussion for an explanation of why `replace('Z', '+00:00')` is needed: python/cpython#32041
@merwok @godlygeek Any progress on this? Would love to help if needed :D |
Add an optional parameter to datetime.isoformat() and time.isoformat()
called use_utc_designator. If provided and True and the object is
associated with a tzinfo and its tzname is exactly "UTC", the formatted
UTC offset will be "Z" rather than an offset from UTC.
https://bugs.python.org/issue46614