根据 cron 规范计算下一个计划时间
在给定当前时间和 cron 规范的情况下,计算事件下一次运行时间的有效方法是什么?
我正在寻找“每分钟循环检查是否符合规范”以外的东西。
规范的示例可能是:
- 每个月,1 日和 15 日 15:01
- 每小时 10、20、30、40、50 分钟,
Python 代码很可爱,但伪代码或高级描述也将受到赞赏。
[更新] 假设规范已经解析并且采用某种合理的格式。
What's an efficient way to calculate the next run time of an event given the current time and a cron spec?
I'm looking for something other than "loop through every minute checking if it matches spec".
Examples of specs might be:
- Every month, on the 1st and 15 at 15:01
- At 10,20,30,40,50 mins past the hour every hour
Python code would be lovely but psuedo code or high level description would also be appreciated.
[Update] Assume the spec is already parsed and is in some reasonable format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看它,我认为你需要:
我不知道如何同时处理星期几和月份;我确信有一种方法,但另一方面,我认为我从未见过真正指定这两者的规范。我认为为其中一个编写一个处理程序并在收到两者时抛出一个错误就足够了。
编辑:显然如果同时指定了星期几和月份日,它应该在两者触发 - 即,如果规则是“15日,星期三”,它将在每个15号和每个星期三触发。
croniter 包可以做你想要的事情:
打印,
尽管如果它被写成一个实际的迭代器会很好。也许我已经有了下一个项目;-)
Just looking at it, I think you need to:
I don't know how to handle day-of-week and day-of-month simultaneously; I am sure there is a way, but on the other hand I don't think I've ever seen a spec that actually specified both. I think it would be sufficient to write a handler for either and throw an error if you receive both.
Edit: apparently if day-of-week and day-of-month are both specified, it is supposed to fire on both - ie if the rule is '15th, Wednesday' it will fire on every 15th and every Wednesday.
The croniter package does what you want:
prints
although it would be nice if it were written as an actual iterator. Maybe I've got my next project ;-)