Python2:检索星期日 - 星期六星期开始/结束日期的给定日期范围

时间:2016-08-24 23:04:13

标签: python-2.7 datetime python-dateutil

有许多帖子可以解决类似的问题,但是没有一个帖子与我的问题有相同的限制。

我正在编写一个脚本,可以从数据中心获取任意数周的数据。它取出的周数取决于外部用户提供给我的脚本的日期范围。数据中心的周从周日到周六。 Python的周从周一到周日运行。

我需要能够获取日期范围内每个日期之前的星期日和星期六的日期。更复杂的是,周开始日期和周结束日期都不能超出要求的范围。这使我无法简单地从范围中的每个日期减去一天。

一些示例场景:

示例1)

requested_date_range = [datetime(2016,7,1,0,0),datetime(2016,8,5,0,0)]
what I get from the various Python utilities (dateutil, datetime_periods, etc):

[
[datetime(2016,6,27,0,0),datetime(2016,7,3,0,0)],
[datetime(2016,7,4,0,0),datetime(2016,7,10,0,0)],
[datetime(2016,7,11,0,0),datetime(2016,7,17,0,0)],
[datetime(2016,7,18,0,0),datetime(2016,7,24,0,0)],
[datetime(2016,7,25,0,0),datetime(2016,7,31,0,0)],
[datetime(2016,8,1,0,0),datetime(2016,8,7,0,0)]
]

what I actually need:
[
[datetime(2016,7,1,0,0),datetime(2016,7,2,0,0)], #"week" starts on first day of requested range and ends on the following Saturday
[datetime(2016,7,3,0,0),datetime(2016,7,9,0,0)], #Sunday through Saturday
[datetime(2016,7,10,0,0),datetime(2016,7,16,0,0)], #Sunday through Saturday
[datetime(2016,7,17,0,0),datetime(2016,7,23,0,0)], #Sunday through Saturday
[datetime(2016,7,24,0,0),datetime(2016,7,30,0,0)], #Sunday through Saturday
[datetime(2016,7,31,0,0),datetime(2016,8,5,0,0)] #"week" starts on Sunday and ends on last day of requested range
] 

示例2)

requested_date_range = [datetime(2016,7,3,0,0),datetime(2016,8,7,0,0)]
what I get from the various Python utilities (dateutil, datetime_periods, etc):
[
[datetime(2016,6,27,0,0),datetime(2016,7,3,0,0)],
[datetime(2016,7,4,0,0),datetime(2016,7,10,0,0)],
[datetime(2016,7,11,0,0),datetime(2016,7,17,0,0)],
[datetime(2016,7,18,0,0),datetime(2016,7,24,0,0)],
[datetime(2016,7,25,0,0),datetime(2016,7,31,0,0)],
[datetime(2016,8,1,0,0),datetime(2016,8,7,0,0)]
]
what I actually need: 
[
[datetime(2016,7,3,0,0),datetime(2016,7,9,0,0)], #"week" starts on first day of requested range
[datetime(2016,7,10,0,0),datetime(2016,7,16,0,0)], #Sunday through Saturday
[datetime(2016,7,17,0,0),datetime(2016,7,23,0,0)], #Sunday through Saturday
[datetime(2016,7,24,0,0),datetime(2016,7,30,0,0)], #Sunday through Saturday
[datetime(2016,7,31,0,0),datetime(2016,8,6,0,0)], #Sunday through Saturday
[datetime(2016,8,7,0,0),datetime(2016,8,7,0,0)]  #"week" ends up being only one day long because the max requested date falls on a Sunday
]

2 个答案:

答案 0 :(得分:1)

您应该可以使用dateutil.relativedelta轻松完成此操作。下面是一个示例函数:

from dateutil.relativedelta import relativedelta
from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU

def week_range(range_start, range_end):
    dts = []
    WEEK_START = relativedelta(weekday=SU(+2))
    WEEK_END = relativedelta(weekday=SA)

    c_wstart = range_start + relativedelta(weekday=SU(+1))
    c_wend = c_wstart + WEEK_END

    if range_start < c_wstart:
        dts.append((range_start, range_start + WEEK_END))

    while True:
        if c_wend > range_end:
            c_wend = range_end

        dts.append((c_wstart, c_wend))

        if c_wend >= range_end:
            break

        c_wstart = c_wstart + WEEK_START
        c_wend = c_wstart + WEEK_END

        if c_wstart > range_end:
            break

    return dts

在上面的函数中,我首先从范围开始并向其添加relativedelta(weekday=SU),这为我提供了原始日期或之后的第一个星期日。然后我连续将relativedelta(weekday=SU(+2))添加到“当前周”以在当前日期或之后获得第二星期日(由于我的“周开始”始终是星期日,因此始终是下周日)。

对于我生成的每个日期,我只需添加relativedelta(weekday=SA)即可生成即将到来的星期六,如果我超出日期范围,我会将最后一个日期“剪辑”为日期范围。

使用您的示例:

>>> week_range(datetime(2016, 7, 1), datetime(2016, 8, 5))
[(datetime.datetime(2016, 7, 1, 0, 0), datetime.datetime(2016, 7, 2, 0, 0)),
 (datetime.datetime(2016, 7, 3, 0, 0), datetime.datetime(2016, 7, 9, 0, 0)),
 (datetime.datetime(2016, 7, 10, 0, 0), datetime.datetime(2016, 7, 16, 0, 0)),
 (datetime.datetime(2016, 7, 17, 0, 0), datetime.datetime(2016, 7, 23, 0, 0)),
 (datetime.datetime(2016, 7, 24, 0, 0), datetime.datetime(2016, 7, 30, 0, 0)),
 (datetime.datetime(2016, 7, 31, 0, 0), datetime.datetime(2016, 8, 5, 0, 0))]
>>> week_range(datetime(2016, 7, 3), datetime(2016, 8, 7))
[(datetime.datetime(2016, 7, 3, 0, 0), datetime.datetime(2016, 7, 9, 0, 0)),
 (datetime.datetime(2016, 7, 10, 0, 0), datetime.datetime(2016, 7, 16, 0, 0)),
 (datetime.datetime(2016, 7, 17, 0, 0), datetime.datetime(2016, 7, 23, 0, 0)),
 (datetime.datetime(2016, 7, 24, 0, 0), datetime.datetime(2016, 7, 30, 0, 0)),
 (datetime.datetime(2016, 7, 31, 0, 0), datetime.datetime(2016, 8, 6, 0, 0)),
 (datetime.datetime(2016, 8, 7, 0, 0), datetime.datetime(2016, 8, 7, 0, 0))]

根据您的喜好,您还可以使用rruleset

完成类似的工作
from dateutil.rrule import rrule, rruleset
from dateutil.rrule import WEEKLY, SU, SA
from datetime import timedelta

from itertools import zip_longest, chain
def week_range_rrule(range_start, range_end, weekday_start=SU, weekday_end=SA):
    # Beginning of the week rule
    rr1 = rrule(WEEKLY, byweekday=weekday_start,
                dtstart=range_start, until=range_end)

    # End of the week rule - adding 1 second to the range end because
    # "until" isn't inclusive
    rr2 = rrule(WEEKLY, byweekday=weekday_end,
                dtstart=range_start+relativedelta(SA),
                until=range_end+timedelta(seconds=1))

    # Combine these into a rule set
    rrs = rruleset()
    rrs.rrule(rr1)
    rrs.rrule(rr2)

    # Explicitly add range start and end to the rules, in case they don't
    # fall on neat week boundaries
    rrs.rdate(range_start)
    rrs.rdate(range_end)

    if next(iter(rr2)) == range_start:
        rrs = chain((range_start, ), rrs)

    # Modified version of the "grouper" recipe from itertools
    args = [iter(rrs)] * 2

    return list(zip_longest(*args, fillvalue=range_end))

请注意,如果您希望第一个延迟,请将dts.append(x)的所有实例替换为yield x。如果你想让第二个延迟,只需删除return语句中list()周围的zip_longest包装器。

答案 1 :(得分:1)

这里有一个稍微冗长,虽然简洁的答案。

<div class="model-dialog">
        <div class="model-content">