time.strptime() undocumented difference python 2.5.1>2.5.2

TYR a.harrowell at gmail.com
Wed Mar 25 17:26:36 EDT 2009


On Mar 25, 7:38 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> TYR wrote:
> > A server that runs one of my programs was upgraded to Debian Lenny
> > last night, which moved it from Python 2.4.4 to 2.5.2. This caused
> > immediate trouble. At one point, data is parsed from a Web page, and
> > among other things a time date group is collected. This is in a nice
> > human readable format, but I want it in RFC 822 format because it's
> > going to be used in an outbound GeoRSS feed and it's a requirement.
>
> > So the time group is split up with a regex, merged into one string,
> > and fed to time.strptime() with a fitting format, then passed to
> > Utils.formatdate() and used. The time group looks like this:
> > 25/03/2009 21:05:00
>
> > Code:
> > if airline not in whitelist:
> >                  retime = re.split('[-\s:/]', rawtime)
> >                  timeinput = ''.join(retime)
> >                  t = time.strptime(timeinput, "%d %m %Y %H %M %S")
> >                  timeout = Utils.formatdate(t)
>
> > Error:
> > Traceback (most recent call
> > last):
> >   File "/home/yorksranter/vfeed-data/bothv7.py", line 46, in
> > <module>
> >     t = time.strptime(timeinput, "%d %m %Y %H %M
> > %S")
> >   File "/usr/lib/python2.5/_strptime.py", line 330, in
> > strptime
> >     (data_string,
> > format))
> > ValueError: time data did not match format:  data=25032009210500  fmt=
> > %d %m %Y %H %M %S
>
> > Weirdness:
>
> > Python 2.5.2 (r252:60911, Jan  4 2009, 21:59:32) #the
> > server
> > [GCC 4.3.2] on
> > linux2
> > Type "help", "copyright", "credits" or "license" for more
> > information.
> >>>> timeinput = '25032009210500'
> >>>> import time
> >>>> t = time.strptime(timeinput, "%d %m %Y %H %M %S")
> > Traceback (most recent call
> > last):
> >   File "<stdin>", line 1, in
> > <module>
> >   File "/usr/lib/python2.5/_strptime.py", line 330, in
> > strptime
> >     (data_string,
> > format))
> > ValueError: time data did not match format:  data=25032009210500  fmt=
> > %d %m %Y %H %M %S
>
> > But....
>
> > Python 2.5.1 (r251:54863, Jan 10 2008, 18:01:57) #the laptop
> > [GCC 4.2.1 (SUSE Linux)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> import time
> >>>> timeinput = '25032009210500'
> >>>> t = time.strptime(timeinput, "%d %m %Y %H %M %S")
> >>>> print t
> > (2009, 3, 25, 21, 5, 0, 2, 84, -1)
>
> > Docs (http://docs.python.org/library/time.html#time.strptime) say:
> > Parses a string representing a time according to a format. If I send
> > it through str() to make absolutely sure it is a string I get the same
> > error.
>
> > I note from the changelog that there was a change affecting
> > time.strptime()...(http://www.python.org/download/releases/2.5.2/
> > NEWS.txt)
>
> > - Bug #1730389: Have time.strptime() match spaces in a format argument
> > with
> >   ``\s+`` instead of ``\s*``.
>
> > - Bug #1290505: Properly clear time.strptime's locale cache when the
> > locale
> >   changes between calls.  Backport of r54646 and r54647.
>
> > I don't see that either of these should break it, but it's got to be a
> > suspect.
>
> I think it's due to bug #1730389. This says that a space in the format
> string should match _at least one_ space in the string it's parsing.
> Your format is "%d %m %Y %H %M %S" (it contains spaces) but your string
> is "25032009210500" (it doesn't contain spaces), hence the failure.
>
> I suggest you change:
>
>      timeinput = ''.join(retime)
>
> to:
>
>      timeinput = ' '.join(retime)
>
> It'll make the string a little clearer anyway: "25 03 2009 21 05 00".

Thank you; that was indeed the issue, which helped me discover that
email.Utils.formatdate() stopped accepting struct_times in the same
change and started wanting floats.



More information about the Python-list mailing list