Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Crontab Parsing by tokiojapan55
import re
from datetime import datetime, timedelta
def next_crontab_exec(crontab_time: str, current_time_str: str) -> list[str]:
special = {
"@yearly": "0 0 1 1 *",
"@annually": "0 0 1 1 *",
"@monthly": "0 0 1 * *",
"@weekly": "0 0 * * 0",
"@daily": "0 0 * * *",
"@midnight": "0 0 * * *",
"@hourly": "0 * * * *",
}
if crontab_time in special:
crontab_time = special[crontab_time]
def parse(field, start, end):
result = re.fullmatch(r'([\d\-,*]+)(/(\d+))?', field)
number, step = result.group(1), result.group(3)
if number.isdigit():
return [int(number)]
if ',' in number:
return sorted(map(int, number.split(',')))
if '-' in number:
start, end = map(int, number.split('-'))
return list(range(start, end + 1, int(step or '1')))
fields = [parse(f, s, e) for f, (s, e) in zip(
crontab_time.split(), [(0, 59), (0, 23), (1, 31), (1, 12), (0, 6)])]
result, t = [], datetime.fromisoformat(current_time_str) + timedelta(minutes=1)
while len(result) < 3:
if t.month not in fields[3]:
t = t.replace(minute=fields[0][0], hour=fields[1][0], day=1,
month=t.month % 12 + 1, year=t.year + (t.month == 12))
elif t.day not in fields[2] or (t.isoweekday() % 7) not in fields[4]:
t = t.replace(minute=fields[0][0], hour=fields[1][0])
t += timedelta(days=1)
elif t.hour not in fields[1]:
t = t.replace(minute=fields[0][0])
t += timedelta(hours=1)
else:
if t.minute in fields[0]:
result.append(t.isoformat(timespec='minutes'))
t += timedelta(minutes=1)
return result
March 6, 2025
Comments: