wagner-felix
wagner-felix

Reputation: 875

Checking for minute value with datetime in python

lets say i have a start_date

start_date = datetime.time(7, 55, 56)

and i created a time list:

timelist = []
for y in range(0, 24, 1):
    for x in range(0, 60, 5):
        start = datetime.time(y, x)
        timelist.append(start)

timelist = [datetime.time(0, 0), 
            datetime.time(0, 10), 
            datetime.time(0, 20), 
            datetime.time(0, 30), 
            datetime.time(0, 40), 
            datetime.time(0, 50), 
            datetime.time(1, 0),
            ...]

x.hour returns [0, 1, 2, ..., 23] x.minute returns [0, 10, 20, 30, 40, 50]

I then want to create a dictionary { [time: 00:00:00, value: 0], [time: 00:10:00, value=0], ... }

now time is x. and the value is created by checking when the start time is:

For instance:

start_date = 07:55:56 → at 07:50:00 value = 1
start_date = 08:03:22 → at 08:00:00 value = 1
start_date = 08:18:00 → at 08:10:00 value = 1

else the value is 0

I need help creating the if statement for checking the minute.

So far I have:

for x in timelist:
    if x.hour == start_date.hour:
        ...

Upvotes: 0

Views: 106

Answers (1)

lucemia
lucemia

Reputation: 6617

a little bit hard to understand what are asking.

Do you want to know which time interval the start_date belong to?

If so:

timelist = []
for y in range(0, 24, 1):
    for x in range(0, 60, 10):     
        # should be ten? 
        start = datetime.time(y, x)
        timelist.append(start)


timedict = {}
for start in timelist:
    timedict[start] = 0
# is it the dictionary looking for?

start_date = datetime.time(7, 55, 56)

for i in range(len(timelist)):
    if timelist[i] <= start_date:
        if i == len(timelist)-1 or start_date < timelist[i+1]
            timedict[timelist[i]] += 1  # or = 1, depend on your usage

Upvotes: 1

Related Questions