Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > time.strptime() and time.strftime() reciprocity

Reply
Thread Tools

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

 
 
Greg Krohn
Guest
Posts: n/a
 
      02-16-2009
[Apologies for the use of "reciprocity" in the subject - it's a little
pompous, I know, but it fits so well. ]

Hello c.l.python!

I'm trying to parse some dates of the form "01/29/09 12:55 PM" from a
CSV file, but I'm having trouble with the format string in
time.strptime() . In testing this I found that the time module chokes on
a string that it created itself with the same format:

ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:3 [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?

Thanks.

--
("%s@gmail com" % "Greg Krohn".lower()).replace(" ", ".")
 
Reply With Quote
 
 
 
 
Denis Kasak
Guest
Posts: n/a
 
      02-17-2009
On Mon, Feb 16, 2009 at 11:57 PM, Greg Krohn <(E-Mail Removed)>
wrote:

<snip>

> ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
> Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:3 [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.

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

--
Denis Kasak
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I don't *think* this is reciprocity failure... Paul Ciszek Digital Photography 7 09-16-2012 01:49 AM
Reciprocity a problem with digital? jimphotog via PhotoKB.com Digital Photography 4 03-22-2007 11:02 AM
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM
Reciprocity failure in digital camers? g@risky-biz.com Digital Photography 6 09-19-2006 10:54 PM
reciprocity failure - what is it? cc Digital Photography 27 01-18-2004 09:42 PM



Advertisments