在Python中找到推算时间间隔的时间范围

时间:2019-06-26 12:57:19

标签: python

我想创建一个函数,该函数将使用start_timeend_time,并将找到适合这些时间的时间间隔。 时间范围必须是每5分钟一次,但是估算的时间间隔可以是任何其他时间。

我有这些间隔(我通过下面的函数来设置它们):

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)

我应该怎么做,以便当用户输入start_timeend_time时, 例如00:13:2400:22:41,我得到的结果是: [00:10:00, 00:15:00, 00:20:00, 00:25:00]

3 个答案:

答案 0 :(得分:2)

尝试一下:

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)

我正在使用您的函数来创建间隔列表,然后从输入时间中查找“覆盖”输入时间并返回它们的第一次和最后一次。

以您要求的格式打印它,我将datetime.time对象转换为str

输出:

  

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

答案 1 :(得分:1)

您唯一需要考虑的就是如何将00:13:41转换为00:10:00,并将00:22:41转换为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']

答案 2 :(得分:0)

基于@rpanai's answerpd.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))

输出:

  

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