-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Add option to output UTC datetimes as "Z" in .isoformat()
#90772
Comments
As part of bpo-35829, it was suggested that we add the ability to output the "Z" suffix in I've spun this off into its own issue so that we can discuss how to implement the feature. The two obvious questions I see are:
For example, do we want this? >>> LON = zoneinfo.ZoneInfo("Europe/London")
>>> datetime(2022, 3, 1, tzinfo=LON).isoformat(utc_as_z=True)
2022-03-01T00:00:00Z
>>> datetime(2022, 6, 1, tzinfo=LON).isoformat(utc_as_z=True)
2022-06-01T00:00:00+01:00 Another possible definition might be if the >>> datetime.timezone.utc.utcoffset(None)
timedelta(0)
>>> zoneinfo.ZoneInfo("UTC").utcoffset(None)
timedelta(0)
>>> dateutil.tz.UTC.utcoffset(None)
timedelta(0)
>>> pytz.UTC.utcoffset(None)
timedelta(0) The only "odd man out" is I feel like "If the offset is 00:00, use Z" is the wrong rule to use conceptually, but considering that people will be opting into this behavior, it is more likely that they will be surprised by Yet another option might be to add a completely separate function, |
Would it be horrible to have the timezone instance control this? |
This is a really good point that I hadn't considered: It seems like the most semantically correct thing would be to only use >>> datetime.datetime.now(zoneinfo.ZoneInfo("Europe/London")).tzname()
'GMT'
>>> datetime.datetime.now(zoneinfo.ZoneInfo("UTC")).tzname()
'UTC'
>>> datetime.datetime.now(datetime.timezone.utc).tzname()
'UTC'
>>> datetime.datetime.now(dateutil.tz.gettz("Europe/London")).tzname()
'GMT'
>>> datetime.datetime.now(dateutil.tz.UTC).tzname()
'UTC'
>>> datetime.datetime.now(pytz.timezone("Europe/London")).tzname()
'GMT'
>>> datetime.datetime.now(pytz.UTC).tzname()
'UTC' I think the right rule to use conceptually is "if |
I think this approach is probably the best we can do, but I could also imagine that users might find it to be confusing behavior. I wonder if there's any informal user testing we can do? I guess the ISO 8601 spec does call "Z" the "UTC designator", so A name like |
Bad idea: pass It is short, memorable if you know about it, otherwise obscure enough to push people to read the docs and be clear about what it does. Also strange and far from obvious, so a bad idea. Unless… ? |
I was thinking along similar lines when I used
This would definitely be more memorable and more approachable. If we stick with making it conditional on
|
After running a survey where I tried out 4 different keyword arguments at random with 972 participants (not all of them completed the survey all the way, admittedly) and asked people what they thought it does in various situations, I got the following results:
This is strange because I think none of us like
I was hoping the results here would be less... ambiguous. |
Given these results and the fact that people don't seem to have a great grasp on what this is supposed to do, let's push this feature to 3.12 to try to come up with a better design. |
I have to disagree about this approach:
Mainly because you may expect that your local TZ always return the 2nd format instead of the 1st one (the shortcut of +00:00) At the end what we want is to avoid this => https://i.redd.it/ocpk67fp6tx81.jpg |
After seeing usage in the PR, I find |
I suggest this is controlled in part by the For instance, in
can be extended as something like
A respective change would be needed in The string
Now changing the default behavior of datetime formatting function to start outputting So looking at the declaration of
Perhaps we could use a new |
It should just automatically do the right thing, i.e. use |
In principle, I support this. In practice, I suspect this won't be done because changing the default behaviour of isoformat would break code that depends on the UTC offset always being formatted the way it currently is. |
I object to the idea that using
Whatever we do needs to be explicit, but it seems very hard to do that, since when we ran the survey people seemed to have conflicting understandings of what any of this stuff might do; there doesn't seem to be an unambiguous way to convey what the behavior might be. If this were a high priority and in high demand, I would suggest we might bite the bullet and have this be just one more thing that end users need to learn about |
It might be worth adding a |
JFYI: https://www.rfc-editor.org/rfc/rfc9557#name-updating-rfc-3339 Note that almost all timestamps in reality do not carry the intention to express a time zone offset, so Z is now the normal way to write an RFC 3339 timestamp -- the spec now follows the established practice for instance in the JavaScript community. |
Sorry Paul but you have been assigned for a few years and no pr so I assume you have not been actively working on this. Continued the work from the old pr. Making it mandatory is a discussion for later as its a backwards compatibility nightmare. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: