/// <summary> /// get the first time of run time of the day /// </summary> /// <param name="dayFrequency">daily frequency</param> /// <returns></returns> private TimeSpan GetFristRunTime(DayFrequency dayFrequency) { if (dayFrequency.Option == 1) { return(dayFrequency.RunOnceTime); } else { return(dayFrequency.StartTime); } }
public DateTime NextTime(DateTime curTime) { DateTime defaultTime = DateTime.MaxValue; DateTime nextTime; DateTime datePart; //NextTime date TimeSpan timePart; //NextTime time Schema schema = this; if (schema.State == SchemaState.Enable) //enable { if (schema.Type == SchemaType.RunOnce) //run one time only { if (schema.SchemaInfo.RunOnceTime < curTime) //large than one time { return(defaultTime); } else { return(schema.SchemaInfo.RunOnceTime); } } else //repeating { datePart = schema.SchemaInfo.RunTime.StartDate.Date; DayFrequency dayFrequency = schema.SchemaInfo.DayFrequency; timePart = GetFristRunTime(dayFrequency); //if (schema.SchemaInfo.RunTime.IsInfinity == true) //infinity { //date if (schema.SchemaInfo.Frequency.FrequencyType == FrequencyType.Day)//daily { double DayInterval = (double)schema.SchemaInfo.Frequency.EveryDay.RunInterval; while (datePart < curTime.Date) // { datePart = datePart.AddDays(DayInterval); } //is large than the Max value of daily frequency range in next run time if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = datePart.AddDays(DayInterval); timePart = GetFristRunTime(dayFrequency); } } else if (schema.SchemaInfo.Frequency.FrequencyType == FrequencyType.Week)//weekly { double WeekInterval = (double)(schema.SchemaInfo.Frequency.EveryWeek.RunInterval); DayOfWeek[] daysOfWeek = schema.SchemaInfo.Frequency.EveryWeek.DaysOfWeek; if (daysOfWeek.Length > 0) { if (schema.SchemaInfo.Frequency.EveryWeek.RunInterval <= 1) //every week { if (datePart < curTime.Date) // { datePart = curTime.Date; } datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); //is large than the Max value of daily frequency range in next run time if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = datePart.AddDays(1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); timePart = GetFristRunTime(dayFrequency); } } else//inteval in weeks { if (datePart >= curTime.Date)//less than start date { datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); //is large than the Max value of daily frequency range in next run time if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { if (datePart.DayOfWeek != DayOfWeek.Sunday) { datePart = datePart.AddDays(1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); } else { datePart = datePart.AddDays((WeekInterval - 1) * 7 + 1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); } timePart = GetFristRunTime(dayFrequency); } } else//current date large than start date { for (int s = 0; s < 7; s++) { if (datePart.DayOfWeek == DayOfWeek.Monday && s == 0) { datePart = datePart.AddDays(7.0); break; } if (datePart.DayOfWeek != DayOfWeek.Monday) { datePart = datePart.AddDays(1.0); } else { break; } } if (datePart.AddDays(-1.0) >= curTime.Date) { datePart = GetNextChooseDaysOfWeek(curTime.Date, daysOfWeek, WeekInterval); //is large than the Max value of daily frequency range in next run time if (datePart == curTime.Date) { if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { if (datePart.DayOfWeek != DayOfWeek.Sunday) { datePart = datePart.AddDays(1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); } else { datePart = datePart.AddDays((WeekInterval - 1) * 7 + 1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); } timePart = GetFristRunTime(dayFrequency); } } else { timePart = GetFristRunTime(dayFrequency); } } else { datePart = datePart.AddDays((WeekInterval - 1.0) * 7.0); if (datePart < curTime.Date) { while (datePart < curTime.Date) { datePart = datePart.AddDays((WeekInterval) * 7.0); } datePart = datePart.AddDays(-WeekInterval * 7.0); if (datePart.AddDays(7.0) >= curTime.Date) { datePart = curTime.Date; } else { datePart = datePart.AddDays(WeekInterval * 7.0); } } datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); //is large than the Max value of daily frequency range in next run time if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { if (datePart.DayOfWeek != DayOfWeek.Sunday) { datePart = datePart.AddDays(1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); } else { datePart = datePart.AddDays((WeekInterval - 1) * 7 + 1.0); datePart = GetNextChooseDaysOfWeek(datePart, daysOfWeek, WeekInterval); } timePart = GetFristRunTime(dayFrequency); } } } } } } else if (schema.SchemaInfo.Frequency.FrequencyType == FrequencyType.Month)//monthly { int MonthInterval = schema.SchemaInfo.Frequency.EveryMonth.RunInterval; int nDays = schema.SchemaInfo.Frequency.EveryMonth.WhichDay; //which day int nWeeks = schema.SchemaInfo.Frequency.EveryMonth.WhichWeek; //which week DaysOfWeek dayName = schema.SchemaInfo.Frequency.EveryMonth.DayOfWeek; //day of week if (schema.SchemaInfo.Frequency.EveryMonth.RunInterval <= 1) //monthly { //the ? day of each month if (schema.SchemaInfo.Frequency.EveryMonth.Option == 1) //which day { if (datePart <= curTime.Date) // { if (nDays >= curTime.Day) //this month { if (nDays == curTime.Day) { datePart = curTime.Date; //is large than the Max value of daily frequency range in next run time if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = curTime.AddMonths(1); //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); timePart = GetFristRunTime(dayFrequency); } } else//nDays is lager { //datePart = new DateTime(curTime.Year, curTime.Month, nDays); datePart = GetRunDayInMonth(curTime, nDays, MonthInterval); timePart = GetFristRunTime(dayFrequency); } } else//next month { datePart = curTime.AddMonths(1); //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); timePart = GetFristRunTime(dayFrequency); } } else//dataPart is large { if (nDays >= datePart.Day)// current month { //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); timePart = GetFristRunTime(dayFrequency); } else//next month { datePart = datePart.AddMonths(1); //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); timePart = GetFristRunTime(dayFrequency); } } } else//every month ,week ,day { if (datePart < curTime.Date) { datePart = curTime.Date; } while (GetDaysOfWeekInMonth(datePart, nWeeks, dayName) < datePart) { DateTime temp = datePart.AddMonths(1); datePart = new DateTime(temp.Year, temp.Month, 1); } datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = datePart.AddDays(1.0); datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); } } } else//interval month { if (datePart < curTime.Date)//scheduel date is smaller than current date { if (datePart.Year == curTime.Year && datePart.Month == curTime.Month)//same month and year { //in ?day of the year if (schema.SchemaInfo.Frequency.EveryMonth.Option == 1)//which day { if (curTime.Day <= nDays) { //datePart = new DateTime(curTime.Year, curTime.Month, nDays); datePart = GetRunDayInMonth(curTime, nDays, MonthInterval); } else { datePart = curTime.AddMonths(MonthInterval); datePart = new DateTime(datePart.Year, datePart.Month, nDays); } if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = curTime.AddMonths(MonthInterval); //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); } } else//which day which wee { datePart = curTime.Date; while (GetDaysOfWeekInMonth(datePart, nWeeks, dayName) < datePart) { DateTime temp = datePart.AddMonths(MonthInterval); datePart = new DateTime(temp.Year, temp.Month, 1); } datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { DateTime temp = datePart.AddMonths(MonthInterval); datePart = new DateTime(temp.Year, temp.Month, 1); datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); } } } else//different year or month ,curTime is large { //while (datePart.Day != 1) //{ // datePart = datePart.AddDays(1.0); //} DateTime tt = datePart.AddMonths(1); datePart = new DateTime(tt.Year, tt.Month, 1); datePart = datePart.AddMonths(MonthInterval - 1); while (datePart < curTime.Date) // { datePart = datePart.AddMonths(MonthInterval); } datePart = datePart.AddMonths(-MonthInterval); //the first day of datePart, if (datePart.Month == curTime.Month)//datepart of month is same as current time of month { //the ?day of the month if (schema.SchemaInfo.Frequency.EveryMonth.Option == 1)//which day { datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = datePart.AddMonths(MonthInterval); //datePart = new DateTime(datePart.Year,datePart.Month,nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); } } else//which day of which week { datePart = curTime.Date; while (GetDaysOfWeekInMonth(datePart, nWeeks, dayName) < datePart) { DateTime temp = datePart.AddMonths(MonthInterval); datePart = new DateTime(temp.Year, temp.Month, 1); } datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { DateTime temp = datePart.AddMonths(MonthInterval); datePart = new DateTime(temp.Year, temp.Month, 1); datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); } } } else//current month is out of weekinmonth { datePart = datePart.AddMonths(MonthInterval); //the ?day of the month if (schema.SchemaInfo.Frequency.EveryMonth.Option == 1)//which day { datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); //datePart = datePart.AddDays(nDays - 1); timePart = GetFristRunTime(dayFrequency); } else//which day of which week { //while (GetDaysOfWeekInMonth(datePart, nWeeks, dayName) < datePart) //{ // DateTime temp = datePart.AddMonths(MonthInterval); // datePart = new DateTime(temp.Year, temp.Month, 1); //} datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); timePart = GetFristRunTime(dayFrequency); } } } } else//scheduel start date is large than current date { //the ? day of interval month if (schema.SchemaInfo.Frequency.EveryMonth.Option == 1)//which day { if (nDays >= datePart.Day) { //datePart = new DateTime(datePart.Year,datePart.Month,nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); } else { datePart = datePart.AddMonths(MonthInterval); //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); } if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { datePart = datePart.AddMonths(MonthInterval); //datePart = new DateTime(datePart.Year, datePart.Month, nDays); datePart = GetRunDayInMonth(datePart, nDays, MonthInterval); } } else//which day of which month { while (GetDaysOfWeekInMonth(datePart, nWeeks, dayName) < datePart) { DateTime temp = datePart.AddMonths(MonthInterval); datePart = new DateTime(temp.Year, temp.Month, 1); } datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); if (IsOverLastRunTime(datePart, curTime, dayFrequency, out timePart)) { DateTime temp = datePart.AddMonths(MonthInterval); datePart = new DateTime(temp.Year, temp.Month, 1); datePart = GetDaysOfWeekInMonth(datePart, nWeeks, dayName); } } } } } } if (schema.SchemaInfo.RunTime.IsInfinity == false) //infinity { if (datePart != defaultTime) { if (schema.SchemaInfo.RunTime.EndDate.Date < datePart) { //over infinity return(defaultTime); } } else { return(defaultTime); } } nextTime = new DateTime(datePart.Year, datePart.Month, datePart.Day, timePart.Hours, timePart.Minutes, timePart.Seconds); return(nextTime); } } return(defaultTime); }
/// <summary> ///current time is over than last run time /// </summary> /// <param name="curTimeSpan">current date time</param> /// <param name="dayFrequency">daily frequency</param> /// <param name="nextTimeSpan">next run time</param> /// <returns></returns> private bool IsOverLastRunTime(DateTime datePart, DateTime curTime, DayFrequency dayFrequency, out TimeSpan nextTimeSpan) { bool isOverLastRunTime; TimeSpan curTimeSpan = curTime.TimeOfDay; TimeSpan startTime = dayFrequency.StartTime; TimeSpan endTime = dayFrequency.EndTime; TimeSpan runIntervalTimeSpan; nextTimeSpan = startTime; int nIntervalSeconds = 1; if (dayFrequency.TimeUnit == TimeUnit.Hour) { runIntervalTimeSpan = new TimeSpan(dayFrequency.RunInterval, 0, 0); nIntervalSeconds = dayFrequency.RunInterval * 3600; } else { runIntervalTimeSpan = new TimeSpan(0, dayFrequency.RunInterval, 0); nIntervalSeconds = dayFrequency.RunInterval * 60; } if (datePart <= curTime.Date) { if (dayFrequency.Option == 1)//run one time only { if (curTimeSpan > dayFrequency.RunOnceTime) { isOverLastRunTime = true; } else { isOverLastRunTime = false; nextTimeSpan = dayFrequency.RunOnceTime; } } else//interval run time { if (curTimeSpan > endTime)//is over current date EndTime { isOverLastRunTime = true; } else { if (curTimeSpan < startTime)//before current date StartTime { isOverLastRunTime = false; nextTimeSpan = startTime; } else//after current date StartTime { int nCurTimeSeconds = (int)curTime.TimeOfDay.TotalSeconds; int nStartTimeSeconds = (int)startTime.TotalSeconds; int nEndTimeSeconds = (int)endTime.TotalSeconds; int nNextTimeSpanSeconds = nCurTimeSeconds + (nIntervalSeconds - ((nCurTimeSeconds - nStartTimeSeconds) % nIntervalSeconds)); if (nNextTimeSpanSeconds > nEndTimeSeconds) { isOverLastRunTime = true; nextTimeSpan = startTime; } else { int hours = nNextTimeSpanSeconds / 3600; int m = nNextTimeSpanSeconds % 3600; int minutes = m / 60; int seconds = m % 60; nextTimeSpan = new TimeSpan(hours, minutes, seconds); isOverLastRunTime = false; } } } } } else { isOverLastRunTime = false; if (dayFrequency.Option == 1) { nextTimeSpan = dayFrequency.RunOnceTime; } else { nextTimeSpan = startTime; } } return(isOverLastRunTime); }
private void btnConfirm_Click(object sender, EventArgs e) { if (txtSchemaName.Text.Trim() == "") { MessageBox.Show("Name can't be empty!"); txtSchemaName.Focus(); return; } if (textBoxDatabase.Text.Trim() == "") { MessageBox.Show("Databaase can't be empty!"); txtSchemaName.Focus(); return; } if (textBoxSql.Text.Trim() == "") { MessageBox.Show("Sql can't be empty!"); txtSchemaName.Focus(); return; } Schema schema = new Schema(); schema.SchemaId = SchemaId; schema.Name = txtSchemaName.Text.Trim(); schema.UserName = textBoxUserName.Text.Trim(); schema.Password = Hubble.Framework.Security.DesEncryption.Encrypt(new byte[] { 0x14, 0x0A, 0x0C, 0x0E, 0x0A, 0x11, 0x42, 0x58 }, textBoxPassword.Text.Trim()); schema.Database = textBoxDatabase.Text.Trim(); schema.Sql = textBoxSql.Text.Trim(); schema.Description = txtSummary.Text; schema.Type = (cobSchemaType.SelectedIndex == 0) ? SchemaType.RunOnce : SchemaType.RunRepeat; schema.State = (cbState.Checked == true) ? SchemaState.Enable : SchemaState.Disable; //Content SchemaInfo schemaInfo = new SchemaInfo(); //Frequency Frequency frequency = new Frequency(); EveryDay everyDay = new EveryDay(); EveryWeek everyWeek = new EveryWeek(); EveryMonth everyMonth = new EveryMonth(); //Daily DayFrequency dayFrequency = new DayFrequency(); //Available time AvailableTime runTime = new AvailableTime(); if (schema.Type == SchemaType.RunOnce) { schemaInfo.RunOnceTime = Convert.ToDateTime(dtRunOnceDate.Text + " " + dtRunOnceTime.Text); } else { FrequencyType frequencyType; switch (cobFrequencyType.SelectedIndex) { case 0: frequencyType = FrequencyType.Day; everyDay.RunInterval = (int)(numEveryDayRunInterval.Value); break; case 1: frequencyType = FrequencyType.Week; everyWeek.RunInterval = (int)(numEveryWeekRunInterval.Value); DayOfWeek[] dayOfWeek = new DayOfWeek[7]; int i = 0; if (cbMonday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Monday; } if (cbTuesday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Tuesday; } if (cbWednesday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Wednesday; } if (cbThursday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Thursday; } if (cbFriday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Friday; } if (cbSaturday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Saturday; } if (cbSunday.Checked == true) { dayOfWeek[i++] = DayOfWeek.Sunday; } // everyWeek.DaysOfWeek = dayOfWeek; break; case 2: frequencyType = FrequencyType.Month; if (rdoOptionWhichDay.Checked == true) { everyMonth.Option = 1; everyMonth.RunInterval = (int)(numEveryMonthRunInterval1.Value); everyMonth.WhichDay = (int)(numWhichDay.Value); } else { everyMonth.Option = 2; everyMonth.RunInterval = (int)(numEveryMonthRunInterval2.Value); switch (cobWhichWeek.SelectedIndex) { case 0: case 1: case 2: case 3: everyMonth.WhichWeek = cobWhichWeek.SelectedIndex + 1; break; case 4: everyMonth.WhichWeek = 9; break; } switch (cobDaysOfWeek.SelectedIndex) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: everyMonth.DayOfWeek = (DaysOfWeek)(cobDaysOfWeek.SelectedIndex); break; case 7: everyMonth.DayOfWeek = DaysOfWeek.Day; break; } } break; default: frequencyType = FrequencyType.Day; everyDay.RunInterval = (int)(numEveryDayRunInterval.Value); break; } frequency.FrequencyType = frequencyType; frequency.EveryDay = everyDay; frequency.EveryWeek = everyWeek; frequency.EveryMonth = everyMonth; //Daily if (rdoRunOnce.Checked == true)//One time only { //string[] timeString = dtDayFrequencyRunOnceTime.Text.Split(':'); int hour = dtDayFrequencyRunOnceTime.Value.Hour; int minute = dtDayFrequencyRunOnceTime.Value.Minute; int second = dtDayFrequencyRunOnceTime.Value.Second; //int hour = int.Parse(timeString[0]); //int minute = int.Parse(timeString[1]); //int second = int.Parse(timeString[2]); dayFrequency.Option = 1; dayFrequency.RunOnceTime = new TimeSpan(hour, minute, second); } else { dayFrequency.Option = 2; dayFrequency.RunInterval = (int)(numRunInterval.Value); if (cobIntervalUnit.SelectedIndex == 0)//hour { dayFrequency.TimeUnit = TimeUnit.Hour; } else//minute { dayFrequency.TimeUnit = TimeUnit.Minute; } //string[] startTime = dtStartTime.Text.Split(':'); int hour = dtStartTime.Value.Hour; int minute = dtStartTime.Value.Minute; int second = dtStartTime.Value.Second; //int hour = int.Parse(startTime[0]); //int minute = int.Parse(startTime[1]); //int second = int.Parse(startTime[2]); dayFrequency.StartTime = new TimeSpan(hour, minute, second); //string[] endTime = dtEndTime.Text.Split(':'); hour = dtEndTime.Value.Hour; minute = dtEndTime.Value.Minute; second = dtEndTime.Value.Second; //hour = int.Parse(endTime[0]); //minute = int.Parse(endTime[1]); //second = int.Parse(endTime[2]); dayFrequency.EndTime = new TimeSpan(hour, minute, second); } //Duration runTime.IsInfinity = rdoInfinity.Checked; runTime.StartDate = Convert.ToDateTime(dtStartDate.Text); runTime.EndDate = Convert.ToDateTime(dtEndDate.Text); schemaInfo.RunTime = runTime; schemaInfo.DayFrequency = dayFrequency; schemaInfo.Frequency = frequency; } schema.SchemaInfo = schemaInfo; _Schema = schema; _Result = DialogResult.OK; Close(); }