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.
Install
Pip
pip install cron-converter
Use
from cron_converter import Cron
Create a new instance
cron_instance = Cron()
or
cron_instance = Cron('*/10 9-17 1 * *')
or (with constructor options)
cron_instance = Cron('*/10 9-17 1 * *', {
'output_weekday_names': True,
'output_month_names': True
})
Parse a cron string
cron_instance.from_string('*/10 9-17 1 * *')
print(cron_instance.to_string())
print(cron_instance.to_list())
Parse an Array
cron_instance.from_list([[0], [1], [1], [5], [0,2,4,6]])
print(cron_instance.to_string())
Constructor options
Possible options:
- output_weekday_names: false (default)
- output_month_names: false (default)
- output_hashes: false (default)
output_weekday_names and output_month_names
cron_instance = Cron(None, {
'output_weekday_names': True,
'output_month_names': True
})
cron_instance.from_string('*/5 9-17/2 * 1-3 1-5')
print(cron_instance)
or
cron_instance = Cron('*/5 9-17/2 * 1-3 1-5', {
'output_weekday_names': True,
'output_month_names': True
})
print(cron_instance)
output_hashes
cron_instance = Cron('*/5 9-17/2 * 1-3 1-5', {
'output_hashes': True
})
print(cron_instance.to_string())
Get the schedule execution times. Example with raw Datetime
cron_instance.from_string('*/5 * * * *')
reference = datetime.now()
schedule = cron_instance.schedule(reference)
print(schedule.next().isoformat())
print(schedule.next().isoformat())
schedule.reset()
print(schedule.prev().isoformat())
print(schedule.prev().isoformat())
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')
print(schedule.next())
Example using pytz:
from pytz import timezone
from datetime import datetime
from cron_converter import Cron
tz = timezone('Europe/Rome')
local_date = tz.localize(datetime(2021, 1, 1))
cron = Cron('0 0 * * *')
schedule = cron.schedule(start_date=local_date)
next_schedule = schedule.next()
next_next_schedule = schedule.next()
print(next_schedule.isoformat())
print(next_next_schedule.isoformat())
Example using python_dateutil:
import dateutil.tz
from datetime import datetime
from cron_converter import Cron
tz = dateutil.tz.gettz('Asia/Tokyo')
local_date = datetime(2021, 1, 1, tzinfo=tz)
cron = Cron('0 0 * * *')
schedule = cron.schedule(start_date=local_date)
next_schedule = schedule.next()
next_next_schedule = schedule.next()
print(next_schedule.isoformat())
print(next_next_schedule.isoformat())
About Cron schedule times frequency
It's possible to compare the Cron object schedules frequency. Thanks @zevaverbach.
Cron('0 1 * * 1-5') == Cron('0 2 * * 1-5')
Cron('0 1,2,3 * * 1-5') > Cron('0 1,23 * * 1-5')
Cron('* 1 * * 1-5') == Cron('0-59 1 * * 1-5')
Cron('1-30 1 * * 1-5') > Cron('1-29 1 * * 1-5')
Cron('* 1 1 * 1-5') == Cron('0-59 1 2 * 1-5')
Cron('* 1 1,2 * 1-5') > Cron('* 1 6 * 1-5')
Cron('* 1 1 11 1-5') == Cron('* 1 1 1 1-5')
Cron('* 1 6 * 1-5') > Cron('* 1 6 1 1-5')
Cron('* 1 1 11 *') == Cron('* 1 1 11 0-6')
Cron('* 1 6 * 1-5') > Cron('* 1 6 * 1-4')
About seconds repeats
Cron-converter is NOT able to do second repetition crontabs form.
About datetime objects validation
Cron can also validate datetime objects (datetime and date).
Cron("* * 10 * *").validate(datetime(2022, 1, 10, 1, 9))
Cron("* * 12 * *").validate(datetime(2022, 1, 10, 1, 9))
A datetime object can also be validated with the in
operator
datetime(2024, 3, 19, 15, 55) in Cron('*/5 9-17/2 * 1-3 1-5')
Develop & Tests
git clone https://github.com/Sonic0/cron-converter
cd cron-converter
...
python -m unittest discover -s tests/unit
python -m unittest discover -s tests/integration
Project info
This repo is part of a projects group, called Cron-Converter.
Its related repositories: