Syntax Problem with strptime in Python 2.4

W

W. eWatson

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/>
 
S

skip

>> 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
 
S

skip

>> 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
 
D

Diez B. Roggisch

W. eWatson said:
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
 
D

Diez B. Roggisch

W. eWatson said:
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

W. eWatson

Diez said:
W. eWatson said:
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
That's it. Thanks.

--
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/>
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top