datetime.stdptime help

L

Laszlo Nagy

def str2datetime(s):
#28 May 2008 12:38:16 BDT
return time.strptime(s,"%d %b %Y %H:%M:%S %Z")

#.... some code here....
print repr(pd)
print str2datetime(pd)


Result:

'24 May 2008 12:38:16 BDT'
Traceback (most recent call last):
File "__init__.py", line 40, in <module>
print ssc.get_order_data('202-9810306-3236320')
File
"/usr/local/www/vhosts/zeusd1/fantasy/controller/sites/ControllerBase.py",
line 306, in get_order_data
print str2datetime(pd)
File
"/usr/local/www/vhosts/zeusd1/fantasy/controller/sites/ControllerBase.py",
line 299, in str2datetime
return time.strptime(s,"%d %b %Y %H:%M:%S %Z")
File "/usr/local/lib/python2.5/_strptime.py", line 331, in strptime
(data_string, format))
ValueError: time data did not match format: data=24 May 2008 12:38:16
BDT fmt=%d %b %Y %H:%M:%S %Z

I think that the format is correct. What am I doing wrong?

Thanks,

Laszlo
 
M

Mike Driscoll

            def str2datetime(s):
                #28 May 2008 12:38:16 BDT
                return time.strptime(s,"%d %b %Y %H:%M:%S %Z")

            #.... some code here....
            print repr(pd)
            print str2datetime(pd)

Result:

'24 May 2008 12:38:16 BDT'
Traceback (most recent call last):
  File "__init__.py", line 40, in <module>
    print ssc.get_order_data('202-9810306-3236320')
  File
"/usr/local/www/vhosts/zeusd1/fantasy/controller/sites/ControllerBase.py",
line 306, in get_order_data
    print str2datetime(pd)
  File
"/usr/local/www/vhosts/zeusd1/fantasy/controller/sites/ControllerBase.py",
line 299, in str2datetime
    return time.strptime(s,"%d %b %Y %H:%M:%S %Z")
  File "/usr/local/lib/python2.5/_strptime.py", line 331, in strptime
    (data_string, format))
ValueError: time data did not match format:  data=24 May 2008 12:38:16
BDT  fmt=%d %b %Y %H:%M:%S %Z

I think that the format is correct. What am I doing wrong?

Thanks,

   Laszlo


I tried taking out components until I got it to work. It seems to be
choking on the time zone part of it. If you remove the "BDT" from your
string and the "%Z" from your formatter, it works. Unfortunately, I do
not know why it doesn't work the other way.

Mike
 
L

Laszlo Nagy

Out of curiosity -- just what zone /is/ "BDT"... The only thing that
comes to mind is a type for "BST" (British Summer Time)
I think you are right. This date string was taken from a .co.uk site. :)

My fault. It was a 3 letter code after a date, I was sure that it is a
time zone. Apparently, it is not. :)

The "did not match format" message was misleading me. It would be better
to throw ValueError("Unknown time zone: BDT"). :)

Thank you,

Laszlo
 
M

Mike Driscoll

Am Tue, 27 May 2008 12:37:34 -0700 schrieb Dennis Lee Bieber:
   From the library reference:
"""
Support for the %Z directive is based on the values contained in tzname
and whether daylight is true. Because of this, it is platform-specific
except for recognizing UTC and GMT which are always known (and are
considered to be non-daylight savings timezones). """
   The only value that passed for me was UTC (I didn't try GMT) but....

For me, only UTC, GMT, CET and CEST (Central European [Summer] Time) work.
My time.tzname is ('CET', 'CEST').
I think the documentation must be read that ***only***
UTC,GMT,time.tzname work.

Also, time zone names are not unique: EST can be Eastern Summer Time (US)
as well as Eastern Summer Time (Australia).

For working with time zones, I think that a module like pytzhttp://pytz.sourceforge.net/
may be better suited.

My 0.02c.
Martin

Yeah. The timezone part of Python's time module is probably it's
weakest link. There was a pretty long thread about it about a year
ago. Maybe 2.6 or the 3.0 branch will make it a little more friendly.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top