Cron Expression Parser and Evaluator

2.7 · active · verified Thu Apr 09

cronsim is a Python library (current version 2.7) for parsing cron expressions and simulating their execution. It allows calculating the next scheduled run time based on a given start date. It receives low but steady maintenance, with updates for bug fixes and minor features.

Warnings

Install

Imports

Quickstart

Initialize CronSim with a cron expression and use the `.next()` method to find the next scheduled run after a given datetime object.

from datetime import datetime
from cronsim import CronSim

# Daily at midnight
cron_expression = "0 0 * * *"
sim = CronSim(cron_expression)

# Get the next run after a specific date
start_date = datetime(2023, 1, 1, 10, 30, 0)
next_run = sim.next(start_date)
print(f"Cron expression: '{cron_expression}'")
print(f"Next run after {start_date}: {next_run}")

# Example with a more frequent schedule (every 15 minutes)
cron_expression_2 = "*/15 * * * *"
sim_2 = CronSim(cron_expression_2)
start_date_2 = datetime(2024, 7, 15, 14, 0, 0)
next_run_2 = sim_2.next(start_date_2)
print(f"\nCron expression: '{cron_expression_2}'")
print(f"Next run after {start_date_2}: {next_run_2}")

view raw JSON →