|
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace RecurringClasses
{
/// <summary>
/// Concrete class to handle annually recurring events
/// for example, 3rd of October
/// J. Finn October 2008
/// james.finn@gmail.com
/// </summary>
class YearlyRecurPeriod : absRecurPeriod
{
int yearFrequency = 0; // how many years between appointments
int monthOfTheYear = 0; // month of the year
int dayOfTheMonth = 0; // day of the month
/// <summary>
/// constructor sets up variables necessary to respond to GetNext
/// </summary>
/// <param name="StartDate">Start Date</param>
/// <param name="EndDate">End Date</param>
/// <param name="repeatNode">XML Node containing recurring definition</param>
public YearlyRecurPeriod(ref DateTime StartDate, ref DateTime EndDate, XmlNode RepeatNode)
: base(StartDate, EndDate)
{
// extracts values for yearFrequency, month of the year, and day of the month
if (!Int32.TryParse(RepeatNode.Attributes["yearFrequency"].Value,
out yearFrequency))
{
throw new Exception("Unexpected yearFrequency: " + RepeatNode.Attributes["yearFrequency"].Value);
}
if (!Int32.TryParse(RepeatNode.Attributes["month"].Value,
out monthOfTheYear))
{
throw new Exception("Unexpected Day: " + RepeatNode.Attributes["day"].Value);
}
if (!Int32.TryParse(RepeatNode.Attributes["day"].Value,
out dayOfTheMonth))
{
throw new Exception("Unexpected Day: " + RepeatNode.Attributes["day"].Value);
}
// adjust start date to first selected day
initialStartDate = new DateTime(this.initialStartDate.Year,
monthOfTheYear, dayOfTheMonth,
this.initialStartDate.Hour, this.initialStartDate.Minute,
this.initialStartDate.Second);
initialEndDate = new DateTime(this.initialEndDate.Year,
monthOfTheYear, dayOfTheMonth,
this.initialEndDate.Hour, this.initialEndDate.Minute,
this.initialEndDate.Second);
currentStartDate = initialStartDate;
currentEndDate = initialEndDate;
}
/// <summary>
/// Increments dates to get the next date in the recurrence pattern
/// </summary>
protected override void incrementDates()
{
currentStartDate = currentStartDate.AddYears(yearFrequency);
currentEndDate = currentEndDate.AddYears(yearFrequency);
if (currentStartDate.Day != dayOfTheMonth ||
currentStartDate.Month != monthOfTheYear)
{
try
{
currentStartDate = new DateTime(this.currentStartDate.Year,
monthOfTheYear, dayOfTheMonth,
this.currentStartDate.Hour, this.currentStartDate.Minute,
this.currentStartDate.Second);
currentEndDate = new DateTime(this.currentEndDate.Year,
monthOfTheYear, dayOfTheMonth,
this.currentEndDate.Hour, this.currentEndDate.Minute,
this.currentEndDate.Second);
}
catch (ArgumentOutOfRangeException ex)
{
// invalid date leaves the original calculation
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.