将Python日期时间拆分为小时对齐的块

时间:2012-09-08 17:59:55

标签: python datetime split

我有一个datetime.datetime实例,d和一个datetime.timedelta实例td,我正在尝试编写一个将范围(d, d+td)拆分为的函数[(d,x1),(x1,x2),...,(xn,d+td)] xn变量全部与小时对齐。

例如,如果

d = datetime.datetime(2012, 9, 8, 18, 53, 34)
td = datetime.timedelta(hours=2, minutes=34, seconds=5)

我想要一份

列表
[(datetime.datetime(..., 18, 53, 34), datetime.datetime(..., 19, 0, 0)),
 (datetime.datetime(..., 19, 0, 0), datetime.datetime(..., 20, 0, 0)),
 (datetime.datetime(..., 20, 0, 0), datetime.datetime(..., 21, 0, 0)),
 (datetime.datetime(..., 21, 0, 0), datetime.datetime(..., 21, 27, 39))]

任何人都可以建议一个好的,Pythonic,实现这个的手段吗?

2 个答案:

答案 0 :(得分:6)

使用dateutil,您可以使用rrule生成列表:

import dateutil.rrule as rrule
import datetime

def hours_aligned(start, end, inc = True):
    if inc: yield start
    rule = rrule.rrule(rrule.HOURLY, byminute = 0, bysecond = 0, dtstart=start)
    for x in rule.between(start, end, inc = False):
        yield x
    if inc: yield end

d = datetime.datetime(2012, 9, 8, 18, 53, 34)
td = datetime.timedelta(hours=2, minutes=34, seconds=5)

for x in hours_aligned(d,d+td):
    print(x)

产量

2012-09-08 18:53:34
2012-09-08 19:00:00
2012-09-08 20:00:00
2012-09-08 21:00:00
2012-09-08 21:27:39

答案 1 :(得分:1)

chunks = []
end = d + td

current = d
# Set next_current to the next hour-aligned datetime
next_current = (d + datetime.timedelta(hours=1)).replace(minute=0, second=0)

# Grab the start block (that ends on an hour alignment)
# and then any full-hour blocks
while next_current < end:
    chunks.append( (current, next_current) )

    # Advance both current and next_current to the following hour-aligned spots
    current = next_current
    next_current += datetime.timedelta(hours=1)

# Grab any remainder as the last segment
chunks.append( (current, end) )

这里的主要假设是你的初始指定时间delta不是负数。如果您这样做,您将获得单个列表[(x,y)] y < x