在 linux 之外解析/评估/生成 CrontabExpressions? [英] Parse/evaluate/generate CrontabExpressions outside of linux?
问题描述
我正在构建一些需要调度输入的软件,我真的很想重新使用 crontab 的设计,因为它很有效.
I'm building some software that needs a scheduling input, and I'd really like to re-use the design of crontab because it simply works.
CrontabExpressions 可以非常简单*/5 * * * *
每五分钟运行一次"或更复杂的 2-59/3 1,9,22 11-26 1-6 ?2003
2003年1月至6月每月11日至26日,从凌晨2点、上午9点和晚上10点开始,每三分钟一次".
CrontabExpressions can be really simple */5 * * * *
"run every five minutes" or more complex 2-59/3 1,9,22 11-26 1-6 ? 2003
"In 2003 on the 11th to 26th of each month in January to June every third minute starting from 2 past 1am, 9am and 10pm".
我不想使用名为 crontab 的 linux 软件,我正在寻找一种可以正确评估这些表达式的方法(例如,输出与 crontab 匹配的接下来的 25 个时间戳,或者基于某些抽象的 GUI 生成它以用于用户).
I am not looking to use the linux software called crontab, I'm seeking a way I can evaluate these expressions correctly (for instance, output the next 25 timestamps that match the crontab, or generate it based on some abstracted GUI for the users).
我真的找不到任何可以在 JavaScript 或 PHP 甚至其他语言中执行此操作的库或函数.如果它们不存在,那么这样做的好方法是什么?我已经知道过于复杂的正则表达式很可能是错误的答案.我很难在 crontab 中找到 C 源代码这个任务也是如此,这让我相信它可能不会在这里进行?
I can't really find any libraries or functions that do this in JavaScript or PHP or even other languages. If they don't exist, what would be a good method to do this? I already know an overly-complicated regular expression is likely to be the wrong answer. I'm having a hard time finding the C source code in crontab that does this task as well, which makes me believe it might not take place here?
推荐答案
要输出与 crontab 匹配的下 25 个时间戳,您可以使用 crontab
Python 模块:
To output the next 25 timestamps that match the crontab you could use crontab
Python module:
from datetime import datetime, timedelta
import crontab
tab = crontab.CronTab('2-59/3 1,9,22 11-26 1-6 ? 2012')
dt = datetime.now()
for _ in xrange(25):
delay = tab.next(dt) # seconds before this crontab entry can be executed.
dt += timedelta(seconds=delay)
print(dt)
输出
2012-01-11 22:41:00
2012-01-11 22:44:00
2012-01-11 22:47:00
2012-01-11 22:50:00
2012-01-11 22:53:00
2012-01-11 22:56:00
2012-01-11 22:59:00
2012-01-12 01:02:00
2012-01-12 01:05:00
2012-01-12 01:08:00
2012-01-12 01:11:00
2012-01-12 01:14:00
2012-01-12 01:17:00
2012-01-12 01:20:00
2012-01-12 01:23:00
2012-01-12 01:26:00
2012-01-12 01:29:00
2012-01-12 01:32:00
2012-01-12 01:35:00
2012-01-12 01:38:00
2012-01-12 01:41:00
2012-01-12 01:44:00
2012-01-12 01:47:00
2012-01-12 01:50:00
2012-01-12 01:53:00
还有 python-crontab 提供 crontab
模块,但具有更丰富的功能(解析/生成).
There is also python-crontab that provides crontab
module but with richer functionality (parse/generate).
这篇关于在 linux 之外解析/评估/生成 CrontabExpressions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!