croniter
croniter is a Python library that provides iteration capabilities for `datetime` objects based on cron-like formats. It allows generating future or past occurrences matching a given cron expression. The current version is 6.2.2, released on March 15, 2026. While it experienced a period of abandonment, it is now actively maintained by the Pallets-Eco organization.
Warnings
- breaking The `croniter` project was previously declared unmaintained and abandoned by its original author in late 2024 due to concerns about the EU Cyber Resilience Act. It has since been adopted and is now maintained by Pallets-Eco. Users should ensure they are using the `pallets-eco` version and monitor its status if the CRA impacts open source libraries.
- gotcha By default, `croniter` uses 'OR' logic when both 'day of month' and 'day of week' fields are specified in a cron expression, matching standard Vixie-cron behavior. This means a schedule will trigger if *either* the day of month *or* the day of week matches. For 'AND' logic (trigger only if *both* match, like fcron), you must explicitly pass `day_or=False` to the `croniter` constructor.
- gotcha `croniter` is less strict than some cron implementations regarding ranges for months (e.g., 'APR-JAN') and days of the week (e.g., 'SAT-SUN', 'WED-SUN'). These reverse ranges are interpreted, which might lead to unexpected iterations if a strict cron parser is anticipated.
- gotcha The `is_valid()` method, by default, only performs basic syntax and field range validation. It does not perform cross-field validation (e.g., it will report '31 2 * * *' as valid even though February has no 31st). To enable strict cross-field validation, you must pass `strict=True`.
- gotcha The `croniter.match()` method has a default precision of 60 seconds for 5-field expressions and 1 second for 6-field expressions. This means a target `datetime` up to 60 (or 1) seconds *after* the scheduled time will still be considered a match. This can cause issues in scenarios requiring exact time matching.
Install
-
pip install croniter
Imports
- croniter
from croniter import croniter
Quickstart
from datetime import datetime
from croniter import croniter
# Example 1: Every 5 minutes
base = datetime(2010, 1, 25, 4, 46)
iter_5min = croniter('*/5 * * * *', base)
print(f"Next occurrence (every 5 min): {iter_5min.get_next(datetime)}")
# Example 2: Every Monday and Friday at 04:02
base = datetime.now()
iter_mf = croniter('2 4 * * mon,fri', base)
print(f"Next occurrence (Mon/Fri 04:02): {iter_mf.get_next(datetime)}")
# Example 3: First day of the month AND a Wednesday
# (using day_or=False for AND logic, like fcron)
base = datetime.now()
iter_and = croniter('2 4 1 * wed', base, day_or=False)
print(f"Next occurrence (1st day AND Wed 04:02): {iter_and.get_next(datetime)}")