1 个稳定版本
| 5.9.0 |  | 
|---|---|
| 5.7.0 |  | 
| 4.0.0 |  | 
| 3.0.0 |  | 
| 2.0.9 |  | 
#2509 在 神奇豆子
120 每月下载量
用于 5 个crate(3 个直接使用)
65KB
 1.5K  SLoC
clockwork-cron  
 
一个在 Solana 运行时安全使用的 cron 表达式解析器。与稳定的 Rust v1.28.0 兼容。
use clockwork_cron::Schedule;
use chrono::{DateTime, NaiveDateTime, Utc};
use std::str::FromStr;
fn main() {
  //               sec  min   hour   day of month   month   day of week   year
  let expression = "0   30   9,12,15     1,15       May-Aug  Mon,Wed,Fri  2018/2";
  let schedule = Schedule::from_str(expression).unwrap();
  let ts = 1234567890;
  let next_ts = schedule
      .after(&DateTime::<Utc>::from_utc(
          NaiveDateTime::from_timestamp(ts, 0),
          Utc,
      ))
      .take(1)
      .next()
    {
        Some(datetime) => Some(datetime.timestamp()),
        None => None,
    }
}
/*
Upcoming fire times:
-> 2018-06-01 09:30:00 UTC
-> 2018-06-01 12:30:00 UTC
-> 2018-06-01 15:30:00 UTC
-> 2018-06-15 09:30:00 UTC
-> 2018-06-15 12:30:00 UTC
-> 2018-06-15 15:30:00 UTC
-> 2018-08-01 09:30:00 UTC
-> 2018-08-01 12:30:00 UTC
-> 2018-08-01 15:30:00 UTC
-> 2018-08-15 09:30:00 UTC
*/
⚠️ 语法
sec  min   hour   day of month   month   day of week   year
如果您使用 crontab guru 等工具,请注意 clockwork 解析器是一个 7 列字符串。您可能需要添加 秒(最左侧列),并且可以可选地添加 年份(最右侧列)。例如以下 5 列 cron
| 分钟 | 小时 | 月份中的日期 | 月份 | 星期中的日期 | 
|---|---|---|---|---|
| 0 | 18 | * | * | 星期五 | 
变为
| 秒 | 分钟 | 小时 | 月份中的日期 | 月份 | 星期中的日期 | 年份 | 
|---|---|---|---|---|---|---|
| 0 | 0 | 18 | * | * | 星期五 | * | 
或
| 秒 | 分钟 | 小时 | 月份中的日期 | 月份 | 星期中的日期 | 
|---|---|---|---|---|---|
| 0 | 0 | 18 | * | * | 星期五 | 
这些也受支持
"@yearly"
"@weekly"
"@daily"
"@hourly"
依赖
~2.5MB
~37K SLoC