0

I want to make a function that will take start_time and end_time and that will find the time interval where those times fit. Time range must be at every 5 minutes, but imputed time interval can be any other time.

I have these intervals (I made them with a function below):

00:00:00
00:05:00
00:10:00
00:15:00
00:20:00
00:25:00
00:30:00
00:35:00
00:40:00
00:45:00
00:50:00
00:55:00

import datetime
import pandas as pd
import time

# creating time intervals at every 5 minutes
def find_interval(start_time, end_time):
    start_time = "00:00:00"
    end_time = "0:59:59"

    start = datetime.datetime.strptime(start_time,  '%H:%M:%S')
    end = datetime.datetime.strptime(end_time, '%H:%M:%S')
    step = datetime.timedelta(minutes=5)

    time_intervals = []

    while start <= end:
        time_intervals.append(start.time())
        #print(start.time())
        start += step

    #print(time_intervals)

What should I do, so that when user enters start_time and end_time, for example 00:13:24 and 00:22:41, i get the result: [00:10:00, 00:15:00, 00:20:00, 00:25:00]

taga
  • 3,537
  • 13
  • 53
  • 119

3 Answers3

2

try this:

import datetime
import time


# creating time intervals at every 5 minutes
def create_intervals():
    start_time = "00:00:00"
    end_time = "0:59:59"

    start = datetime.datetime.strptime(start_time, '%H:%M:%S')
    end = datetime.datetime.strptime(end_time, '%H:%M:%S')
    step = datetime.timedelta(minutes=5)

    time_intervals = []

    while start <= end:
        time_intervals.append(start.time())
        # print(start.time())
        start += step
    return time_intervals


def find_intervals(start_time, end_time, intervals):
    start = datetime.datetime.strptime(start_time, '%H:%M:%S').time()
    end = datetime.datetime.strptime(end_time, '%H:%M:%S').time()

    first = next(i for i, v in enumerate(intervals) if start <= v) - 1
    last = next(i for i, v in enumerate(intervals) if end <= v) + 1

    return intervals[first:last]


intervals = create_intervals()

result = [str(i) for i in find_intervals("00:13:24", "00:22:41", intervals)]
print(result)

I'm using your function to create the interval list, then from the input times I find the first and last time that "cover" the input times, and return them.

to print it in the format you requested, I cast the datetime.time object to a str.

Output:

['00:10:00', '00:15:00', '00:20:00', '00:25:00']

Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
  • Thanks a lot, can you tell why did you use `next`, what does `next ` do? – taga Jun 26 '19 at 16:02
  • it is like a single iteration of `for`, I used it to get the first element that matches the condition, without having to create an entire list and pick the first element (which can be memory problematic of the list is very big) – Adam.Er8 Jun 26 '19 at 17:19
  • took it from this answer: https://stackoverflow.com/questions/2361426/get-the-first-item-from-an-iterable-that-matches-a-condition – Adam.Er8 Jun 26 '19 at 17:20
1

The only things you should think about is how to transform 00:13:41 to 00:10:00 and 00:22:41 to 00:25:00.

import pandas as pd

# this function round to previous or next 5M
def round5M(tm, PoN):
    PoN = 0 if PoN is "previous" else 1
    return "00:{:02}:00".format((int(tm[3:5])//5 + PoN) * 5)

start_time = "00:13:24"
end_time = "00:22:41"

out = pd.timedelta_range(start=round5M(start_time, "previous"),
                         end=round5M(end_time, "next"),
                         freq="5T")\
        .astype(str)\
        .to_list()


print(out)
['00:10:00', '00:15:00', '00:20:00', '00:25:00']
rpanai
  • 12,515
  • 2
  • 42
  • 64
  • THIS IS AWESOME! – Adam.Er8 Jun 26 '19 at 13:31
  • 1
    @Adam.Er8 Thanks. I'm not that happy about `round5M` I hope there is a nicer way to do the same. – rpanai Jun 26 '19 at 13:34
  • I have an idea, to actually build the full intervals list using `pd.date_range`, and sort of "query" it using panda conditionals – Adam.Er8 Jun 26 '19 at 13:42
  • 1
    @Adam.Er8 I guess one could play with something like `df['time'].dt.round('15min') ` showed [here](https://stackoverflow.com/questions/32344533/how-do-i-round-datetime-column-to-nearest-quarter-hour) – rpanai Jun 26 '19 at 13:45
  • 1
    If you import it you better take all the juice from it. – rpanai Jun 26 '19 at 14:06
0

a completely different approach, based on @rpanai's answer and pd.date_range:

import pandas as pd
import datetime

intervals = pd.date_range('00:00', '01:00:00', freq='5min').time

start_time, end_time = "00:13:24", "00:22:41"
start = (datetime.datetime.strptime(start_time, '%H:%M:%S') - datetime.timedelta(minutes=5)).time()
end = (datetime.datetime.strptime(end_time, '%H:%M:%S') + datetime.timedelta(minutes=5)).time()

print(intervals[(intervals >= start) & (intervals <= end)].astype(str))

Output:

['00:10:00' '00:15:00' '00:20:00' '00:25:00']

Adam.Er8
  • 12,675
  • 3
  • 26
  • 38