/// <summary>Adds some <see cref="System.DateTime"/> objects into a specific date schedule. /// </summary> /// <param name="dateSchedule">The date schedule.</param> /// <exception cref="NotOperableException">Thrown, if the current instance it not operable.</exception> public void AddDatesToDateSchedule(DateSchedule dateSchedule) { if (IsOperable == false) { throw new NotOperableException("BackwardDateScheduleRule"); } DateTime firstDate, terminationDate; m_TimeSpanDescription.GetStartAndEndDate(m_ReferenceDate, dateSchedule.HolidayCalendar, out firstDate, out terminationDate, dateSchedule.Logging); if (terminationDate < firstDate) { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentOutOfRangeGreaterEqual, "End date [" + terminationDate.ToShortDateString() + "]", "first date [" + firstDate.ToShortDateString() + "]")); } dateSchedule.Add(terminationDate); // add the end date // gets the date which is used as 'base date' for the date generation loop: DateTime seed = terminationDate; if ((m_LastRegularDate != null) && (m_LastRegularDate.HasValue)) // 'last regular date' is given { seed = m_LastRegularDate.Value; if (seed > terminationDate) { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentOutOfRangeLessEqual, "Last regular date [" + seed.ToShortDateString() + "]", "end date [" + terminationDate.ToShortDateString() + "]")); } } if (m_SeedBusinessDayConvention != null) // perhaps adjust the 'base date' which is used for the date generation loop { seed = m_SeedBusinessDayConvention.GetAdjustedDate(seed, dateSchedule.HolidayCalendar); } else { seed = m_BusinessDayConvention.GetAdjustedDate(seed, dateSchedule.HolidayCalendar); } dateSchedule.Add(seed); // gets the exit condition for the loop: DateTime exitDate = firstDate; // perhaps not a business day if ((m_FirstRegularDate != null) && (m_FirstRegularDate.HasValue)) { exitDate = m_FirstRegularDate.Value; if (exitDate > seed) { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentOutOfRangeLessEqual, "First regular date [" + exitDate.ToShortDateString() + "]", "last [regular] date [" + seed.ToShortDateString() + "]")); } } if (m_Frequency.IsOnceFrequency()) // end date and seed date are already added { if (exitDate != firstDate) // perhaps add the first regular date { dateSchedule.Add(exitDate); // one may apply the business day convention to the first regular date } } else // do the loop with respect to the frequency { TenorTimeSpan frequencyTenor = m_Frequency.GetFrequencyTenor(); DateTime runningDate = m_BusinessDayConvention.GetAdjustedDate(seed.AddTenorTimeSpan(frequencyTenor), dateSchedule.HolidayCalendar); int periodIndex = -1; while (runningDate >= exitDate) { dateSchedule.Add(runningDate); periodIndex--; runningDate = seed.AddTenorTimeSpan(frequencyTenor, periodIndex); runningDate = m_BusinessDayConvention.GetAdjustedDate(runningDate, dateSchedule.HolidayCalendar); } } dateSchedule.Add(firstDate); }