Cron-converter is a Python library that provides a cron string parser and scheduler. It allows parsing cron expressions from strings or lists and iterating over datetime objects in a cron-like format. It is a transposition of the JavaScript cron-converter library and is currently at version 1.3.1, actively maintained with regular releases.
pip install cron-converterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a `Cron` object with a cron string, retrieve its string and list representations, and iterate over upcoming scheduled times using the `schedule` method. It also shows how to use constructor options for named output and highlights the importance of using `itertools.islice` or similar mechanisms when iterating over the (infinite) schedule.
Wrap the `schedule()` call with `itertools.islice(cron_instance.schedule(start_date), num_iterations)` or add manual break logic within your loop.
When calling `cron_instance.schedule()`, pass a `datetime` object with `tzinfo` set, e.g., `datetime.now(pytz.timezone('America/New_York'))` or `datetime.now(dateutil.tz.gettz('Asia/Tokyo'))` if using `python-dateutil`.Update imports and refactor code to use `cron-converter`'s `Cron` class and `schedule()` method. Refer to `cron-converter` documentation for specific API changes.
Pass a dictionary of options to the `Cron` constructor: `cron_instance = Cron('...', {'output_weekday_names': True})`.Review and correct the cron expression to ensure it follows the standard 5-field (or 6-field with seconds) cron syntax, e.g., '*/5 * * * *' for every five minutes.
Specify the full path to the Python interpreter (e.g., from your virtual environment) in the crontab entry, or ensure that `cron-converter` is installed in the system's default Python environment that cron uses.
Implement a mechanism to limit the iteration, such as using `itertools.islice()` to get a fixed number of future occurrences, or add a `break` statement based on a specific condition (e.g., a target end date).