Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python library for parsing AWS cron strings and converting them to calendar dates?

I would like to take a list of all the cron expressions that the get back from calling a boto3 api call. Take take that list and convert it into calendar dates for the current month. This way I can use the dates for the backend to look at the events on a calendar.

The current problem I am facing is that I can't find a library in python for parsing AWS cron strings and definitely not a library which does both parsing and cron to date conversion.

Does anyone know of any python libraries for such work?

AWS cron strings are different than cron/crontab.

https://docs.aws.amazon.com/glue/latest/dg/monitor-data-warehouse-schedule.html

cron(Minutes Hours Day-of-month Month Day-of-week Year)

enter image description here

Wildcards

The , (comma) wildcard includes additional values. In the Month field, JAN,FEB,MAR would include January, February, and March.

The - (dash) wildcard specifies ranges. In the Day field, 1–15 would include days 1 through 15 of the specified month.

The * (asterisk) wildcard includes all values in the field. In the Hours field, * would include every hour.

The / (forward slash) wildcard specifies increments. In the Minutes field, you could enter 1/10 to specify every 10th minute, starting from the first minute of the hour (for example, the 11th, 21st, and 31st minute).

The ? (question mark) wildcard specifies one or another. In the Day-of-month field you could enter 7, and if you didn't care what day of the week the seventh was, you could enter ? in the Day-of-week field.

The L wildcard in the Day-of-month or Day-of-week fields specifies the last day of the month or week.

The W wildcard in the Day-of-month field specifies a weekday. In the Day-of-month field, 3W specifies the day closest to the third weekday of the month.

Limits

You can't specify the Day-of-month and Day-of-week fields in the same cron expression. If you specify a value in one of the fields, you must use a ? (question mark) in the other.

Cron expressions that lead to rates faster than 5 minutes are not supported.

like image 744
pitchblack408 Avatar asked Oct 19 '25 06:10

pitchblack408


1 Answers

@pitchblack408, Kindly merge the pull request that I have raised after reviewing.

pitchblack408 has done a great job porting over typescript project to Python. It is really helpful. However there is small typo in the README because of which I was scratching my head while using it. I had to read the whole source code and then finally got it.

I have raised a pull request to fix the typo in the README as well as added few methods that will be really helpful to people using that. While that pull request is merged request everyone to try this below piece of code.

NOTE: The typo is in the occurrence method.

    dt = cron.occurrence(dt).next()

Along with that here are the details of the methods I have added...

Additional methods added get_all_schedule_bw_dates and get_next_n_schedule.

These methods are very useful while using AWS cron schedule.

For example...

  1. When you need to get all possible execution dates between two dates, you can use get_all_schedule_bw_dates and compare whether the execution was done or not.

    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 * * * ? *')
    
    # Outputs
    
    [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)]
    
  2. Sometimes you have to predict next few(say 5 or 10) executions based on a cron pattern. In this case you can use get_next_n_schedule and pass n according to our requirements.

    from_dt = datetime.datetime(2021, 8, 7, 8, 30, 57, tzinfo=datetime.timezone.utc)
    
    AWSCron.get_next_n_schedule(10, from_dt, '0/23 * * * ? *')
    
    #Outputs
    [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)]
    
    
like image 66
er1shivam Avatar answered Oct 20 '25 20:10

er1shivam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!