-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
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
strftime/strptime round trip fails even for UTC datetime object #66437
Comments
>>> from datetime import datetime, timezone
>>> dt = datetime.now(timezone.utc)
>>> fmt = '%Y-%m-%d %H:%M:%S.%f %Z%z'
>>> datetime.strptime(dt.strftime(fmt), fmt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/cpython/Lib/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/cpython/Lib/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data '2014-08-21 11:29:13.537251 UTC+00:00+0000'
does not match format '%Y-%m-%d %H:%M:%S.%f %Z%z' The issue is that dt.strftime('%Z') produces 'UTC+00:00' [1] https://docs.python.org/3.4/library/datetime.html#strftime-and-strptime-behavior |
This is a duplicate of bpo-15873. |
I don't see, how it is a duplicate. Everything works if pytz.utc (.tzname() == 'UTC') [1] https://docs.python.org/3.4/library/datetime.html#datetime.tzinfo.fromutc Why does datetime.now(timezone.utc).strftime('%Z') |
I see. I thought you were complaining about "%z" format not supporting "00:00" as in >>> from datetime import *
>>> datetime.strptime("00:00","%z")
Traceback (most recent call last):
..
ValueError: time data '00:00' does not match format '%z' but your issue is that %Z does not parse "UTC+00:00" as in >>> datetime.strptime("UTC+00:00x","%Zx")
Traceback (most recent call last):
..
ValueError: time data 'UTC+00:00x' does not match format '%Zx' The name produced by timezone when no name is explicitly specified is documented: https://docs.python.org/3.4/library/datetime.html#datetime.timezone.tzname
I think it can. I am surprised this did not come up in bpo-5094 where UTC±hh:mm syntax was discussed. The change would be trivial - just supply explicit name to utc singleton. Please ask on Python-Dev if anyone would object. |
See also <http://bugs.python.org/issue5094#msg106476\>. It looks like providing 'UTC' as the name of utc singleton was part of my original proposal. I have no recollection on why it was not implemented that way. |
Akira, I see that you participated in the original discussion (msg107608). We settled on str(timezone.utc) == 'UTC+00:00' and this was clearly a deliberate choice. I don't think we can revisit this now, but we can probably make strptime smart enough to parse UTC±hh:mm with %Z format code. |
I see it advocates either 'UTC' or '+00:00' ('-00:00' is semantically UTC+HH:MM is neither here nor there: it is easily confusable with pytz is *the* module that exposes zoneinfo (the timezone database) |
OK, I guess we can change stdlib datetime.timezone.utc's str() to 'UTC'. Make sure to update both the C and the Python version, and the tests, and the docs. It's going to have to go into 3.6. |
I left a couple comments on Rietveld. |
Addressed review comments. |
New changeset f904b7eb3981 by Alexander Belopolsky in branch 'default': |
New changeset ea1bfb64e898 by Victor Stinner in branch 'default': |
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:
The text was updated successfully, but these errors were encountered: