Python2:检索周日 - 周六周 Start/End 给定日期范围内的日期
Python2: Retrieve Sunday - Saturday Week Start/End Dates For Given Date Range
有许多帖子解决了类似的问题,但其中 none 与我遇到的问题有相同的限制。
我正在编写一个脚本,用于从数据中心获取任意数周的数据。它获取的周数取决于外部用户提供给我的脚本的日期范围。数据中心的一周从周日到周六。 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
]
您应该可以使用 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))
添加到 "current week" 以获得当前日期或之后的 second 星期日(因为我的 "week start" 始终是周日,总是下一个周日)。
对于我生成的每个日期,我只是将relativedelta(weekday=SA)
添加到它以生成即将到来的星期六,如果我在日期范围之外,我"clip"最后一个日期作为日期范围。
使用您的示例:
>>> 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 语句中 zip_longest
周围的 list()
包装。
这里有一个不那么冗长但简洁的答案。
import datetime as dt
if __name__ == "__main__":
weekend_index = (6, 5) # Sunday, Saturday
requested_range = (dt.datetime(2016, 7, 9, 0, 0), dt.datetime(2016, 8, 11, 0, 0))
start, end = requested_range
sun, sat = weekend_index
cur = start
my_range = []
while cur < end:
cr = []
cr.append(cur)
cur = end if end < cur+dt.timedelta(days=6) else (cur+dt.timedelta(days=(sun if cur.weekday() == sun else (sat-cur.weekday()))))
cr.append(cur)
cur += dt.timedelta(days=1)
my_range.append(cr)
print(my_range) # Returns:
# [[datetime.datetime(2016, 7, 9, 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, 11, 0, 0)]]
有许多帖子解决了类似的问题,但其中 none 与我遇到的问题有相同的限制。
我正在编写一个脚本,用于从数据中心获取任意数周的数据。它获取的周数取决于外部用户提供给我的脚本的日期范围。数据中心的一周从周日到周六。 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
]
您应该可以使用 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))
添加到 "current week" 以获得当前日期或之后的 second 星期日(因为我的 "week start" 始终是周日,总是下一个周日)。
对于我生成的每个日期,我只是将relativedelta(weekday=SA)
添加到它以生成即将到来的星期六,如果我在日期范围之外,我"clip"最后一个日期作为日期范围。
使用您的示例:
>>> 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 语句中 zip_longest
周围的 list()
包装。
这里有一个不那么冗长但简洁的答案。
import datetime as dt
if __name__ == "__main__":
weekend_index = (6, 5) # Sunday, Saturday
requested_range = (dt.datetime(2016, 7, 9, 0, 0), dt.datetime(2016, 8, 11, 0, 0))
start, end = requested_range
sun, sat = weekend_index
cur = start
my_range = []
while cur < end:
cr = []
cr.append(cur)
cur = end if end < cur+dt.timedelta(days=6) else (cur+dt.timedelta(days=(sun if cur.weekday() == sun else (sat-cur.weekday()))))
cr.append(cur)
cur += dt.timedelta(days=1)
my_range.append(cr)
print(my_range) # Returns:
# [[datetime.datetime(2016, 7, 9, 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, 11, 0, 0)]]