Cron-converter provides a Cron string parser ( from string/lists to string/lists ) and iteration for the datetime object with a cron like format.
This project would be a transposition in Python of JS cron-converter by roccivic.
Get the schedule execution times. Example with raw Datetime
# Parse a string to init a schedule
cron_instance.from_string('*/5 * * * *')
# Raw datetime without timezone info (not aware)
reference = datetime.now()
# Get the iterator, initialised to now
schedule = cron_instance.schedule(reference)
# Calls to .next() and .prev()
# return a Datetime object
# Examples with time now: '2021-01-01T09:32:00
# Prints: '2021-01-01T09:35:00'
print(schedule.next().isoformat())
# Prints: '2021-01-01T09:40:00'
print(schedule.next().isoformat())
# Reset
schedule.reset()
# Prints: '2021-01-01T09:30:00'
print(schedule.prev().isoformat())
# Prints: '2021-01-01T09:25:00'
print(schedule.prev().isoformat())
Using the Iterator Protocol
Starting from version 1.3, the Seeker object implements Python's Iterator protocol, enabling standard iteration patterns.
Important: The schedule iterator is infinite. Always use limiting mechanisms like itertools.islice() or explicit break conditions.
from cron_converter import Cron
from datetime import datetime
from itertools import islice
cron = Cron('*/5 * * * *')
schedule = cron.schedule(datetime(2021, 1, 1, 9, 32))
# Using built-in next()
next_time = next(schedule)
print(next_time) # 2021-01-01T09:35:00
# Get multiple occurrences with islice
next_10 = list(islice(schedule, 10))
# For loops with islice
for dt in islice(schedule, 5):
print(dt.isoformat())
# List comprehensions
dates = [dt.isoformat() for dt in islice(schedule, 7)]
# Conditional iteration with break
for dt in schedule:
print(dt)
if dt.year > 2021:
break
Mixing Iterator Protocol with Custom Methods
You can combine standard iteration with custom .next(), .prev(), and .reset() methods:
.next() is exclusive: the datetime it returns is always strictly after the start date,
or after the value returned by the previous call. In the example above the start date is
10:00, which is itself a scheduled minute, so the first .next() skips it and returns
10:15.
Pass inclusive=True when you want to know whether a run is due at or after a given
moment, and a start date landing exactly on a scheduled minute should count:
schedule = Cron('0 12 * * *').schedule(datetime(2021, 1, 1, 12, 0))
schedule.next(inclusive=True) # 2021-01-01T12:00:00 - the start minute itself
schedule.next() # 2021-01-02T12:00:00 - the following occurrence
inclusive only affects the first call on a new or freshly reset() schedule; every
later call is exclusive regardless. A start date carrying seconds or microseconds never
sits exactly on a scheduled minute, so inclusive makes no difference there. The
parameter is not reachable through the iterator protocol (next(schedule),
islice(schedule, n)), which is always exclusive.
Migrating from 1.x — before 2.0.0 .next() was inclusive on that first call, so a
start date falling exactly on a scheduled minute returned that same minute. The shift is
one full cron period: on 0 12 * * * starting at 2021-01-01 12:00, 1.x returned
2021-01-01T12:00:00 where 2.0.0 returns 2021-01-02T12:00:00. Add inclusive=True to
the first call to keep the 1.x results.
Warning: Avoiding Infinite Loops
# Avoid this - will hang forever!
all_dates = list(schedule)
# Do this - always limit iteration
dates = list(islice(schedule, 100))
# or
dates = [dt for dt in schedule if dt.year < 2025] # Limit with condition
About DST
Be sure to init your cron-converter instance with a TZ aware datetime for this to work!
A Scheduler has two optional mutually exclusive arguments: start_date or timezone_str.
By default (no parameters), a Scheduler start count with a UTC datetime ( utcnow() ) if you not specify any start_date datetime object.
If you provide timezone_str the Scheduler will start count from a localized now datetime ( datetime.now(tz_object) ).
Example starting from localized now datetime
from cron_converter import Cron
cron = Cron('0 0 * * *')
schedule = cron.schedule(timezone_str='Europe/Rome')
# Prints: result datetime + utc offset
print(schedule.next())