5

我有一个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 方法来实现这一点吗?

4

2 回答 2

7

使用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
于 2012-09-08T18:52:03.263 回答
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) )

这里的主要假设是您最初指定的 timedelta 不是负数。[(x,y)]如果y < x你这样做,你会得到一个单块列表。

于 2012-09-08T18:07:56.490 回答