Hi folks,


(Python 2.4.x - I cannot install any modules including dateutil)


Here's what I have so far.

mydate=['20111025.00', '20111026.12']

start_time= time.strptime(mydate[0], "%Y%m%d.%H")[:5]
end_time=time.strptime(mydate[1], "%Y%m%d.%H")[:5]

for myhour in datespan(time.strptime(mydate[0], "%Y%m%d.%H")[:4], time.strptime(mydate[1], "%Y%m%d.%H")[:4], delta=datetime.timedelta(hours=1)):
    print myhour

What I'm trying to do is take the two values in mydate list - and show all hours inbetween the two values given. in the above example it would (or should) show.

20111025.00
20111025.01
20111025.02
...
20111026.12

Problem with the above code is it tells me.

can only concatenate tuple (not "datetime.timedelta") to tuple

Just can't make heads or tails out of it. Any suggestions?

Thank you.

Recommended Answers

All 5 Replies

I seems to me that you add a datetime.timedelta of one hour to a datetime.datetime to get a new datetime.datetime.

Grib means this:

import datetime

mydate=['20111025.00', '20111026.12']
fmt = "%Y%m%d.%H"
hour = datetime.timedelta(hours=1)

start_time,  end_time = [datetime.datetime.strptime(d, fmt) for d in mydate]

now = start_time
while now <= end_time:
    print now.strftime(fmt)
    now += hour

Doing this manually by normal loop is also not so tough job.

hmmmmm,

Now it's popping an error on me.

AttributeError: type object 'datetime.datetime' has no attribute 'strptime'

is datetime.strptime new in 2.5? I've got 2.4.x

man geez finally.

I saw that too - read right the heck over it. Thanks for the splash of water to the face.

working code.

import datetime, time

mydate=['20111025.00', '20111026.12']
fmt = "%Y%m%d.%H"
hour = datetime.timedelta(hours=1)
start_time, end_time = [datetime.datetime(*(time.strptime(d, fmt)[0:6])) for d in mydate]

now = start_time
while now <= end_time:
    print now.strftime(fmt)
    now += hour
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.