Skip to content

Instantly share code, notes, and snippets.

@timkpaine
Created July 25, 2019 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timkpaine/09386bb62673d0fd38a76856a1dcb8e3 to your computer and use it in GitHub Desktop.
Save timkpaine/09386bb62673d0fd38a76856a1dcb8e3 to your computer and use it in GitHub Desktop.
def interval_to_chron(interval, start_time):
if interval == Interval.MINUTELY:
# simple
return '*/1 * * * *'
elif interval == Interval.FIVE_MINUTES:
# simple
'*/5 * * * *'
elif interval == Interval.TEN_MINUTES:
return '{start_minute_mod_ten}-59/10 * * * *'.format(start_minute_mod_ten=start_time.minute % 10)
elif interval == Interval.THIRTY_MINUTES:
return '{start_minute_mod_thirty}-59/30 * * * *'.format(start_minute_mod_thirty=start_time.minute % 30)
elif interval == Interval.HOURLY:
return '{start_minute} */1 * * *'.format(start_minute=start_time.minute)
elif interval == Interval.TWO_HOURS:
return '{start_minute} {start_hour_mod_two}-23/2 * * *'.format(start_minute=start_time.minute, start_hour_mod_two=start_time.hour % 2)
elif interval == Interval.THREE_HOURS:
return '{start_minute} {start_hour_mod_three}-23/3 * * *'.format(start_minute=start_time.minute, start_hour_mod_three=start_time.hour % 3)
elif interval == Interval.SIX_HOURS:
return '{start_minute} {start_hour_mod_six}-23/6 * * *'.format(start_minute=start_time.minute, start_hour_mod_six=start_time.hour % 6)
elif interval == Interval.TWELVE_HOURS:
return '{start_minute} {start_hour_mod_twelve}-23/12 * * *'.format(start_minute=start_time.minute, start_hour_mod_twelve=start_time.hour % 12)
elif interval == Interval.DAILY:
return '{start_minute} {start_hour} */1 * *'.format(start_minute=start_time.minute, start_hour=start_time.hour)
elif interval == Interval.WEEKLY:
return '{start_minute} {start_hour} */1 */1 {day_of_week}'.format(start_minute=start_time.minute, start_hour=start_time.hour, day_of_week=start_time.strftime('%a'))
elif interval == Interval.MONTHLY:
return '{start_minute} {start_hour} {start_day} */1 *'.format(start_minute=start_time.minute, start_hour=start_time.hour, start_day=start_time.day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment