-
-
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
%Z in strptime doesn't match EST and others #66571
Comments
The documentation for %Z ( https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior ) says it matches Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
DreamPie 1.2.1
>>> import datetime
>>> datetime.datetime.strptime('2016-12-04 08:00:00 UTC', '%Y-%m-%d %H:%M:%S %Z')
0: datetime.datetime(2016, 12, 4, 8, 0)
>>> datetime.datetime.strptime('2016-12-04 08:00:00 EST', '%Y-%m-%d %H:%M:%S %Z')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
datetime.datetime.strptime('2016-12-04 08:00:00 EST', '%Y-%m-%d %H:%M:%S %Z')
File "C:\Python34\lib\_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Python34\lib\_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data '2016-12-04 08:00:00 EST' does not match format '%Y-%m-%d %H:%M:%S %Z'
>>> |
Looking at the code, the only timezone strings it recognizes are utc, gmt, and whatever is in time.tzname (EST and EDT, in my case). This seems...barely useful, although clearly not useless :) And does not seem to be documented. |
if PEP-431 is implemented (or anything that gives access to zoneinfo) import pytz # $ pip install pytz {tzname for tz in map(pytz.timezone, pytz.all_timezones) It includes EST. |
Without %z (utc offset) strptime returns a naive datetime object that It might explain why %Z tries to match only utc and the local timezone |
I think its existing behavior is because it doesn't have access to a list of recognized timezones. As you say, this could be fixed by PEP-431. It could also be fixed by adopting the "email standard" timezones (see email/_parseaddr.py), which is a def-facto standard. Regardless of whether something is done to expand the number of timezone it knows about, though, there's a current doc bug that should be fixed. If someone wants to advocate for expanding the timezone list, that should be a separate issue. |
FTR, I have a WIP(and probably a bit outdated) branch to implement PEP-431 on GitHub:
|
If the current implementation is considered correct (%Z not recognizing The current behavior is broken, see also bpo-22426 If we assume that the docs are correct (%Z should match EST) even if it Lib/email/_parseaddr.py approach (tzname corresponds to a fixed utc CST is ambiguous if %z is not given therefore even if strptime() had the
email._parseaddr._timezones is misleading if used globally: CST is also One of possible solutions is to return aware datetime objects if a new If *timezones* is False then strptime() returns a naive datetime With bool(timezones) == True, strptime() could return an aware datetime Default *timezones* is None that means timezone=False for backward if tzname and timezones is None: # %Z matches non-empty string
warn("Default *timezones* parameter for "
"%s.strptime() will be True in Python 3.6. "
"Pass timezones=False to preserve the old behaviour" % (
cls.__qualname__,),
category=DeprecationWarning, stacklevel=2) I've uploaded the patch draft-strptime-%Z-timezones.diff that implements |
I don't think we are going to support a timezone list like that without PEP-431. You should attach your patch to a new issue. When I said this should the doc issue, that is because only a doc fix is acceptable for 3.4. Adding more timezones to recognize would be an enhancement, given the complexity of the proposed solution. On the other hand, if timezone names are ambiguous, I'm not sure there *is* a fix other than using the "defacto standard" names and offsets used by the email library. Actually, isn't there a written standard that addresses this issue? I seem to remember reading a discussion of the problem somewhere... |
PEP-431 won't fix this issue. See below.
The docs are correct (they imply that %Z should accept EST). It is the The patch introduces a new parameter therefore I agree: it should be
Multi-timezone programming email._parseaddr._timezones with CST=-600 is like US-ASCII (the Code that uses local timezone is bilingual (locale-based): CST=-600 in Code that uses the tz database is multilingual (Unicode): knowing the If you don't know the encoding then the support for Unicode (the Given that the list is used to accept a string as a timezone The initial list could be seeded from using pytz as in my patch and then |
Given the difference between the documented and the actual behaviours, and given that it's apparently not obvious what the correct fix should be, would a patch that updates the docs (to say that %Z only matched GMT and UTC) be welcome? |
Peter: yes, that is what I've been saying this issue is for :) Anything else is a new issue. Note that it *does* also recognize the strings in time.tzname in addition to UTC and GMT. |
I think strptime should only accept %Z when it comes together with %z and not do any validation. This is close to the current behavior. %Z by itself is useless because even when it is accepted, the value is discarded: >>> print(datetime.strptime('UTC', '%Z'))
1900-01-01 00:00:00 You have to use %z to get an aware datetime instance: >>> print(datetime.strptime('UTC+0000', '%Z%z'))
1900-01-01 00:00:00+00:00 The validation is already fairly lax: >>> print(datetime.strptime('UTC+1234', '%Z%z'))
1900-01-01 00:00:00+12:34 I don't think this issue has anything to do with the availability of zoneinfo database. Timezone abbreviations are often ambiguous and should only serve as a human-readable supplement to the UTC offset and cannot by itself be used as a TZ specification. |
This behavior is currently unchanged and the docs still state that
|
@alex LordThorsen: It will accept EST if EST is one of your "local" time zones, so whatever's in In the short term, I think the right thing to do would be to update the documentation to remove the reference to "EST", and add an explanatory note in the section about %Z that explains that it accepts a few hard-coded values + whatever's in In the long run, I think the best "out of the box" support we can provide would be supporting %Z when %z is present (per Alexander's suggestion), and possibly something akin to |
It's been a while since I've committed a patch. Do I still upload a diff file here or should I open a PR for the doc changes on github? |
PR on github, Alex |
I created a PR following the recommendations of p-ganssle |
With the introduction of PEP-0615 (https://www.python.org/dev/peps/pep-0615/) — Support for the IANA Time Zone Database in the Standard Library — should this be revisited to now leverage ZoneInfo to fully parse these time zone values in Python 3.9+ (or 3.11 with introduction of the feature if it is unreasonable to backport this)? |
There are two major issues that aren't solved by the
As mentioned earlier in the thread, the best options we have for this are:
I am not sure that either option is entirely satisfactory. The second one is probably the better one, but I don't think people would like it as much 😅. Maybe something like this:
If We might also be able to be more lax about what is accepted for
It would be possible for someone to implement their own tzname-to-IANA mapping, since they could key on
I'm inclined to start with this last solution since it's a minimal change and the current behavior is probably unexpected for most people anyway, and it makes it at least possible for most people to solve their own problem. |
@pganssle If I'm understanding correctly, the main crux of this problem is that
Are you referring to this?
|
Sorry, I realize that was a bit confusing. I was actually referring to this:
I think the least controversial change here would probably be to make it so |
The docs were corrected, is there anything else you want to do here @pganssle ? |
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: