c 解析成Cron表示式方法

2021-10-01 17:16:57 字數 2688 閱讀 5928

using system;

/// 任務型別

/// 開始日期

/// 結束日期

/// 任務執行時間

/// 執行的條件

/// /// 月計畫具體執行的日期

///

public static string parsecron(tasktype tasktype, string startdate, string enddate, string tasktime, string condition="", string spacing = "")

//1.執行的年份

string tasktimestaryear = ""; //開始年份

string tasktimeendyear = "";  //結束年份

string tasktimeyear = "*"; //任務執行的年份

if (!string.isnullorwhitespace(startdate)) tasktimestaryear = (datetime.parse(startdate).year).tostring();

if (!string.isnullorwhitespace(enddate)) tasktimeendyear = (datetime.parse(enddate).year).tostring();

if (!string.isnullorwhitespace(tasktimestaryear) && !string.isnullorwhitespace(tasktimestaryear)) //如果開始日期和結束日期都不為空

else if (string.isnullorwhitespace(tasktimestaryear) && !string.isnullorwhitespace(tasktimestaryear)) //如果只有結束日期,開始日期預設今天

else

//1.執行的月份

string tasktimestarmonth = ""; //開始月份

string tasktimeendmonth = "";  //結束月份

string tasktimemonth = "*"; //任務執行的月份

if (!string.isnullorwhitespace(startdate)) tasktimestarmonth = (datetime.parse(startdate).month).tostring();

if (!string.isnullorwhitespace(enddate)) tasktimeendmonth = (datetime.parse(enddate).month).tostring();

if (!string.isnullorwhitespace(tasktimestarmonth) && !string.isnullorwhitespace(tasktimeendmonth)) //如果開始日期和結束日期都不為空

else if (string.isnullorwhitespace(tasktimestarmonth) && !string.isnullorwhitespace(tasktimeendmonth)) //如果只有結束日期,開始日期預設今天

else

//2.執行的小時和分鐘

datetime dttasktime = datetime.parse(tasktime);

string tasktimehour = (dttasktime.hour).tostring(); //執行時間的小時

string tasktimemin = (dttasktime.minute).tostring(); //執行時間的分鐘

string result = string.empty;

switch (tasktype)

1/1 ? ", tasktimemin, tasktimehour, tasktimeyear, tasktimemonth);

break;

//周計畫 (condition 已逗號分隔 eg:1,2,5,7) 1:表示星期天  7:表示星期六

case tasktype.week:

if (string.isnullorwhitespace(condition)) condition = "1-7";

result = string.format("0 ? ", tasktimemin, tasktimehour, condition, tasktimeyear, tasktimemonth);

break;

//月計畫(condition表示月份 已逗號分隔 eg:1,2,5,7) 1:表示1月  12:表示12月

case tasktype.month:

if (string.isnullorwhitespace(condition)) condition = "1-12"; //預設1到12月

if (string.isnullorwhitespace(spacing)) spacing = "1";   //預設1號傳送

result = string.format("0 ? ", tasktimemin, tasktimehour, spacing, condition, tasktimeyear);

break;

default:

result = string.empty;

break;

}return result;}}

public enum tasktype

cron表示式解析

cron表示式有5位 linux中的crontab命令中就是5位 6位 7位之分,不同位數之間以空格分隔 位數每位的含義 5位 分 時 天 月 周 6位 秒 分 時 天 月 周 7位 秒 分 時 天 月 周 年 在大部分情況下會省略年這一位,成為6位,再省略秒,就成為5位。匹配該域的任意值 表示範圍...

將cron表示式解析成中文,方便客戶理解

描述 author norton lai created 2019 2 27 下午4 54 36 public class test 解析月 descmonth tmpcorns 4 sbuffer 解析周 descweek tmpcorns 5 sbuffer 解析日 descday tmpcor...

Cron表示式詳解 job表示式

cron表示式的詳細用法 字段 允許值 允許的特殊字元 秒 0 59 分 0 59 小時 0 23 日期 1 31 l w c 月份 1 12 或者 jan dec 星期 1 7 或者 sun sat l c 年 可選 留空,1970 2099 例子 0 5 每5秒執行一次 字元被用來指定所有的值。...