/// <summary> /// Creates List[Chunk], with regular start and end dates for required intervals. Amount = 0 for all Chunks. /// </summary> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="interval"></param> /// <returns>List[Chunk] for required duration, each Chunk covering one interval.</returns> public List <Chunk> setupDatedChunksForApportionToPeriod(DateTime startDate, DateTime endDate, AppotionmentPeriod interval) { ///calendar object used for adding days/months/years to start date System.Globalization.Calendar calendar = System.Globalization.CultureInfo.CurrentCulture.Calendar; ///set up apportionment requirements List <Chunk> datedChunks = new List <Chunk>(); int periodType = (int)interval; ///i used to keep track of how many Chunks have already been created & adds appropriate no. of intervals to start date. int i = 0; ///create list of chunks with correct required start dates do { Chunk chunk = new Chunk(); TimeSpan oneDay = new TimeSpan(1, 0, 0); ///one day timespan needed to calculate end dates switch (periodType) { case 1: ///interval is daily /// chunk.StartDate = calendar.AddDays(startDate, i); chunk.EndDate = calendar.AddDays(chunk.StartDate, 1); break; case 2: ///interval is weekly /// chunk.StartDate = calendar.AddWeeks(startDate, i); chunk.EndDate = calendar.AddWeeks(chunk.StartDate, 1); break; case 3: ///interval is monthly /// chunk.StartDate = calendar.AddMonths(startDate, i); chunk.EndDate = calendar.AddMonths(chunk.StartDate, 1); break; case 4: ///interval is quarterly /// chunk.StartDate = calendar.AddMonths(startDate, 3 * i); chunk.EndDate = calendar.AddMonths(chunk.StartDate, 3 * 1); break; case 5: ///interval is annually /// chunk.StartDate = calendar.AddYears(startDate, i); chunk.EndDate = calendar.AddYears(chunk.StartDate, 1); break; } ///take on day off each date date to avoid overlapping date ranges chunk.EndDate = chunk.EndDate.Subtract(oneDay); datedChunks.Add(chunk); i++; } while (datedChunks.ElementAt(i - 1).EndDate <= endDate); return(datedChunks); }