Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Syntax Problem with strptime in Python 2.4

111 views
Skip to first unread message

W. eWatson

unread,
Sep 8, 2008, 5:51:54 AM9/8/08
to
Apparently, use of strptime of datetime needs a workaround in Python 2.4 to
work properly. The workaround is d =
datetime.datetime(*(time.strptime(date_string, format)[0:5])). However, when
I try to use it, or even use it the regular way, it fails with
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'.
From the following code code segment:

format = '%Y%m%d_%H%M%S'
#d=datetime.strptime('20080321_113405', format)-- typical use
print time.strptime('20080321_113405', format)[0:5]
d = datetime.datetime(*time.strptime('20080321_113405', format)[0:5])

Does anyone know how to make this work in 2.4? If not, is there a way to
achieve the same result?
--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

sk...@pobox.com

unread,
Sep 8, 2008, 6:51:48 AM9/8/08
to pytho...@python.org

>> Apparently, use of strptime of datetime needs a workaround in Python
>> 2.4 to work properly. The workaround is d =
>> datetime.datetime(*(time.strptime(date_string,
>> format)[0:5])). However, when I try to use it, or even use it the
>> regular way, it fails with AttributeError: type object
>> 'datetime.datetime' has no attribute 'datetime'.

Works for me:

>>> import datetime
>>> format = '%Y%m%d_%H%M%S'
>>> import time


>>> print time.strptime('20080321_113405', format)[0:5]

(2008, 3, 21, 11, 34)


>>> d = datetime.datetime(*time.strptime('20080321_113405', format)[0:5])

>>> d
datetime.datetime(2008, 3, 21, 11, 34)

Python 2.4.4, Mac OS X 10.5.4.

Skip

Diez B. Roggisch

unread,
Sep 8, 2008, 6:52:57 AM9/8/08
to
W. eWatson wrote:

> Apparently, use of strptime of datetime needs a workaround in Python 2.4
> to work properly. The workaround is d =
> datetime.datetime(*(time.strptime(date_string, format)[0:5])). However,
> when I try to use it, or even use it the regular way, it fails with
> AttributeError: type object 'datetime.datetime' has no attribute
> 'datetime'.
> From the following code code segment:
>
> format = '%Y%m%d_%H%M%S'
> #d=datetime.strptime('20080321_113405', format)-- typical use
> print time.strptime('20080321_113405', format)[0:5]
> d = datetime.datetime(*time.strptime('20080321_113405', format)[0:5])
>
> Does anyone know how to make this work in 2.4? If not, is there a way to
> achieve the same result?

This is not what you think it is. All your problem is that you do

from datetime import datetime

which imports the datetime-class, but then try to access

datetime.datetime

as if you had done

import datetime.


This actually is a wart in the datetime-module - it would be better if the
classes in there would follow PEP-8.

Diez

W. eWatson

unread,
Sep 9, 2008, 12:27:08 AM9/9/08
to
That's it. Thanks.
0 new messages