有没有更优雅/pythonic的方式来做标题所说的和这个函数的作用?
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 服务