Cron format

Cron format

A cron specification allows the complex declaration of job scheduling which is tied to the system clock and calendar.

 

For those unfamiliar with "cron", this means being able to create a firing schedule such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month".

 

A "Cron-Expression" is a string comprised of 6 or 7 fields separated by white space. The 6 mandatory and 1 optional fields are as follows:

Field NameAllowed ValuesAllowed Special Characters
Seconds0-59, - * /
Minutes0-59, - * /
Hours0-23, - * /
Day-of-month1-31, - * ? / L W C
Month1-12 or JAN-DEC, - * /
Day-of-Week1-7 or SUN-SAT, - * ? / L C #
Year (Optional)      empty, 1970-2099    , - * /

 

The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".

 

The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.

 

The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".

 

The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".

 

The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.

 

The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

 

The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.

 

The 'L' and 'W' characters can also be combined for the day-of-month expression to yield 'LW', which translates to "last weekday of the month".

 

The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.

 

The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".

 

The legal characters and the names of months and days of the week are not case sensitive.

Examples

ExpressionMeaning
"0 0 12 * * ?"Fire at 12pm (noon) every day
"0 15 10 ? * *"Fire at 10:15am every day
"0 15 10 * * ?"Fire at 10:15am every day
"0 15 10 * * ? *"Fire at 10:15am every day
"0 15 10 * * ? 2005"Fire at 10:15am every day during the year 2005
"0 * 14 * * ?"Fire every minute starting at 2pm and ending at 2:59pm, every day
"0 0/5 14 * * ?"Fire every five minutes starting at 2pm and ending at 2:55pm, every day
"0 0/5 14,18 * * ?"Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
"0 0-5 14 * * ?"Fire every minute starting at 2pm and ending at 2:05pm, every day
"0 10,44 14 ? 3 WED"Fire at 2:10pm and at 2:44pm every Wednesday in the month of March
"0 15 10 ? * MON-FRI"Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"0 15 10 15 * ?"Fire at 10:15am on the 15th day of every month
"0 15 10 L * ?"Fire at 10:15am on the last day of every month
"0 15 10 ? * 6L"Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L 2002-2005"Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
"0 15 10 ? * 6#3"Fire at 10:15am on the third Friday of every month

 

Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!

NOTES:

  • Support for the features described for the 'C' character is not complete.
  • Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
  • Be careful when setting fire times between midnight and 1:00 AM - "daylight savings" can cause a skip or a repeat depending on whether the time moves back or jumps forward.
### 如何在 Vue3 中实现 Cron 表达式调度任务 #### 使用 `vue-cron` 插件简化 Cron 表达式的创建和管理 为了方便开发者在前端构建合法有效的 cron 表达式并将其传递给后端执行相应的计划任务,在 Vue.js 应用中可以采用专门为此目的设计的组件库——`vue-cron`。此插件不仅支持通过图形界面直观地设置时间规则,还能够自动转换这些规则为标准格式的字符串表示形式即 cron expression[^1]。 对于希望升级到最新版本框架环境下的用户来说,确保所使用的 `vue-cron` 版本兼容于 Vue 3 是至关重要的。如果官方文档未明确指出其对 Vue 3 的支持情况,则可能需要寻找社区维护的支持 Vue 3 的分支或是考虑其他替代方案。 #### 实现步骤概述 - **安装依赖** 首先需引入必要的开发包至项目之中: ```bash npm install @chenfengyuan/vue-cron element-plus axios ``` 这里除了核心功能所需的 `@chenfengyuan/vue-cron` 外,还包括了用于 UI 展示的基础组件集 `element-plus` 和发起 HTTP 请求的数据交互工具 `axios`。 - **配置全局注册或局部导入** 为了让新加入的应用模块能够在模板内正常使用来自第三方库的功能特性,应当按照官方指南完成相应初始化操作。以 Element Plus 为例,可以在 main.ts 文件里添加如下代码片段来达到全局生效的目的;当然也可以仅限于特定页面按需加载减少打包体积。 ```typescript import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' import '@chenfengyuan/vue-cron/dist/style.css' const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` - **编写视图逻辑** 接下来就是具体业务场景下如何利用上述资源组合起来达成预期目标的关键部分了。下面给出一段简单的实例演示怎样借助 `<el-date-picker>` 控制器配合自定义方法生成最终提交给服务器处理的任务描述信息。 ```html <template> <div class="cron-editor"> <!-- 时间选择 --> <el-form :model="form" label-width="80px"> <el-form-item label="Cron表达式"> <vc-cron v-model="form.cronExpression"></vc-cron> </el-form-item> <el-button type="primary" @click="submitForm">保存</el-button> </el-form> <!-- 显示解析后的中文说明 --> <p>当前表达式含义:{{ parsedDescription }}</p> </div> </template> <script lang="ts"> import { defineComponent, ref, computed } from 'vue'; import VcCron from "@chenfengyuan/vue-cron"; import parseCronToChinese from '@/utils/cron-parser'; // 假设这是本地编写的辅助函数路径 export default defineComponent({ components: { VcCron, }, setup() { const form = ref({ cronExpression: '' }); const submitForm = () => { console.log('即将发送:', form.value); // 这里可以通过API接口向后台传送数据... }; const parsedDescription = computed(() => parseCronToChinese(form.value.cronExpression) ); return { form, submitForm, parsedDescription, }; } }); </script> ``` 这段代码展示了如何结合使用 `vue-cron` 组件让用户输入 cron 表达式,并实时显示对应的自然语言解释。同时提供了按钮触发事件处理器以便后续进一步的操作流程衔接[^3]。 #### 解析 Cron 表达式为人类可读文本 考虑到并非所有人都熟悉 cron 格式的语法结构及其代表意义,因此提供一种机制将复杂的字符序列转化为易于理解的文字表述就显得尤为重要。这不仅能帮助普通使用者更好地掌握参数设定技巧,也能有效降低因误解而导致错误配置的风险。 针对这一需求,可以自行开发一个小巧实用的帮助类来进行此类转化工作。以下是 Python 风格伪码示意(实际应用时应调整为目标编程语言),它接收一个完整的 cron 字符串作为输入参数返回一句简洁明了的话术概括整个周期安排规律。 ```javascript function parseCronToChinese(cronString) { let parts = cronString.split(' '); if (parts.length !== 5 && parts.length !== 6){ throw new Error("Invalid cron format"); } // 省略具体的解析逻辑... return `${minuteDesc} ${hourDesc} ${dayOfMonthDesc}, ${monthDesc} ${dayOfWeekDesc}`; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值