/// <summary> /// Returns the next date after <paramref name="minDate"/> that is represented by this token. /// </summary> /// <remarks> /// This method may return a date that is before <paramref name="minDate"/> if there is no date after <paramref /// name="minDate"/> that is represented by this token. /// </remarks> /// <param name="minDate">The minimum date to return. The time part is ignored.</param> /// <param name="inclusive">A value indicating whether the returned date should be on or after rather than /// strictly after <paramref name="minDate"/>.</param> /// <returns>The next date after <paramref name="minDate"/> that is represented by this token.</returns> /// <exception cref="InvalidOperationException">If this token is not valid.</exception> public override DateTime ToDateTime(DateTime minDate, bool inclusive) { this.ThrowIfNotValid(); DateTime date = minDate.Date.AddDays(1); // Find the next date with the matching weekday DayOfWeek dayOfWeek = this.DayOfWeek ?? System.DayOfWeek.Sunday; while (date.DayOfWeek != dayOfWeek) { date = date.AddDays(1); } // Advance the date by a week if necessary DayOfWeekRelation dayOfWeekRelation = this.DayOfWeekRelation ?? Parsing.DayOfWeekRelation.Next; if (dayOfWeekRelation == Parsing.DayOfWeekRelation.AfterNext || (dayOfWeekRelation == Parsing.DayOfWeekRelation.NextWeek && dayOfWeek > minDate.DayOfWeek)) { date = date.AddDays(7); } return(date); }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="DayOfWeekDateToken"/> class and /// that its value is equal to the specified parameters. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> /// <param name="expectedDayOfWeek">The expected day of week.</param> /// <param name="expectedDayOfWeekRelation">The expected relation between the day of week and date.</param> private static void AssertAreEqual(DateToken actual, DayOfWeek? expectedDayOfWeek, DayOfWeekRelation? expectedDayOfWeekRelation) { Assert.AreEqual(typeof(DayOfWeekDateToken), actual.GetType()); Assert.AreEqual(expectedDayOfWeek, ((DayOfWeekDateToken)actual).DayOfWeek); Assert.AreEqual(expectedDayOfWeekRelation, ((DayOfWeekDateToken)actual).DayOfWeekRelation); }