Python: Given a Date and Weekday find the date of the next occurrence of a given weekday

爱⌒轻易说出口 提交于 2019-12-21 09:02:51

问题


This is a bit difficult to explain, so I apologize if this doesn't make much sense.

I have a program where I am doing some scheduling. One of the settings it has is to run a task weekly on certain days. For example, weekly on Monday, Wednesday and Friday.

Consider the example where the current task is scheduled for 1/2/2012, which is a Monday, I already have a bunch of code working to the point where I know the next task run should be on the wednesday following 1/2/2012. All I need to do is calculate the actual date of that Wednesday (1/4/2012).

I actually have the day of the week as the corresponding integer from date.weekday(), so in this case I have 2 which represents wednesday.

Whats the best way to handle something like this? I feel like there should be a fairly simple solution, but it isn't coming to mind. I was thinking of using a calendar object to search through to find the day of the week I want, but that seems like overkill.


回答1:


Use a timedelta to add to a date. For example, using some date d as the scheduled date for a current task, and some day next_day (the next day you'd like to run the task).

from datetime import date, timedelta
...
n = (next_day - d.weekday()) % 7 # mod-7 ensures we don't go backward in time
next_run_date = d + timedelta(days=n)



回答2:


Use dateutil.relativedelta:

from dateutil import relativedelta
import datetime

today = datetime.date.today()
# datetime.date(2012, 1, 3)

today + relativedelta.relativedelta(weekday=2) # 2 is Wednesday
# datetime.date(2012, 1, 4)

today + relativedelta.relativedelta(weekday=6) # 6 is Sunday
# datetime.date(2012, 1, 8)

today + relativedelta.relativedelta(weekday=1) # 1 is Tuesday
# datetime.date(2012, 1, 3)
# returns today

today + relativedelta.relativedelta(weeks=1, weekday=1)
# datetime.date(2012, 1, 10)
# returns Tuesday at least one week ahead



回答3:


We know that:

  • from Monday to Wednesday there is 2 days of difference.
  • from Wednesday to Friday there is 2 days of difference.
  • from Friday to Monday there is 3 days of difference.

So, this is as simple as adding the days of difference between scheduled tasks:

from datetime import datetime, timedelta
first_scheduled_task = datetime(year=2012, month=1, day=2)
second_scheduled_task = first_scheduled_task + timedelta(2)
third_scheduled_task = second_scheduled_task + timedelta(2)
fourth_scheduled_task = third_scheduled_task + timedelta(3)

This would give us as result:

>>> first_scheduled_task
datetime.datetime(2012, 1, 2, 0, 0) # Monday 1/2/2012
>>> second_scheduled_task
datetime.datetime(2012, 1, 4, 0, 0) # Wednesday 1/4/2012
>>> third_scheduled_task
datetime.datetime(2012, 1, 6, 0, 0) # Friday 1/6/2012
>>> fourth_scheduled_task
datetime.datetime(2012, 1, 9, 0, 0) # Monday 1/9/2012



回答4:


Use dateutil for working with scheduling and recurrences.



来源:https://stackoverflow.com/questions/8708058/python-given-a-date-and-weekday-find-the-date-of-the-next-occurrence-of-a-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!