Calendar instances implement the rules for determining if a Date is a business day in the location it represents.

The rules for public holidays vary from country to country but with the exception of special occaisions (e.g. royal weddings, funerals, etc.) can usually be predicted years in advance.

On first reference this class bootstraps an initial set of Calendar instances by processing a XML data file.
예제 #1
0
            /// <summary>
            /// Applies the algorithm defined by the <see cref="CalendarRule"/> to calculate
            /// the date that a holiday falls in the given year using the given
            /// <see cref="Calendar"/> to avoid other non-business dates.
            /// </summary>
            /// <param name="calendar">The <see cref="Calendar"/> used to identify non-business days.</param>
            /// <param name="year">The year to generate the holiday for.</param>
            /// <returns>The adjusted <see cref="Date"/> that the holiday falls on.</returns>
            public override Date Generate(Calendar calendar, int year)
            {
                int 	limit = Date.MonthLength (month, year);

                if (when == LAST) {
                    for (int index = limit - 6; index <= limit; ++index) {
                        Date date = new Date (index, month, year);

                        if (date.Weekday == day) return (date);
                    }
                }
                else {
                    int count = 0;

                    for (int index = 1; index <= limit; ++index) {
                        Date date = new Date (index, month, year);

                        if ((date.Weekday == day) && (++count == when))
                            return (date);
                    }
                }
                return (null);
            }
예제 #2
0
 /// <summary>
 /// Applies the algorithm defined by the <see cref="CalendarRule"/> to calculate
 /// the date that a holiday falls in the given year using the given
 /// <see cref="Calendar"/> to avoid other non-business dates.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> used to identify non-business days.</param>
 /// <param name="year">The year to generate the holiday for.</param>
 /// <returns>The adjusted <see cref="Date"/> that the holiday falls on.</returns>
 public override Date Generate(Calendar calendar, int year)
 {
     return (new Date (1, 1, year).PlusDays (easterMonday [year - 1900] + offset - 1));
 }
예제 #3
0
 /// <summary>
 /// Applies the algorithm defined by the <see cref="CalendarRule"/> to calculate
 /// the date that a holiday falls in the given year using the given
 /// <see cref="Calendar"/> to avoid other non-business dates.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> used to identify non-business days.</param>
 /// <param name="year">The year to generate the holiday for.</param>
 /// <returns>The adjusted <see cref="Date"/> that the holiday falls on.</returns>
 public override Date Generate(Calendar calendar, int year)
 {
     return (dateRoll.Adjust (calendar, new Date (dayOfMonth, month, year)));
 }
예제 #4
0
 /// <summary>
 /// Adjusts dates which fall on a Saturday to the preceding Friday, and
 /// those falling on a Sunday to the following Monday. This convention
 /// is used by some national holidays, for example Christmas Day in the
 /// USA.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
 /// <param name="date">The <see cref="Date"/> to adjust.</param>
 /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
 private static Date Weekend(Calendar calendar, Date date)
 {
     switch (date.Weekday) {
     case Date.SATURDAY:		return (date.PlusDays (-1));
     case Date.SUNDAY:		return (date.PlusDays (+1));
     }
     return (date);
 }
예제 #5
0
 /// <summary>
 /// Applies the algorithm defined by the <b>CalendarRule</b> to calculate
 /// the date that a holiday falls in the given year using the given
 /// <see cref="Calendar"/> to avoid other non-business dates.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> used to identify non-business days.</param>
 /// <param name="year">The year to generate the holiday for.</param>
 /// <returns>The adjusted <see cref="Date"/> that the holiday falls on.</returns>
 public abstract Date Generate(Calendar calendar, int year);
예제 #6
0
 /// <summary>
 /// A dummy adjustment that performs no change to the date.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
 /// <param name="date">The <see cref="Date"/> to adjust.</param>
 /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
 private static Date None(Calendar calendar, Date date)
 {
     return (date);
 }
예제 #7
0
 /// <summary>
 /// Adjusts a <see cref="Date"/> to the preceding business day if it falls
 /// on a holiday.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
 /// <param name="date">The <see cref="Date"/> to adjust.</param>
 /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
 private static Date Preceding(Calendar calendar, Date date)
 {
     while (!calendar.IsBusinessDay (date))
         date = date.PlusDays (-1);
     return (date);
 }
예제 #8
0
        /// <summary>
        ///	Adjusts a <see cref="Date"/> to the previous business day if it
        ///	falls on a holiday unless that would move the date into the
        ///	previous month in which case it is rolled to a following date.
        /// </summary>
        /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
        /// <param name="date">The <see cref="Date"/> to adjust.</param>
        /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
        private static Date ModPreceding(Calendar calendar, Date date)
        {
            Date			result = Preceding (calendar, date);

            if (date.Month != result.Month)
                result = Following (calendar, date);
            return (result);
        }
예제 #9
0
 /// <summary>
 /// Adjusts a <see cref="Date"/> to the next business day if it falls on
 /// a holiday.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
 /// <param name="date">The <see cref="Date"/> to adjust.</param>
 /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
 private static Date Following(Calendar calendar, Date date)
 {
     while (!calendar.IsBusinessDay (date))
         date = date.PlusDays (+1);
     return (date);
 }
예제 #10
0
 /// <summary>
 /// Adjusts a <see cref="Date"/> which falls on a holiday within the
 /// indicated <see cref="Calendar"/> to an appropriate business day.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
 /// <param name="date">The <see cref="Date"/> to adjust.</param>
 /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
 public abstract Date Adjust(Calendar calendar, Date date);
 /// <summary>
 /// Adjusts a <see cref="Date"/> which falls on a holiday within the
 /// indicated <see cref="Calendar"/> to an appropriate business day.
 /// </summary>
 /// <param name="calendar">The <see cref="Calendar"/> to be used.</param>
 /// <param name="date">The <see cref="Date"/> to adjust.</param>
 /// <returns>A (possibly) adjusted <see cref="Date"/> instance.</returns>
 public override Date Adjust(Calendar calendar, Date date)
 {
     return (function (calendar, date));
 }
예제 #12
0
        // --------------------------------------------------------------------
        /// <summary>
        /// Generates a set of dates according to schedule defined by a start date,
        /// an end date, an interval, roll convention and a calendar.
        /// </summary>
        /// <param name="start">The start date.</param>
        /// <param name="end">The end date.</param>
        /// <param name="frequency">The frequency of the schedule (e.g. 6M).</param>
        /// <param name="roll">The date roll convention or <c>null</c>.</param>
        /// <param name="calendar">The holiday calendar or <c>null</c>.</param>
        /// <returns>An array of calculated and adjusted dates.</returns>
        private static Date[] GenerateSchedule(Date start, Date end,
            Interval frequency, DateRoll roll, Calendar calendar)
        {
            Date		current = start;
            ArrayList	found	= new ArrayList ();
            Date []		dates;

            while (Less (current, end)) {
                Date		adjusted;

                if (roll != null)
                    adjusted = roll.Adjust (calendar, current);
                else
                    adjusted = current;

                if (!found.Contains (adjusted))
                    found.Add (adjusted);

                if (frequency.Period == Period.TERM) {
                    if (Equal (current, start))
                        current = end;
                    else
                        break;
                }
                else
                    current = current.Plus (frequency);
            }

            found.CopyTo (dates  = new Date [found.Count]);
            return (dates);
        }