/// <summary> /// Gets the occurrences with option to override the ICal.Event.DTStart /// </summary> /// <param name="beginDateTime">The begin date time.</param> /// <param name="endDateTime">The end date time.</param> /// <param name="scheduleStartDateTimeOverride">The schedule start date time override.</param> /// <returns></returns> public IList <Occurrence> GetOccurrences(DateTime beginDateTime, DateTime?endDateTime, DateTime?scheduleStartDateTimeOverride) { var occurrences = new List <Occurrence>(); DDay.iCal.Event calEvent = GetCalendarEvent(); if (calEvent == null) { return(occurrences); } if (scheduleStartDateTimeOverride.HasValue) { calEvent.DTStart = new DDay.iCal.iCalDateTime(scheduleStartDateTimeOverride.Value); } if (calEvent.DTStart != null) { var exclusionDates = new List <DateRange>(); if (this.CategoryId.HasValue && this.CategoryId.Value > 0) { var category = CategoryCache.Get(this.CategoryId.Value); if (category != null) { exclusionDates = category.ScheduleExclusions .Where(e => e.Start.HasValue && e.End.HasValue) .ToList(); } } foreach (var occurrence in endDateTime.HasValue ? ScheduleICalHelper.GetOccurrences(calEvent, beginDateTime, endDateTime.Value) : ScheduleICalHelper.GetOccurrences(calEvent, beginDateTime)) { bool exclude = false; if (exclusionDates.Any() && occurrence.Period.StartTime != null) { var occurrenceStart = occurrence.Period.StartTime.Value; if (exclusionDates.Any(d => d.Start.Value <= occurrenceStart && d.End.Value >= occurrenceStart)) { exclude = true; } } if (!exclude) { occurrences.Add(occurrence); } } } return(occurrences); }
/// <summary> /// Returns value indicating if check-in was active at a current time for this schedule. /// </summary> /// <param name="time">The time.</param> /// <returns></returns> public bool WasCheckInActive(DateTime time) { if (!IsCheckInEnabled) { return(false); } var calEvent = this.GetCalenderEvent(); if (calEvent != null && calEvent.DTStart != null) { var checkInStart = calEvent.DTStart.AddMinutes(0 - CheckInStartOffsetMinutes.Value); if (time.TimeOfDay.TotalSeconds < checkInStart.TimeOfDay.TotalSeconds) { return(false); } var checkInEnd = calEvent.DTEnd; if (CheckInEndOffsetMinutes.HasValue) { checkInEnd = calEvent.DTStart.AddMinutes(CheckInEndOffsetMinutes.Value); } // If compare is greater than zero, then check-in offset end resulted in an end time in next day, in // which case, don't need to compare time int checkInEndDateCompare = checkInEnd.Date.CompareTo(checkInStart.Date); if (checkInEndDateCompare < 0) { // End offset is prior to start (Would have required a neg number entered) return(false); } if (checkInEndDateCompare == 0 && time.TimeOfDay.TotalSeconds > checkInEnd.TimeOfDay.TotalSeconds) { // Same day, but end time has passed return(false); } var occurrences = ScheduleICalHelper.GetOccurrences(calEvent, time.Date); return(occurrences.Count > 0); } return(false); }
/// <summary> /// Gets a list of scheduled start datetimes between the two specified dates, sorted by datetime. /// </summary> /// <param name="beginDateTime">The begin date time.</param> /// <param name="endDateTime">The end date time.</param> /// <returns></returns> public virtual List <DateTime> GetScheduledStartTimes(DateTime beginDateTime, DateTime endDateTime) { var result = new List <DateTime>(); DDay.iCal.Event calEvent = GetCalenderEvent(); if (calEvent != null) { // use ThreadSafe helper method to get occurrences var occurrences = ScheduleICalHelper.GetOccurrences(calEvent, beginDateTime, endDateTime); foreach (var startDateTime in occurrences.Where(a => a.Period != null && a.Period.StartTime != null).Select(a => a.Period.StartTime.Value)) { // ensure the the datetime is DateTimeKind.Local since iCal returns DateTimeKind.UTC result.Add(DateTime.SpecifyKind(startDateTime, DateTimeKind.Local)); } } return(result); }
/// <summary> /// Gets the check in times. /// </summary> /// <param name="beginDateTime">The begin date time.</param> /// <returns></returns> public virtual List <CheckInTimes> GetCheckInTimes(DateTime beginDateTime) { var result = new List <CheckInTimes>(); if (IsCheckInEnabled) { var scheduledStartTimes = this.GetScheduledStartTimes(beginDateTime, beginDateTime.Date.AddDays(1)); DDay.iCal.Event calEvent = GetCalenderEvent(); if (calEvent != null && calEvent.DTStart != null) { var occurrences = ScheduleICalHelper.GetOccurrences(calEvent, beginDateTime, beginDateTime.Date.AddDays(1)); foreach (var occurrence in occurrences .Where(a => a.Period != null && a.Period.StartTime != null && a.Period.EndTime != null) .Select(a => new { Start = a.Period.StartTime.Value, End = a.Period.EndTime.Value })) { var checkInTimes = new CheckInTimes(); checkInTimes.Start = DateTime.SpecifyKind(occurrence.Start, DateTimeKind.Local); checkInTimes.End = DateTime.SpecifyKind(occurrence.End, DateTimeKind.Local); checkInTimes.CheckInStart = checkInTimes.Start.AddMinutes(0 - CheckInStartOffsetMinutes.Value); if (CheckInEndOffsetMinutes.HasValue) { checkInTimes.CheckInEnd = checkInTimes.Start.AddMinutes(CheckInEndOffsetMinutes.Value); } else { checkInTimes.CheckInEnd = checkInTimes.End; } result.Add(checkInTimes); } } } return(result); }
/// <summary> /// Returns value indicating if the schedule was active at a current time. /// </summary> /// <param name="time">The time.</param> /// <returns></returns> public bool WasScheduleActive(DateTime time) { var calEvent = this.GetCalenderEvent(); if (calEvent != null && calEvent.DTStart != null) { if (time.TimeOfDay.TotalSeconds < calEvent.DTStart.TimeOfDay.TotalSeconds) { return(false); } if (time.TimeOfDay.TotalSeconds > calEvent.DTEnd.TimeOfDay.TotalSeconds) { return(false); } var occurrences = ScheduleICalHelper.GetOccurrences(calEvent, time.Date); return(occurrences.Count > 0); } return(false); }
/// <summary> /// Gets the Schedule's iCalender Event. /// </summary> /// <value> /// A <see cref="DDay.iCal.Event"/> representing the iCalendar event for this Schedule. /// </value> public virtual DDay.iCal.Event GetCalenderEvent() { return(ScheduleICalHelper.GetCalenderEvent(iCalendarContent)); }