我想创建一个时间戳元组。 如果我定义类似
的函数def time_stamps(starttime,endtime,intervals):
#start time of tuple like 08:00:00
#End time of tuple like 21:00:00
#With Intervals of 60 or 30 0r 90 minutes.
#example : if i call the function
#x = time_stamps(08:00:00,21:00:00,00:30:00)
#It should generate a tuple like this
#('08:00:00','08:30:00','09:00:00',....,'20:30:00','21:00:00)
我迷路了。请帮助。 谢谢
答案 0 :(得分:1)
使用dateutils's parser
以字符串格式将时间解析为datetime
对象。然后,我们可以轻松地基于时间增量计算计数,从而生成一个元组
from dateutil import parser
def time_stamps(starttime,endtime,intervals):
start = parser.parse(starttime)
end = parser.parse(endtime)
intervals = parser.parse(intervals) - parser.parse('00:00:00')
count = int((end-start)/intervals)
return tuple(( str((start+i*intervals).time()) for i in range(count+1)))
>>> time_stamps('10:00:00', '12:00:00', '00:30:00')
('10:00:00', '10:30:00', '11:00:00', '11:30:00', '12:00:00')