有没有一种更优雅的/Python式的方法来完成标题所说的和这个函数所做的?在
def split_datetime_range(start, end, split):
"""Splits a range of dates into a list of equal ranges
with remaining time allocated to the last of the series.
This function doesn't overlap dates, so seconds are lost
inbetween each range
Parameters:
start - The start of the range
end - The end of the range
split - How many ranges to produce
Returns:
A list of tuples, each tuple is its own range
"""
total_seconds = int((end - start).total_seconds())
delta = total_seconds / split
starts = [start + timedelta(seconds=delta * i) for i in range(split)]
ends = [s + timedelta(seconds=delta - 1) for s in starts]
ends[len(ends) - 1] = end
return zip(starts, ends)
编辑-
一个限制是它必须是“秒”分辨率,而不是“微秒”分辨率。其思想是生成一个日期范围列表,以提供给接受iso8601中具有秒分辨率的日期的web服务
你提到了
itertools
-所以这里有一些你可以玩的东西-但是没有测试过:我不确定这是否符合你的要求。在
如果您希望iso格式不包含毫秒,请给定日期时间对象“now”:
^{pr2}$相关问题 更多 >
编程相关推荐