Eliot Courtel
02/19/2025, 1:33 PMdef get_next_cron_run_time(current: datetime, interval_hours: int) -> datetime:
"""
Calculates the next scheduled run time rounded to an exact multiple of the interval.
Input:
current: datetime - The current time.
interval_hours: int - The interval in hours between runs.
Output:
A datetime object representing the next run time.
"""
floor_hour = current.hour - (current.hour % interval_hours)
base_time = current.replace(hour=floor_hour, minute=0, second=0, microsecond=0)
next_run = base_time + timedelta(hours=interval_hours)
if next_run <= current:
next_run += timedelta(hours=interval_hours)
return next_run
@flow
def example():
run_start = datetime.now()
interval_hours = 4
next_run = get_next_cron_run_time(run_start, interval_hours)
run_end = next_run - timedelta(minutes=1)
print(f"[{datetime.now()}] Flow will run until: {run_end}")
current_time = datetime.now()
while current_time < run_end:
# some code