crontools is a library that allows you to parse crontab expression and iterate over scheduled fire times.
Features:
- crontab expression parser
- optional seconds field support
- optional year field support
- crontab fire time sequential iteration support
You can install crontools with pip:
$ pip install crontools>>>importdatetimeasdt>>>importcrontoolsasct>>>>>>tz=dt.timezone.utc>>>now=dt.datetime.fromisoformat('2020-02-29 23:59:59.999+00:00') >>>ct=ct.Crontab.parse( ... '* * * * * * *', ... seconds_ext=True, ... years_ext=True, ... tz=tz, ... ) >>>>>>print(f"Next fire time: {ct.next_fire_time(now=now)}") Nextfiretime: 2020-03-0100:00:00+00:00>>>importcrontoolsasct>>>>>>tz=dt.timezone.utc>>>now=dt.datetime.fromisoformat('2021-02-01 00:00:00+00:00') >>>ct=ct.Crontab.parse( ... '30 30 12-16/2 1,2 JAN SAT,SUN *', ... seconds_ext=True, ... years_ext=True, ... tz=tz, ... ) >>>>>>cron_iter=ct.iter(start_from=now) >>>forn, fire_datetimeinzip(range(1, 31), cron_iter): ... print("{n:2}:{dt}".format(n=n, dt=fire_datetime)) ... ... 1: 2022-01-0112:30:30+00:002: 2022-01-0114:30:30+00:003: 2022-01-0116:30:30+00:004: 2022-01-0212:30:30+00:005: 2022-01-0214:30:30+00:006: 2022-01-0216:30:30+00:007: 2022-01-0812:30:30+00:008: 2022-01-0814:30:30+00:009: 2022-01-0816:30:30+00:0010: 2022-01-0912:30:30+00:0011: 2022-01-0914:30:30+00:0012: 2022-01-0916:30:30+00:0013: 2022-01-1512:30:30+00:0014: 2022-01-1514:30:30+00:0015: 2022-01-1516:30:30+00:0016: 2022-01-1612:30:30+00:0017: 2022-01-1614:30:30+00:0018: 2022-01-1616:30:30+00:0019: 2022-01-2212:30:30+00:0020: 2022-01-2214:30:30+00:0021: 2022-01-2216:30:30+00:0022: 2022-01-2312:30:30+00:0023: 2022-01-2314:30:30+00:0024: 2022-01-2316:30:30+00:0025: 2023-01-0112:30:30+00:0026: 2023-01-0114:30:30+00:0027: 2023-01-0116:30:30+00:0028: 2023-01-0212:30:30+00:0029: 2023-01-0214:30:30+00:0030: 2023-01-0216:30:30+00:00