Convert cron syntax in minutes to give schedule interval in python

Viewed 932

I have this setup on my side, where I am getting the values in minutes like 1440,60,30, etc which says that my pipeline runs at 1440 minutes, 60 minutes, and 30 minutes respectively.

From the same setup, I am getting the values in cron syntax 0 2 * * *,0 */8 * * * etc, which says my pipeline runs at 2 every day and at every 8 hours.

My query is To maintain the consistency, How should I do to convert the cron syntax to minutes, like 0 2 * * * to 1440, ignoring at what time it runs.

I just want to get the schedule intervals in minutes from the cron syntaxes

PS: I tried with cronitor module, but failed.

Thanks in Advance.

1 Answers

I tried with cronitor module, but failed.

Do you mean the croniter module? It should work – make an iterator, grab two datetimes from it and subtract one from the other. Here's an example with cronsim:

from cronsim import CronSim
from datetime import datetime

it = CronSim("0 */8 * * *", datetime(2000, 1, 1))
a = next(it)
b = next(it)
delta = b - a
delta_minutes = int(delta.total_seconds() / 60)

PS. Note that not every cron expression will translate cleanly to a minute interval. For example, consider 0,5 * * * *. It runs at 0:00, 0:05, 1:00, 1:05, 2:00, 2:05 etc. So the interval will alternate between 5 and 55 minutes.

Related