By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

datetime.stdptime help

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On May 27, 10:34*am, Laszlo Nagy <gand...@shopzeus.comwrote:
* * * * * * 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
Jun 27 '08 #2

P: n/a
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

Jun 27 '08 #3

P: n/a
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 pytz
http://pytz.sourceforge.net/
may be better suited.

My 0.02c.
Martin
Jun 27 '08 #4

P: n/a
On May 28, 12:19*pm, mblume <mbl...@socha.netwrote:
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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.