public override DateTime?GetNextDate(DateTime dateTime) { var dateN = FixedCountdown.GetNextDate(dateTime); if (dateN.HasValue) { var date = dateN; var totalAdjustment = 0.0; while (!DaysOfWeek.Contains(date.Value.DayOfWeek) || date <= dateTime) { date = date.Value.AddDays(DayAdjustment); totalAdjustment += DayAdjustment; if (date <= dateTime) { date = GetNextDate(dateTime.AddDays(7.0 + totalAdjustment)); break; } } return(date); } else { return(null); } }
private void AbbPreviousWeek_Click(object sender, RoutedEventArgs e) { DaysOfWeek days = viewModel.Controller.ViewDays; if (days.GetDaysOfWeek().Count() == 1) { if (days.Contains(viewModel.Settings.RefTime.DayOfWeek)) { viewModel.Settings.RefTime = viewModel.Settings.RefTime.AddDays(-7); } if (days == DaysOfWeek.Monday) { days = DaysOfWeek.Sunday; } else { days = (DaysOfWeek)((int)days >> 1); } viewModel.Controller.ViewDays = days; } else { viewModel.Settings.RefTime = viewModel.Settings.RefTime.AddDays(-7); } }
public List <TimeSpan> GetRunTimes(DateTime runDate, bool excludePastRunTimes = true) { List <TimeSpan> runTimesForDay = new List <TimeSpan>(); var now = DateTime.Now; if (excludePastRunTimes && runDate.Date < now.Date) { return(runTimesForDay); } if (runDate.Date > EndDate || runDate.Date < StartDate || !DaysOfWeek.Contains(runDate.DayOfWeek)) { return(runTimesForDay); } DateTime nextRunTime = new DateTime(runDate.Year, runDate.Month, runDate.Day, StartTime.Hours, StartTime.Minutes, StartTime.Seconds); DateTime lastRunTime = new DateTime(runDate.Year, runDate.Month, runDate.Day, EndTime.Hours, EndTime.Minutes, EndTime.Seconds); var runDateIsToday = runDate.Date == now.Date; while (nextRunTime <= lastRunTime) { if (!excludePastRunTimes || runDate.Date > now.Date || (runDateIsToday && nextRunTime >= now)) { runTimesForDay.Add(nextRunTime.TimeOfDay); } nextRunTime = nextRunTime.AddSeconds(FrequencyInSeconds); } return(runTimesForDay); }
public bool CheckDateTime(DateTime?dateTime = null) { var time = dateTime ?? DateTime.Now; return(Minutes.Contains(time.Minute) && Hours.Contains(time.Hour) && Months.Contains(time.Month - 1) && (DaysOfMonth.Contains(time.Day - 1) || DaysOfWeek.Contains((int)time.DayOfWeek - 1))); }
public RecurrenceDayOfWeek RemoveDayOfWeek(string dayOfWeek) { RecurrenceDayOfWeek day; if (Enum.TryParse(dayOfWeek, out day) && DaysOfWeek.Contains(day)) { DaysOfWeek.RemoveAll(item => item == day); } return(day); }
public RecurrenceDayOfWeek?Contains(string dayOfWeek) { RecurrenceDayOfWeek day; if (Enum.TryParse(dayOfWeek, out day) && DaysOfWeek.Contains(day)) { return(day); } return(null); }
/// <summary> /// Check if a date is valid according to this restriction. /// </summary> /// <param name="dateTime">The date to check.</param> /// <returns>True if the date is valid, False if the date is not valid and null if the restriction does not apply to the given date.</returns> public bool?IsValid(DateTime dateTime) { if (Dates == null && DaysOfWeek == null && (TimeStart == null || TimeEnd == null)) { return(null); } return((Dates?.Contains(dateTime.Date) ?? true) && (DaysOfWeek?.Contains(dateTime.DayOfWeek) ?? true) && ((TimeStart == null || TimeEnd == null) || (TimeStart.Value.TimeOfDay <= dateTime.TimeOfDay && TimeEnd.Value.TimeOfDay >= dateTime.TimeOfDay)) ? (bool?)(Type == RestrictionType.Allow) : null); //Restriction is not applicable }
public RecurrenceDayOfWeek?AddDayOfWeek(string dayOfWeek) { RecurrenceDayOfWeek day; if (Enum.TryParse(dayOfWeek, out day) && !DaysOfWeek.Contains(day)) { DaysOfWeek.Add(day); } else { return(null); } return(day); }
public bool IsInTimeWindow(DateTime nowTime) { if (!DaysOfWeek.Contains(nowTime.DayOfWeek)) { return(false); } if (Holidays.Contains(nowTime.Date)) { return(false); } if (times.Count == 0) { return(true); } else { return((from t in times where t.From.TimeOfDay <= nowTime.TimeOfDay && t.To.TimeOfDay >= nowTime.TimeOfDay select t).Count() > 0); } }
private DateTime FindNextRelevantDate(DateTime datetime) { var relevantDateTime = new DateTime(); day = datetime.Day; month = datetime.Month; year = datetime.Year; var maxDay = Days.Max(); var maxMonth = Months.Max(); var maxYear = Years.Max(); var maxDate = new DateTime(maxYear, maxMonth, maxDay).Date; bool added = false; while (datetime.Date != maxDate) { if (DaysOfWeek.Contains((int)datetime.DayOfWeek)) { relevantDateTime = datetime; break; } else { added = false; } AddNext(Days, ref day, ref added); var monthAdded = AddNext(Months, ref month, ref added); if (monthAdded) { day = GetClosestDay(datetime, year, month); } AddNext(Years, ref year, ref added); datetime = new DateTime(year, month, day, datetime.Hour, datetime.Minute, datetime.Second, datetime.Millisecond); } return(relevantDateTime); }
private DateTime FindPrevRelevantDate(DateTime datetime) { var relevantDateTime = new DateTime(); day = datetime.Day; month = datetime.Month; year = datetime.Year; var minDay = Days.Min(); var minMonth = Months.Min(); var minYear = Years.Min(); var minDate = new DateTime(minYear, minMonth, minDay).Date; bool subtracted = false; while (datetime.Date != minDate) { if (DaysOfWeek.Contains((int)datetime.DayOfWeek)) { relevantDateTime = datetime; break; } else { subtracted = false; } SubtractNext(Days, ref day, ref subtracted); var monthSubtracted = SubtractNext(Months, ref month, ref subtracted); if (monthSubtracted) { day = GetClosestDay(datetime, year, month); } SubtractNext(Years, ref year, ref subtracted); datetime = new DateTime(year, month, day, datetime.Hour, datetime.Minute, datetime.Second, datetime.Millisecond); } return(relevantDateTime); }
public override DateTime?GetNextDate(DateTime dateTime) { var startDate = dateTime; while (!DaysOfWeek.Contains(startDate.DayOfWeek)) { startDate = startDate.AddDays(-1); } var dateN = FixedCountdown.GetNextDate(startDate); if (dateN.HasValue) { var date = dateN.Value; while (!DaysOfWeek.Contains(date.DayOfWeek)) { date = date.AddDays(DayAdjustment); } return(date); } else { return(null); } }
public DateTime GetNextRecurrence(DateTime baseTime) { DateTime recurrence = baseTime; if (DaysOfMonth != null) { var findDay = (from d in DaysOfMonth where d > recurrence.Day select d).FirstOrDefault(); if (findDay == 0) { recurrence = recurrence.AddMonths(1); findDay = (from d in DaysOfMonth where d > recurrence.Day select d).FirstOrDefault(); } if (findDay == 0) { var err = string.Format("Cannot find day of month recurrence for recurrence: {0}", ToString()); _log.Error(err); throw new ArgumentOutOfRangeException(err); } if (findDay > DateTime.DaysInMonth(baseTime.Year, baseTime.Month)) { findDay = DateTime.DaysInMonth(baseTime.Year, baseTime.Month); } recurrence = new DateTime(recurrence.Year, recurrence.Month, findDay, recurrence.Hour, recurrence.Minute, recurrence.Second); } else if (DaysOfWeek != null) { if (!DaysOfWeek.All(d => d >= (int)DayOfWeek.Sunday && d <= (int)DayOfWeek.Saturday)) { var err = string.Format("Cannot find day of week recurrence for recurrence {0}", ToString()); _log.Error(err); throw new ArgumentOutOfRangeException(err); } while (!DaysOfWeek.Contains((int)recurrence.DayOfWeek)) { recurrence = recurrence.AddDays(1.0); } } else { if (MonthsLater > 0) { recurrence = recurrence.AddMonths(MonthsLater); } if (DaysLater > 0) { recurrence = recurrence.AddDays(DaysLater); } if (HoursLater > 0) { recurrence = recurrence.AddHours(HoursLater); } if (MinutesLater > 0) { recurrence = recurrence.AddMinutes(MinutesLater); } } if (AtFixedTime != DateTime.MinValue) { recurrence = new DateTime(recurrence.Year, recurrence.Month, recurrence.Day, AtFixedTime.Hour, AtFixedTime.Minute, AtFixedTime.Second); if (recurrence < DateTime.UtcNow) { recurrence = recurrence.AddDays(1.0); } } _dblog.InfoFormat("Recurrence {0} from base time {1} next {2}", ToString(), baseTime, recurrence); return(recurrence); }