cron_calc
1.0.0
Croncalc :一种用于计算Cron作业事件的红宝石宝石。有了这个宝石,您可以通过提供Cron表达来轻松确定何时会出现CRON作业。关键功能包括计算发生的能力:
该工具可用于调度,预测和分析使用CRON进行工作计划的系统中的任务。您也可以在此处阅读有关Croncalc的文章。
通过执行来安装宝石:
$ gem install cron_calc
安装cron_calc
后,您可以使用cron字符串初始化CronCalc
。
require 'cron_calc'
cron_calc = CronCalc . new ( '5 5 * * *' )
现在,您可以使用#in
, #next
, #last
三种方法之一来确定指定期间内的CRON作业事件。
#in
计算给定时间段内的Cron作业事件。
参数:
period
- 定义计算开始和结束时间的范围对象。 period = Time . new ( 2024 , 1 , 1 , 0 , 0 ) .. Time . new ( 2024 , 1 , 4 , 0 , 0 )
cron_calc . in ( period )
# => [2024-01-01 05:05:00 +0100, 2024-01-02 05:05:00 +0100, 2024-01-03 05:05:00 +0100]
#next
从给定的开始时间计算Cron作业的下一个“ N”出现。
参数:
count
- (可选,整数)要计算的发生数量。默认为1。after:
- (可选,时间,关键字参数)计算出发生的开始时间。如果未提供,则默认为当前时间(time.now)。max_years
(可选,整数,关键字参数)搜索未来发生的最大年度。默认为5。 cron_calc . next
# => [2023-12-20 05:05:00 +0100]
cron_calc . next ( 3 )
# => [2023-12-20 05:05:00 +0100, 2023-12-21 05:05:00 +0100, 2023-12-22 05:05:00 +0100]
cron_calc . next ( 2 , after : Time . new ( 2024 , 1 , 1 , 0 , 0 ) )
# => [2024-01-01 05:05:00 +0100, 2024-01-02 05:05:00 +0100]
#last
计算Cron作业的最后一个“ N”发生,直到给定的结束时间为止。
参数:
count
- (可选,整数)要计算的发生数量。默认为1。before:
- ((可选,时间,关键字参数)计算过去发生的结束时间。如果未提供,则默认为当前时间(time.now)。max_years
(可选,整数,关键字参数)向后搜索过去出现的最大年数。默认为5。 cron_calc . last
# => [2023-12-19 05:05:00 +0100]
cron_calc . last ( 4 , before : Time . new ( 2024 , 1 , 1 , 0 , 0 ) )
# => [2023-12-31 05:05:00 +0100, 2023-12-30 05:05:00 +0100, 2023-12-29 05:05:00 +0100, 2023-12-28 05:05:00 +0100]
# You can omit the count parameter
CronCalc . new ( '5 5 */5 * SUN' ) . last ( before : Time . new ( 2020 , 1 , 1 , 0 , 0 ) )
# => [2019-12-01 05:05:00 +0100]
# You can combine ',' and '-'
CronCalc . new ( '5 5 5-7,10 FEB *' ) . next ( 5 )
# => [2024-02-05 05:05:00 +0100, 2024-02-06 05:05:00 +0100, 2024-02-07 05:05:00 +0100, 2024-02-10 05:05:00 +0100, 2025-02-05 05:05:00 +0100]
# You can use predefined definitions like @daily, @monthly, etc.
CronCalc . new ( '@monthly' ) . next ( 3 , after : Time . new ( 2024 , 1 , 1 , 0 , 0 ) )
# => [2024-01-01 00:00:00 +0100, 2024-02-01 00:00:00 +0100, 2024-03-01 00:00:00 +0100]
# I want to know when the next 10 Friday the 13ths will be!
CronCalc . new ( '0 0 13 * FRI' ) . next ( 10 )
# =>
# [2024-09-13 00:00:00 +0200,
# 2024-12-13 00:00:00 +0100,
# 2025-06-13 00:00:00 +0200,
# 2026-02-13 00:00:00 +0100,
# 2026-03-13 00:00:00 +0100,
# 2026-11-13 00:00:00 +0100,
# 2027-08-13 00:00:00 +0200,
# 2028-10-13 00:00:00 +0200]
欢迎在https://github.com/mizinsky/cron_calc上的GitHub上的错误报告和拉动请求。
根据MIT许可证的条款,该宝石可作为开源。