-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
datetime.strptime doesn't support %z format ? #50890
Comments
When trying to use datetime.strptime %z directive got an unexpected error. >>> from datetime import datetime
>>>
>>> fecha = u'Sun Aug 02 19:01:25 +0000 2009'
>>> datetime.strptime(fecha, '%a %b %d %H:%M:%S %z %Y')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y' |
From the documentation from time.strptime() (which acts the same as "Only the directives specified in the documentation [of time.strftime()] So I think invalid. |
I think this bug is just a doc bug. If you check http://docs.python.org/library/datetime.html?highlight=strptime#strftime-behavior and http://docs.python.org/library/time.html?highlight=strptime#time.strptime You can see that the first link lists %z as a valid modifier, while in |
Yes and no. Firstly, %z isn't listed as deprecated in the documentation of the time Secondly, I still think the bug is invalid, because the documentation of Finally, there *is* a confusing docs issue, however: the "strftime() Since in fact the footnote in the documentation of time.strftime() says |
If %z works only in certain circumstances that behaviour should be documented wherever %z is referred to. |
If concrete tzinfo subclass is added to datetime module (see bpo-5094), it will become feasible to meaningfully parse timezone information. |
Attached patch includes bpo-5094 patch. |
On Fri, Jun 11, 2010 at 10:43 AM, Mark Dickinson <report@bugs.python.org> wrote:
Sure. Unfortunately, I think I fixed a few timezone doc warts here |
Mark, I am reassigning this to you for a commit review. |
Doc nit: "When The _strptime._strptime docstring is inaccurate: it claim to return a time struct, but actually returns tuple, int; please could you also add docstrings for _strptime_time and _strptime_datetime? Spacing in datetimemodule.c: "if( module == NULL)" -> "if (module == NULL)". Also, is there any particular reason for initializing 'result' to NULL? (Or even for using result at all; you could just do "return PyObject_CallMethod(... "). I'm mildly distressed by the inability of strptime to parse UTC offsets in the +HH:MM form that str(timezone) produces, but I'm not sure what the solution to that is. Otherwise, this all looks good to my non-expert eye. |
Hmm. Hold on a sec; I'm getting test failures... |
In test_datetime: ====================================================================== Traceback (most recent call last):
File "Lib/test/test_datetime.py", line 1741, in test_strptime
dt = strptime("-0500 EST", "%z %Z")
File "/Users/dickinsm/python/svn/py3k/Lib/_strptime.py", line 483, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Users/dickinsm/python/svn/py3k/Lib/_strptime.py", line 336, in _strptime
(data_string, format))
ValueError: time data '-0500 EST' does not match format '%z %Z' ====================================================================== Traceback (most recent call last):
File "Lib/test/test_datetime.py", line 1741, in test_strptime
dt = strptime("-0500 EST", "%z %Z")
File "/Users/dickinsm/python/svn/py3k/Lib/_strptime.py", line 483, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Users/dickinsm/python/svn/py3k/Lib/_strptime.py", line 336, in _strptime
(data_string, format))
ValueError: time data '-0500 EST' does not match format '%z %Z' |
issue6641a.diff fixes the nits that Mark found and makes the tests robust with respect to change of timezones. Tested with all timezones avalilable on OSX including TZ="Eire". (Ever heard of Ouagadougou?) Parsing RFC 3339's HH:MM format is a separate issue. I am +1 on either adding %:z specifier or simply allow HH:MM for %z. |
LGTM. |
Committed in r82053. |
Reopening because the patch introduced a regression with respect to datetime subclasses: >>> class DT(datetime): pass
...
>>> DT.strptime('', '')
datetime.datetime(1900, 1, 1, 0, 0) a DT instance expected. Need tests covering subclasses. |
Attaching issue6641b.diff that fixes subclassing issue. Added tests for datetime subclass only to keep the patch focused. Adding tests for datetime and time subclasses will be natural in bpo-1100942. |
Replacing issue6641b.diff after fixing some spelling errors. Thanks, Ezio. |
Committed in r82073. |
PEP-8 says: “If your public attribute name collides with a reserved keyword, append a single trailing underscore to your attribute name. This is preferable to an abbreviation or corrupted spelling.” e.g. “class_” is used for an HTML class. “(However, notwithstanding this rule, 'cls' is the preferred spelling for any variable or argument which is known to be a class, especially the first argument to a class method.)” I’d like this recommendation to be followed, even if it’s in an internal function. |
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: