Created
September 1, 2010 17:31
-
-
Save jbochi/561045 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schedule = a_periodic_task.run_every | |
next = next_ocurrance(schedule.minute, schedule.hour, schedule.day_of_week) | |
def next_ocurrance(minutes, hours, days_of_week): | |
# days_of_week convention: Sunday = 0, Saturday = 6 | |
# dateutil convention: Monday = 0, Sunday = 6 | |
now = datetime.datetime.now() | |
weekday = now.isoweekday() | |
execute_this_hour = weekday in days_of_week \ | |
and now.hour in hours \ | |
and now.minute < max(minutes) | |
if execute_this_hour: | |
next_minute = min([minute for minute in minutes | |
if minute > now.minute]) | |
return now + dr.relativedelta(minute=next_minute, | |
second=0, | |
microsecond=0) | |
else: | |
next_minute = min(minutes) | |
execute_today = weekday in days_of_week \ | |
and (now.hour < max(hours) or execute_this_hour) | |
if execute_today: | |
next_hour = min([hour for hour in hours if hour > now.hour]) | |
return now + dr.relativedelta(hour=next_hour, | |
minute=next_minute, | |
second=0, | |
microsecond=0) | |
else: | |
next_hour = min(hours) | |
next_day = min([day for day in days_of_week if day > weekday] \ | |
or days_of_week) | |
return now + dr.relativedelta(weekday=(next_day - 1) % 7, | |
hour=next_hour, | |
minute=next_minute, | |
second=0, | |
microsecond=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment