private static void ComputeEveryHourCrontab(CrontabExpressionModel model) { if (string.IsNullOrWhiteSpace(model.HourlyModeHours)) { model.HourlyModeHours = "1"; } if (model.HourlyMode && checkIfIsValidHour(model.HourlyModeHours)) { var hours = int.Parse(model.HourlyModeHours); model.OutputCrontabString = $@"0 0 0/{hours} 1/1 * ?"; } }
private static void ComputeEveryMinuteCrontab(CrontabExpressionModel model) { if (string.IsNullOrWhiteSpace(model.MinuteModeMinutes)) { model.MinuteModeMinutes = "1"; } if (model.MinuteMode && checkIfIsValidMinute(model.MinuteModeMinutes)) { var minutes = int.Parse(model.MinuteModeMinutes); model.OutputCrontabString = $@"0 0/{minutes} * 1/1 * ?"; } }
/// <summary> /// This is used to react to Crontab UI radio button form event. It read the UI model and call the right method to produce the crontab expression. /// </summary> /// <param name="model"></param> public static void RecalculateCrontab(CrontabExpressionModel model) { //model.OutputCrontabString = null; try { if (model.CustomMode) { return; } if (model.MinuteMode) { ComputeEveryMinuteCrontab(model); return; } if (model.HourlyMode) { ComputeEveryHourCrontab(model); return; } if (model.DailyMode) { ComputeEveryDayCrontab(model); return; } if (model.WeeklyMode) { ComputeEveryWeekDayCrontab(model); return; } if (model.LastDayOfMonthMode) { ComputeLastMonthDay(model); return; } if (model.MonthlyMode) { ComputeNDayInMonthCrontab(model); return; } } catch (Exception ex) { } }
private static void ComputeLastMonthDay(CrontabExpressionModel model) { if (string.IsNullOrWhiteSpace(model.LastDayOfMonthModeTime)) { model.LastDayOfMonthModeTime = "12:00"; } if (model.LastDayOfMonthMode && checkIfIsValidTime(model.LastDayOfMonthModeTime)) { var minutes = int.Parse(model.LastDayOfMonthModeTime.Split(':')[1]); var hours = int.Parse(model.LastDayOfMonthModeTime.Split(':')[0]); model.OutputCrontabString = $"0 {minutes} {hours} {(model.LastWeekDayOfMonthMode ? "LW" : "L")} * ?"; } }
private static void ComputeEveryDayCrontab(CrontabExpressionModel model) { if (string.IsNullOrWhiteSpace(model.DailyModeTime)) { model.DailyModeTime = "12:00"; } if (model.DailyMode && checkIfIsValidTime(model.DailyModeTime)) { var minutes = int.Parse(model.DailyModeTime.Split(':')[1]); var hours = int.Parse(model.DailyModeTime.Split(':')[0]); model.OutputCrontabString = $"0 {minutes} {hours} * * ?"; } }
private static void ComputeNDayInMonthCrontab(CrontabExpressionModel model) { if (string.IsNullOrWhiteSpace(model.MonthlyModeDay)) { model.MonthlyModeDay = "1"; } if (string.IsNullOrWhiteSpace(model.MonthlyModeTime)) { model.MonthlyModeTime = "12:00"; } if (model.MonthlyMode && checkIfIsValidTime(model.MonthlyModeTime) && checkIfIsValidMonthDay(model.MonthlyModeDay)) { var minutes = int.Parse(model.MonthlyModeTime.Split(':')[1]); var hours = int.Parse(model.MonthlyModeTime.Split(':')[0]); var dayInMonth = int.Parse(model.MonthlyModeDay); model.OutputCrontabString = $"0 {minutes} {hours} {dayInMonth}{(model.MonthlyWeekdayMode ? "W" : "")} * ?"; } }
private static void ComputeEveryWeekDayCrontab(CrontabExpressionModel model) { if (string.IsNullOrWhiteSpace(model.WeeklyModeTime)) { model.WeeklyModeTime = "12:00"; } if (!(model.WeeklyMode && checkIfIsValidTime(model.WeeklyModeTime))) { return; } var daysArray = new Dictionary <string, bool>(); daysArray.Add("MON", model.Monday); daysArray.Add("TUE", model.Tuesday); daysArray.Add("WED", model.Wednesday); daysArray.Add("THU", model.Thursday); daysArray.Add("FRI", model.Friday); daysArray.Add("SAT", model.Saturday); daysArray.Add("SUN", model.Sunday); var days = ""; var checkedDays = daysArray.Where(r => r.Value); var builder = new StringBuilder(); builder.Append(days); foreach (var cd in checkedDays) { builder.Append(cd.Key.ToUpper() + ","); } days = builder.ToString().Trim(','); if (string.IsNullOrWhiteSpace(days)) { days = "*"; } var minutes = int.Parse(model.WeeklyModeTime.Split(':')[1]); var hours = int.Parse(model.WeeklyModeTime.Split(':')[0]); model.OutputCrontabString = $"0 {minutes} {hours} ? * {days}"; }
public static CrontabExpressionModel Parse(string crontab, bool customMode) { CrontabExpressionModel model = new CrontabExpressionModel(); if (string.IsNullOrEmpty(crontab)) { model.CustomMode = customMode; return(model); } if (customMode) { model.CustomMode = true; model.CustomCrontabString = crontab; return(model); } else { model.OutputCrontabString = crontab; var re = new Regex(@"0 0/([0-9]|[0-5][0-9]) \* 1/1 \* \?"); if (re.IsMatch(crontab)) { model.MinuteMode = true; model.MinuteModeMinutes = re.Match(crontab).Groups[1].Value; return(model); } re = new Regex(@"0 0 0/([0-9]|[0-2][0-9]) 1/1 \* \?"); if (re.IsMatch(crontab)) { model.HourlyMode = true; model.HourlyModeHours = re.Match(crontab).Groups[1].Value; return(model); } re = new Regex(@"0 ([0-9]|[0-5][0-9]) ([0-9]|[0-2][0-9]) \* \* \?"); if (re.IsMatch(crontab)) { model.DailyMode = true; string minutes = FillTwoDigits(re.Match(crontab).Groups[1].Value); string hours = FillTwoDigits(re.Match(crontab).Groups[2].Value); model.DailyModeTime = $"{hours}:{minutes}"; return(model); } re = new Regex(@"0 ([0-9]|[0-5][0-9]) ([0-9]|[0-2][0-9]) ([1-9]|0[1-9]|[1-2][0-9]|3[0-1])W? \* \?"); if (re.IsMatch(crontab)) { model.MonthlyMode = true; model.MonthlyWeekdayMode = crontab.Contains("W"); string minutes = FillTwoDigits(re.Match(crontab).Groups[1].Value); string hours = FillTwoDigits(re.Match(crontab).Groups[2].Value); string day = re.Match(crontab).Groups[3].Value; model.MonthlyModeTime = $"{hours}:{minutes}"; model.MonthlyModeDay = day; return(model); } re = new Regex(@"0 ([0-9]|[0-5][0-9]) ([0-9]|[0-2][0-9]) LW? \* \?"); if (re.IsMatch(crontab)) { model.LastDayOfMonthMode = true; model.LastWeekDayOfMonthMode = crontab.Contains("LW"); string minutes = FillTwoDigits(re.Match(crontab).Groups[1].Value); string hours = FillTwoDigits(re.Match(crontab).Groups[2].Value); model.LastDayOfMonthModeTime = $"{hours}:{minutes}"; return(model); } re = new Regex(@"0 ([0-9]|[0-5][0-9]) ([0-9]|[0-2][0-9]) \? \* (MON,?)?(TUE,?)?(WED,?)?(THU,?)?(FRI,?)?(SAT,?)?(SUN,?)?"); if (re.IsMatch(crontab)) { model.WeeklyMode = true; string minutes = FillTwoDigits(re.Match(crontab).Groups[1].Value); string hours = FillTwoDigits(re.Match(crontab).Groups[2].Value); model.WeeklyModeTime = $"{hours}:{minutes}"; model.Monday = crontab.Contains("MON"); model.Tuesday = crontab.Contains("TUE"); model.Wednesday = crontab.Contains("WED"); model.Thursday = crontab.Contains("THU"); model.Friday = crontab.Contains("FRI"); model.Saturday = crontab.Contains("SAT"); model.Sunday = crontab.Contains("SUN"); return(model); } //No match -> assume custom mode model.CustomCrontabString = crontab; model.CustomMode = true; return(model); } }