/// <summary> /// Determine whether the given time is 'included' by the /// Calendar. /// </summary> /// <param name="timeUtc"></param> /// <returns></returns> public override bool IsTimeIncluded(DateTimeOffset timeUtc) { if (CalendarBase != null && CalendarBase.IsTimeIncluded(timeUtc) == false) { return(false); } //Before we start, apply the correct timezone offsets. timeUtc = TimeZoneUtil.ConvertTime(timeUtc, TimeZone); DateTimeOffset startOfDayInMillis = GetStartOfDay(timeUtc); DateTimeOffset endOfDayInMillis = GetEndOfDay(timeUtc); DateTimeOffset timeRangeStartingTimeInMillis = GetTimeRangeStartingTimeUtc(timeUtc); DateTimeOffset timeRangeEndingTimeInMillis = GetTimeRangeEndingTimeUtc(timeUtc); if (!InvertTimeRange) { if (timeUtc > startOfDayInMillis && timeUtc < timeRangeStartingTimeInMillis || timeUtc > timeRangeEndingTimeInMillis && timeUtc < endOfDayInMillis) { return(true); } return(false); } if (timeUtc >= timeRangeStartingTimeInMillis && timeUtc <= timeRangeEndingTimeInMillis) { return(true); } return(false); }
/// <summary> /// Determine the next time (in milliseconds) that is 'included' by the /// Calendar after the given time. Return the original value if timeStamp is /// included. Return 0 if all days are excluded. /// </summary> /// <param name="timeUtc"></param> /// <returns></returns> /// <seealso cref="ICalendar.GetNextIncludedTimeUtc"/> public override DateTimeOffset GetNextIncludedTimeUtc(DateTimeOffset timeUtc) { DateTimeOffset nextIncludedTime = timeUtc.AddMilliseconds(OneMillis); while (!IsTimeIncluded(nextIncludedTime)) { if (!InvertTimeRange) { //If the time is in a range excluded by this calendar, we can // move to the end of the excluded time range and continue // testing from there. Otherwise, if nextIncludedTime is // excluded by the baseCalendar, ask it the next time it // includes and begin testing from there. Failing this, add one // millisecond and continue testing. if (nextIncludedTime >= GetTimeRangeStartingTimeUtc(nextIncludedTime) && nextIncludedTime <= GetTimeRangeEndingTimeUtc(nextIncludedTime)) { nextIncludedTime = GetTimeRangeEndingTimeUtc(nextIncludedTime).AddMilliseconds(OneMillis); } else if (CalendarBase != null && !CalendarBase.IsTimeIncluded(nextIncludedTime)) { nextIncludedTime = CalendarBase.GetNextIncludedTimeUtc(nextIncludedTime); } else { nextIncludedTime = nextIncludedTime.AddMilliseconds(1); } } else { //If the time is in a range excluded by this calendar, we can // move to the end of the excluded time range and continue // testing from there. Otherwise, if nextIncludedTime is // excluded by the baseCalendar, ask it the next time it // includes and begin testing from there. Failing this, add one // millisecond and continue testing. if (nextIncludedTime < GetTimeRangeStartingTimeUtc(nextIncludedTime)) { nextIncludedTime = GetTimeRangeStartingTimeUtc(nextIncludedTime); } else if (nextIncludedTime > GetTimeRangeEndingTimeUtc(nextIncludedTime)) { //(move to start of next day) nextIncludedTime = GetEndOfDay(nextIncludedTime); nextIncludedTime = nextIncludedTime.AddMilliseconds(1); } else if (CalendarBase != null && !CalendarBase.IsTimeIncluded(nextIncludedTime)) { nextIncludedTime = CalendarBase.GetNextIncludedTimeUtc(nextIncludedTime); } else { nextIncludedTime = nextIncludedTime.AddMilliseconds(1); } } } return(nextIncludedTime); }