time.strptime() and time.strftime() reciprocity

Denis Kasak denis.kasak at gmail.com
Mon Feb 16 19:26:04 EST 2009


On Mon, Feb 16, 2009 at 11:57 PM, Greg Krohn <invalid at example.invalid>
wrote:

<snip>

> ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
> Python 2.6.1 (r261:67515, Dec  5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)]
> on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import time
>>>> fmt = "%m/%d/%y %I:%M %p"
>>>> print time.strptime(fmt, time.strftime(fmt))
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "C:\Python26\lib\_strptime.py", line 454, in _strptime_time
>    return _strptime(data_string, format)[0]
>  File "C:\Python26\lib\_strptime.py", line 325, in _strptime
>    (data_string, format))
> ValueError: time data '%m/%d/%y %I:%M %p' does not match format '02/16/09
> 04:52
> PM'
>
> So, yeah, that seems weird to me. Does anyone get similar results, know why
> this is happening, and/or how to fix it?

It's actually pretty trivial to fix, which you would have also known
if you had read the message of the exception more carefully. :P

You passed the arguments for strptime() the wrong way around. Just
pass them in reverse and your problem will be fixed.

--
Denis Kasak



More information about the Python-list mailing list