=================
== Attt's blog ==
=================
Tech otakus save the world

求求别用quartz解析cron表达式

- Permalink

standard cron expr.

stdcronexpr.jpg

标准的cron expression(或者是默认)的实现里,Day of week的合法值是0–6 or SUN–SAT,并且只有5个必填项,最小的单位是分钟

关于为什么最小的单位是分钟:

The cron in Version 7 Unix was a system service (later called a daemon) invoked from /etc/rc when the operating system entered multi-user mode.[12] Its algorithm was straightforward:

  1. Read /usr/lib/crontab[13]
  2. Determine if any commands must run at the current date and time, and if so, run them as the superuser, root.
  3. Sleep for one minute
  4. Repeat from step 1.

其中step 3是重点。

不同的cron表达式实现

cron expr. in linux crontab

crontab.5

crontab5.jpg

linux crontab支持的cron表达式中的Day of week的合法值是0-7 (0 or 7 is Sunday, or use names)

cron expr. in java spring

Spring CronExpression

springcron.jpg

java spring的标准下cron表达式中增加了1个必填项:最小的单位

cron expr. in go robfig/cron/v3

robfig/cron/v3 CRON_Expression_Format

robfigcronv3.jpg

go robfig/cron/v3支持的cron表达式遵循了标准cron表达式的格式,只有5项,并且Day of week的合法值是0–6 or SUN–SAT

看起来还是比较保守的。

不过他也支持的扩展,也就是支持6项实现。

但是!!

扩展单位的情况,他竟然参考了quartz的标准…(不确定是否是完全参照quartz的标准,但大哥你抄谁不好啊抄quartz)

robfigcronv3quartz.jpg

the most popular alternative Cron schedule format

搞go的果然以前都是搞java的,popular啥呀就popular

cron expr. in java quartz

那么quartz的标准有什么问题呢?

注意看 这个男人叫小帅

quartz-2.3.0 crontrigger

quartzcron.jpg

!!!

quartzcrondow.jpg

他tmDay of week的合法值是1-7 or SUN-SAT!也就是1代表Sunday,相对于标准的cron表达式向后推迟了一天…

其他的实现的改动好歹是扩展,这位更是个寄… 这位却有自己的想法。

conclusion

看来填dow这个项的时候还是得填names。


[参考]