An AWS Cron Parser
Main Branch Status
Develop Branch Status
A python port from a typescript project.
https://github.com/beemhq/aws-cron-parser
pip install pyawscron
Field | Values | Wildcards |
---|---|---|
Minutes |
0-59 |
, - * / |
Hours |
0-23 |
, - * / |
Day-of-month |
1-31 |
, - * ? / L W |
Month |
1-12 or JAN-DEC |
, - * / |
Day-of-week |
1-7 or SUN-SAT |
, - * ? L # |
Year |
1970-2199 |
, - * / |
next
from pyawscron import AWSCron
import datetime
import calendar
def main():
aws_cron = AWSCron("0 5 4 * ? *")
today = datetime.datetime.utcnow().date()
start_date = datetime.datetime(today.year, today.month, 1, tzinfo=datetime.timezone.utc)
last_day = calendar.monthrange(today.year, today.month)[1]
end_date = datetime.datetime(today.year, today.month, last_day, tzinfo=datetime.timezone.utc)
dt=start_date
while True:
dt = aws_cron.occurrence(dt).next()
if dt > end_date:
break
print(dt)
if __name__ == "__main__":
main()
prev
from pyawscron import AWSCron
import datetime
def main():
aws_cron = AWSCron("0 5 4 * ? *")
today = datetime.datetime.utcnow().date()
start_date = datetime.datetime(today.year, today.month, 1, tzinfo=datetime.timezone.utc)
end_date = datetime.datetime(today.year, today.month-1, 1, tzinfo=datetime.timezone.utc)
dt=start_date
while True:
dt = aws_cron.occurrence(dt).prev()
if dt < end_date:
break
print(dt)
if __name__ == "__main__":
main()
Returns a list of UTC datetime objects using a start and end date. The end date has a flag to be inclusive or exclusive.
Note: This method has no limit on how many datetime object can be returned. Use Iterative approach or get_next_n_schedule if memory becomes an issue.
from_dt = datetime.datetime(2021, 8, 7, 8, 30, 57, tzinfo=datetime.timezone.utc)
to_date = datetime.datetime(2021, 8, 7, 11, 30, 57, tzinfo=datetime.timezone.utc)
AWSCron.get_all_schedule_bw_dates(from_dt, to_date, '0/23 * * * ? *')
# Resulting list
[datetime.datetime(2021, 8, 7, 8, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 23, tzinfo=datetime.timezone.utc)]
Returns a list with the n next datetimes that match the aws cron expression from the provided start date.
from_dt = datetime.datetime(2021, 8, 7, 8, 30, 57, tzinfo=datetime.timezone.utc)
AWSCron.get_next_n_schedule(10, from_dt, '0/23 * * * ? *')
# Resulting list
[datetime.datetime(2021, 8, 7, 8, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 46, tzinfo=datetime.timezone.utc)]
Returns a list with the n prev datetimes that match the aws cron expression from the provided start date.
from_dt = datetime.datetime(2021, 8, 7, 11, 50, 57, tzinfo=datetime.timezone.utc)
AWSCron.get_prev_n_schedule(10, from_dt, '0/23 * * * ? *')
# Resulting list
[datetime.datetime(2021, 8, 7, 11, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 11, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 10, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 46, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 23, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 9, 0, tzinfo=datetime.timezone.utc),
datetime.datetime(2021, 8, 7, 8, 46, tzinfo=datetime.timezone.utc)]