解析crontab风格的线条

Kru*_*lur 20 python crontab

我需要在Python中解析类似crontab的调度定义(例如00 3***)并获得最后运行的位置.

是否有一个好的(最好是小的)库解析这些字符串并将它们转换为日期?

小智 44

也许python包croniter适合您的需求.

用法示例:

>>> import croniter
>>> import datetime
>>> now = datetime.datetime.now()
>>> cron = croniter.croniter('45 17 */2  * *', now)
>>> cron.get_next(datetime.datetime)
datetime.datetime(2011, 9, 14, 17, 45)
>>> cron.get_next(datetime.datetime)
datetime.datetime(2011, 9, 16, 17, 45)
>>> cron.get_next(datetime.datetime)
datetime.datetime(2011, 9, 18, 17, 45)
Run Code Online (Sandbox Code Playgroud)

  • 克罗尼特+1。我一直在寻找 Java 的 [Spring CronTrigger](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronTrigger.html) 的 Python 等效项。我发现后者的 API 更自然一些,尽管我不太喜欢。我宁愿选择无状态且不可变的对象,例如 `cronExpr = CronExpression.compile("0 0 * * * "); 日期时间 = cronExpr.next(日期时间);`。 (3认同)
  • 这正是我需要的,并且我需要那里的get_prev函数。谢谢! (2认同)