prakshalj0512
prakshalj0512

Reputation: 36

Finding Last Fired time using a Cron Expression in Python

I want to know what's the last time instance is given a cron job?

Ex:

Cron expression @hourly

If I run the code at Oct 23 2:30 pm (datetime.now()), it should return Oct 22 2:00 pm.

I know Java has a package but any implementation in Python?

Thanks.

Upvotes: 0

Views: 2259

Answers (2)

ashish zarekar
ashish zarekar

Reputation: 131

from croniter import croniter
from datetime import datetime


base = datetime(2020,11,21,00,13)
print(base)
expression = "30 * * * *"

itr = croniter(expression,base)
print(itr.get_next(datetime))
print(itr.get_prev(datetime))
print(itr.get_prev(datetime))
  1. croniter on GitHub
  2. croniter on pypi

Upvotes: 3

prakshalj0512
prakshalj0512

Reputation: 36

Found the package at last!

Croniter

https://pypi.org/project/croniter/

Upvotes: 1

Related Questions