public void WeekDaysofMonthTest() { var firstTest = new MonthlyOccurrence(DateTime.MinValue, DateTime.MaxValue , new Month[] { Month.January, Month.March } , new WeekOfMonth[] { WeekOfMonth.First } , new DayOfWeek[] { DayOfWeek.Sunday, DayOfWeek.Tuesday }); visualiseOccurrencesInRange(firstTest, today.AddDays(-today.DayOfYear), today.AddDays(365 - today.DayOfYear)); foreach (var dt in AOccurrence.DateRange(DateTime.MinValue, DateTime.MaxValue)) { firstTest.GetNext(dt); } }
public void AllWeekDaysofMonthTest() { //Run for the whole date range for every week of the month for all days of the week. var firstTest = new MonthlyOccurrence(DateTime.MinValue, DateTime.MaxValue , EVERY_MONTH_OF_THE_YEAR , new WeekOfMonth[] { WeekOfMonth.First, WeekOfMonth.Second, WeekOfMonth.Third, WeekOfMonth.Fourth, WeekOfMonth.Last } , EVERY_DAY_OF_THE_WEEK); // visualiseOccurrencesInRange(firstTest, today.AddDays(-today.DayOfYear), today.AddDays(365 - today.DayOfYear)); foreach (var dt in AOccurrence.DateRange(DateTime.MinValue, DateTime.MaxValue)) { Assert.IsNotNull(firstTest.GetNext(dt)); } }
/// <summary> /// Return which Day of the month the date represents. For example : /// July 17'th 2016 is the 3'rd Sunday of July 2016, therefore we would return WeekOfMonth.Third (0 based index) /// </summary> /// <param name="date">The day of the month</param> /// <returns>Returns 0 first, 1, 2,3,4 or 5 (last) occurrence of the day of the week in the month</returns> public static WeekOfMonth GetOrdinalInMonth(this DateTime date) { // create a range iterator of all the days in the month of the date var range = AOccurrence.DateRange(new DateTime(date.Year, date.Month, 1), new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)) ); // get oonly the days in the range where the days of the week of the date are the same... //TODO: one can also do weird +7,-7 math until the bounds of the month are reached.. would that be that much faster??? var nextRange = range.Where(x => x.DayOfWeek == date.DayOfWeek); // get the index of the day in the month where the date is the passed date int ordinal = nextRange.Select((time, index) => new { time, index }).First(x => date == x.time).index; if (ordinal < 0 || ordinal > 5) { throw new ApplicationException("Something went wrong with determining the Ordinal in the Month"); } return((WeekOfMonth)ordinal); }
//range(start_date, end_date): //for n in range((end_date - start_date).days): // yield start_date + datetime.timedelta(n) /// <summary> /// This method will draw the ocurrences on a timeline /// </summary> /// <param name="occ">An IOccurrence instance</param> /// <param name="start"></param> /// <param name="end"></param> public void visualiseOccurrencesInRange(IOccurrence occ, DateTime?start = null, DateTime?end = null) { if (!end.HasValue) { end = occ.End; } if (!start.HasValue) { start = occ.Start; } Trace.WriteLine(String.Format(" Range from {0} to {1} ", start, end)); var now = DateTime.Now.Date; Trace.Write("|"); foreach (var sd in AOccurrence.DateRange(start.Value, end.Value)) { Trace.Write(String.Format(" {0,5:d} |", (sd - now).Days + 1)); } Trace.Write("\n|"); foreach (var sd in AOccurrence.DateRange(start.Value, end.Value)) { Trace.Write(String.Format("{0:yyyy-MM-dd}|", sd)); } Trace.Write("\n|"); foreach (var sd in AOccurrence.DateRange(start.Value, end.Value)) { if (sd == occ.GetNext(sd, true)) { Trace.Write(String.Format("{0:yyyy-MM-dd}|", sd)); } else { Trace.Write(" |"); } } }
public void EverySecondTuesdayOfTheMonth() { //Run for the whole date range for every week of the month for all days of the week. var everySecondTuesday = new MonthlyOccurrence(DateTime.MinValue, DateTime.MaxValue , EVERY_MONTH_OF_THE_YEAR , new WeekOfMonth[] { WeekOfMonth.Second } , new DayOfWeek[] { DayOfWeek.Tuesday }); visualiseOccurrencesInRange(everySecondTuesday, today.AddDays(-today.DayOfYear), today.AddDays(365 - today.DayOfYear)); foreach (var dt in AOccurrence.DateRange(DateTime.MinValue, DateTime.MaxValue)) { var result = everySecondTuesday.GetNext(dt); if (result.HasValue) { Assert.AreEqual(DayOfWeek.Tuesday, result.Value.DayOfWeek, $"Got result of {result} which is a {result.Value.DayOfWeek}"); } //This test is inconclusive as it does not test the nulls } }