/// <summary> /// This method is used to calculate the date on which a floating day occurs (for example, the 4th /// Thursday in November). /// </summary> /// <param name="year">The year in which the day occurs.</param> /// <param name="month">The month in which the day occurs.</param> /// <param name="occur">The occurrence of the day of the week on which the day falls.</param> /// <param name="dowDay">The day of the week on which the day occurs.</param> /// <param name="offset">The number of days before or after the calculated date on which the day actually /// falls.</param> /// <returns>Returns a <see cref="DateTime" /> object that represents the date calculated from the /// settings.</returns> /// <remarks><para>Use a positive value for the <c>nOffset</c> parameter for a number of days after the /// calculated date or a negative number for a number of days before the calculated date.</para> /// /// <para>Normally, this value will be zero so that the calculated date is the actual date returned. /// However, in cases where a date is calculated in terms of the number of days before or after a given /// date, this can be set to the offset to adjust the calculated date.</para> /// /// <para>For example, to calculate the day after Thanksgiving, the value of this parameter would be set /// to 1 (one day after Thanksgiving, which is the 4th Thursday in November). You cannot use the 4th /// Friday to calculate the date because if the month starts on a Friday, the calculated date would be a /// week too early. As such, the <c>nOffset</c> parameter is used instead.</para></remarks> /// <example> /// <code language="cs"> /// // Returns 11/28/2002 (Thanksgiving) /// dtThanksgiving = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 0); /// /// // Returns 11/29/2002 (Day after Thanksgiving) /// dtDayAfterTG = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 1); /// /// // Returns 11/22/2002 (Fourth Friday isn't after the fourth /// // Thursday in 2002 hence the use of the nOffset parameter /// // in the call above). /// dtFourthFri = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Friday, 0); /// </code> /// <code language="vbnet"> /// ' Returns 11/28/2002 (Thanksgiving) /// dtThanksgiving = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 0) /// /// ' Returns 11/29/2002 (Day after Thanksgiving) /// dtDayAfterTG = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 1) /// /// ' Returns 11/22/2002 (Fourth Friday isn't after the fourth /// ' Thursday in 2002 hence the use of the nOffset parameter /// ' in the call above). /// dtFourthFri = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Friday, 0) /// </code> /// </example> /// <exception cref="ArgumentException">This is thrown if <c>None</c> is passed for the <c>DayOccurrence</c> /// parameter.</exception> public static DateTime CalculateFloatingDate(int year, int month, DayOccurrence occur, System.DayOfWeek dowDay, int offset) { DateTime dtDate; if (occur == DayOccurrence.None) { throw new ArgumentException(LR.GetString("ExDUOccurIsNone"), "occur"); } // Calculating a specific occurrence or the last one? if (occur != DayOccurrence.Last) { // Specific occurrence dtDate = new DateTime(year, month, 1); dtDate = dtDate.AddDays((((int)dowDay + 7 - (int)dtDate.DayOfWeek) % 7) + (((int)occur - 1) * 7)); } else { // Get the last occurrence of the month dtDate = new DateTime(year, month, DateTime.DaysInMonth(year, month)); dtDate = dtDate.AddDays(0 - (((int)dtDate.DayOfWeek + 7 - (int)dowDay) % 7)); } // Return the date plus any additional offset return(dtDate.AddDays(offset)); }
/// <summary> /// Construct a new holiday object that occurs on a floating date /// </summary> /// <param name="month">The month of the holiday.</param> /// <param name="dow">The day of the week on which it occurs.</param> /// <param name="occur">The occurrence of the day of the week on which the floating holiday falls.</param> /// <param name="offset">The number of days before or after the calculated floating date on which the /// holiday actually occurs. See the <see cref="Offset"/> property for more information about this /// parameter.</param> /// <param name="description">A description of the holiday.</param> /// <exception cref="System.ArgumentOutOfRangeException">An exception will be thrown if the month is not /// between 1 and 12.</exception> /// <include file='DateExamples.xml' path='Examples/Holiday/HelpEx[@name="Ex1"]/*' /> public FloatingHoliday(DayOccurrence occur, DayOfWeek dow, int month, int offset, string description) { this.Occurrence = occur; this.Weekday = dow; this.Month = month; this.Offset = offset; this.Description = description; }
/// <summary> /// This method is used to calculate the date on which a specific occurrence of any of a set of days /// occurs (for example, the 4th weekday in November or the last weekend day in January). /// </summary> /// <param name="year">The year in which the day occurs</param> /// <param name="month">The month in which the day occurs</param> /// <param name="occur">The occurrence of the day of the week on which the day falls</param> /// <param name="days">The day(s) of the week on which the day can occurs</param> /// <param name="offset">The number of days before or after the calculated date on which the day falls</param> /// <returns>Returns a <see cref="DateTime" /> object that represents the date calculated from the /// settings.</returns> /// <remarks><para>This method is intended for use in finding an occurrence of any one of a set of days /// of the week and is normally used with the <see cref="DaysOfWeek.Weekdays"/> or /// <see cref="DaysOfWeek.Weekends"/> day of week value. However, the days of week parameter can be any /// valid combination of days including an individual day of the week.</para> /// /// <para>Use a positive value for the <c>nOffset</c> parameter for a number of days after the calculated /// date or a negative number for a number of days before the calculated date.</para> /// /// <para>Normally, this value will be zero so that the calculated date is the actual date returned. /// However, in cases where a date is calculated in terms of the number of days before or after a given /// date, this can be set to the offset to adjust the calculated date. Note that if used, the date /// returned may not be on one of the days of the week specified to calculate the original unadjusted /// date.</para></remarks> /// <example> /// <code language="cs"> /// // Returns 01/06/2004 (fourth weekday in Jan 2004) /// dtFourthWeekday = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 0); /// /// // Returns 01/08/2004 (fourth weekday plus 2 days) /// dtPlusTwo = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 2); /// </code> /// <code language="vbnet"> /// ' Returns 01/06/2004 (fourth weekday in Jan 2004) /// dtFourthWeekday = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 0) /// /// ' Returns 01/08/2004 (fourth weekday plus 2 days) /// dtPlusTwo = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 2) /// </code> /// </example> /// <exception cref="ArgumentException">This is thrown if <c>None</c> is passed for the <c>DayOccurrence</c> /// parameter.</exception> public static DateTime CalculateOccurrenceDate(int year, int month, DayOccurrence occur, DaysOfWeek days, int offset) { DateTime dtDate; int count = 0, occurrence = (int)occur; if(occur == DayOccurrence.None) throw new ArgumentException(LR.GetString("ExDUOccurIsNone"), "occur"); // Calculating a specific occurrence or the last one? if(occur != DayOccurrence.Last) { dtDate = new DateTime(year, month, 1); while(count != occurrence) { switch(dtDate.DayOfWeek) { case DayOfWeek.Sunday: if((days & DaysOfWeek.Sunday) != 0) count++; break; case DayOfWeek.Monday: if((days & DaysOfWeek.Monday) != 0) count++; break; case DayOfWeek.Tuesday: if((days & DaysOfWeek.Tuesday) != 0) count++; break; case DayOfWeek.Wednesday: if((days & DaysOfWeek.Wednesday) != 0) count++; break; case DayOfWeek.Thursday: if((days & DaysOfWeek.Thursday) != 0) count++; break; case DayOfWeek.Friday: if((days & DaysOfWeek.Friday) != 0) count++; break; case DayOfWeek.Saturday: if((days & DaysOfWeek.Saturday) != 0) count++; break; } if(count != occurrence) dtDate = dtDate.AddDays(1); } } else { // Find last occurrence count++; dtDate = new DateTime(year, month, DateTime.DaysInMonth(year, month)); while(count != 0) { switch(dtDate.DayOfWeek) { case DayOfWeek.Sunday: if((days & DaysOfWeek.Sunday) != 0) count--; break; case DayOfWeek.Monday: if((days & DaysOfWeek.Monday) != 0) count--; break; case DayOfWeek.Tuesday: if((days & DaysOfWeek.Tuesday) != 0) count--; break; case DayOfWeek.Wednesday: if((days & DaysOfWeek.Wednesday) != 0) count--; break; case DayOfWeek.Thursday: if((days & DaysOfWeek.Thursday) != 0) count--; break; case DayOfWeek.Friday: if((days & DaysOfWeek.Friday) != 0) count--; break; case DayOfWeek.Saturday: if((days & DaysOfWeek.Saturday) != 0) count--; break; } if(count != 0) dtDate = dtDate.AddDays(-1); } } // Return the date plus any additional offset return dtDate.AddDays(offset); }
/// <summary> /// This method is used to calculate the date on which a floating day occurs (for example, the 4th /// Thursday in November). /// </summary> /// <param name="year">The year in which the day occurs.</param> /// <param name="month">The month in which the day occurs.</param> /// <param name="occur">The occurrence of the day of the week on which the day falls.</param> /// <param name="dowDay">The day of the week on which the day occurs.</param> /// <param name="offset">The number of days before or after the calculated date on which the day actually /// falls.</param> /// <returns>Returns a <see cref="DateTime" /> object that represents the date calculated from the /// settings.</returns> /// <remarks><para>Use a positive value for the <c>nOffset</c> parameter for a number of days after the /// calculated date or a negative number for a number of days before the calculated date.</para> /// /// <para>Normally, this value will be zero so that the calculated date is the actual date returned. /// However, in cases where a date is calculated in terms of the number of days before or after a given /// date, this can be set to the offset to adjust the calculated date.</para> /// /// <para>For example, to calculate the day after Thanksgiving, the value of this parameter would be set /// to 1 (one day after Thanksgiving, which is the 4th Thursday in November). You cannot use the 4th /// Friday to calculate the date because if the month starts on a Friday, the calculated date would be a /// week too early. As such, the <c>nOffset</c> parameter is used instead.</para></remarks> /// <example> /// <code language="cs"> /// // Returns 11/28/2002 (Thanksgiving) /// dtThanksgiving = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 0); /// /// // Returns 11/29/2002 (Day after Thanksgiving) /// dtDayAfterTG = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 1); /// /// // Returns 11/22/2002 (Fourth Friday isn't after the fourth /// // Thursday in 2002 hence the use of the nOffset parameter /// // in the call above). /// dtFourthFri = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Friday, 0); /// </code> /// <code language="vbnet"> /// ' Returns 11/28/2002 (Thanksgiving) /// dtThanksgiving = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 0) /// /// ' Returns 11/29/2002 (Day after Thanksgiving) /// dtDayAfterTG = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Thursday, 1) /// /// ' Returns 11/22/2002 (Fourth Friday isn't after the fourth /// ' Thursday in 2002 hence the use of the nOffset parameter /// ' in the call above). /// dtFourthFri = DateUtils.CalculateFloatingDate(2002, 11, /// DayOccurrence.Fourth, DayOfWeek.Friday, 0) /// </code> /// </example> /// <exception cref="ArgumentException">This is thrown if <c>None</c> is passed for the <c>DayOccurrence</c> /// parameter.</exception> public static DateTime CalculateFloatingDate(int year, int month, DayOccurrence occur, System.DayOfWeek dowDay, int offset) { DateTime dtDate; if(occur == DayOccurrence.None) throw new ArgumentException(LR.GetString("ExDUOccurIsNone"), "occur"); // Calculating a specific occurrence or the last one? if(occur != DayOccurrence.Last) { // Specific occurrence dtDate = new DateTime(year, month, 1); dtDate = dtDate.AddDays((((int)dowDay + 7 - (int)dtDate.DayOfWeek) % 7) + (((int)occur - 1) * 7)); } else { // Get the last occurrence of the month dtDate = new DateTime(year, month, DateTime.DaysInMonth(year, month)); dtDate = dtDate.AddDays(0 - (((int)dtDate.DayOfWeek + 7 - (int)dowDay) % 7)); } // Return the date plus any additional offset return dtDate.AddDays(offset); }
/// <summary> /// This method is used to calculate the date on which a specific occurrence of any of a set of days /// occurs (for example, the 4th weekday in November or the last weekend day in January). /// </summary> /// <param name="year">The year in which the day occurs</param> /// <param name="month">The month in which the day occurs</param> /// <param name="occur">The occurrence of the day of the week on which the day falls</param> /// <param name="days">The day(s) of the week on which the day can occurs</param> /// <param name="offset">The number of days before or after the calculated date on which the day falls</param> /// <returns>Returns a <see cref="DateTime" /> object that represents the date calculated from the /// settings.</returns> /// <remarks><para>This method is intended for use in finding an occurrence of any one of a set of days /// of the week and is normally used with the <see cref="DaysOfWeek.Weekdays"/> or /// <see cref="DaysOfWeek.Weekends"/> day of week value. However, the days of week parameter can be any /// valid combination of days including an individual day of the week.</para> /// /// <para>Use a positive value for the <c>nOffset</c> parameter for a number of days after the calculated /// date or a negative number for a number of days before the calculated date.</para> /// /// <para>Normally, this value will be zero so that the calculated date is the actual date returned. /// However, in cases where a date is calculated in terms of the number of days before or after a given /// date, this can be set to the offset to adjust the calculated date. Note that if used, the date /// returned may not be on one of the days of the week specified to calculate the original unadjusted /// date.</para></remarks> /// <example> /// <code language="cs"> /// // Returns 01/06/2004 (fourth weekday in Jan 2004) /// dtFourthWeekday = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 0); /// /// // Returns 01/08/2004 (fourth weekday plus 2 days) /// dtPlusTwo = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 2); /// </code> /// <code language="vbnet"> /// ' Returns 01/06/2004 (fourth weekday in Jan 2004) /// dtFourthWeekday = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 0) /// /// ' Returns 01/08/2004 (fourth weekday plus 2 days) /// dtPlusTwo = DateUtils.CalculateOccurrenceDate(2004, 1, /// DayOccurrence.Fourth, DaysOfWeek.Weekdays, 2) /// </code> /// </example> /// <exception cref="ArgumentException">This is thrown if <c>None</c> is passed for the <c>DayOccurrence</c> /// parameter.</exception> public static DateTime CalculateOccurrenceDate(int year, int month, DayOccurrence occur, DaysOfWeek days, int offset) { DateTime dtDate; int count = 0, occurrence = (int)occur; if (occur == DayOccurrence.None) { throw new ArgumentException(LR.GetString("ExDUOccurIsNone"), "occur"); } // Calculating a specific occurrence or the last one? if (occur != DayOccurrence.Last) { dtDate = new DateTime(year, month, 1); while (count != occurrence) { switch (dtDate.DayOfWeek) { case DayOfWeek.Sunday: if ((days & DaysOfWeek.Sunday) != 0) { count++; } break; case DayOfWeek.Monday: if ((days & DaysOfWeek.Monday) != 0) { count++; } break; case DayOfWeek.Tuesday: if ((days & DaysOfWeek.Tuesday) != 0) { count++; } break; case DayOfWeek.Wednesday: if ((days & DaysOfWeek.Wednesday) != 0) { count++; } break; case DayOfWeek.Thursday: if ((days & DaysOfWeek.Thursday) != 0) { count++; } break; case DayOfWeek.Friday: if ((days & DaysOfWeek.Friday) != 0) { count++; } break; case DayOfWeek.Saturday: if ((days & DaysOfWeek.Saturday) != 0) { count++; } break; } if (count != occurrence) { dtDate = dtDate.AddDays(1); } } } else { // Find last occurrence count++; dtDate = new DateTime(year, month, DateTime.DaysInMonth(year, month)); while (count != 0) { switch (dtDate.DayOfWeek) { case DayOfWeek.Sunday: if ((days & DaysOfWeek.Sunday) != 0) { count--; } break; case DayOfWeek.Monday: if ((days & DaysOfWeek.Monday) != 0) { count--; } break; case DayOfWeek.Tuesday: if ((days & DaysOfWeek.Tuesday) != 0) { count--; } break; case DayOfWeek.Wednesday: if ((days & DaysOfWeek.Wednesday) != 0) { count--; } break; case DayOfWeek.Thursday: if ((days & DaysOfWeek.Thursday) != 0) { count--; } break; case DayOfWeek.Friday: if ((days & DaysOfWeek.Friday) != 0) { count--; } break; case DayOfWeek.Saturday: if ((days & DaysOfWeek.Saturday) != 0) { count--; } break; } if (count != 0) { dtDate = dtDate.AddDays(-1); } } } // Return the date plus any additional offset return(dtDate.AddDays(offset)); }
/// <summary> /// Initialize a yearly recurrence pattern that occurs on a specific occurrence of a day of the week in /// the specified month at the specified yearly interval (i.e. the last Sunday in September every year). /// </summary> /// <param name="occur">The occurrence of the day of the week on which to occur</param> /// <param name="daysOfWeek">The day of the week on which to occur</param> /// <param name="month">The month in which to occur.</param> /// <param name="recurInterval">The interval between occurrences in years</param> /// <remarks>This is a convenience method that mimics the yearly recurrence pattern in Microsoft Outlook. /// When called, it sets up the recurrence for a yearly pattern that recurs at the specified interval on /// the specified occurrence of the days of the week. All rule parts are cleared prior to setting the /// monthly options but other parameters such as the start date are left alone.</remarks> /// <exception cref="ArgumentOutOfRangeException">An exception is thrown if the month is not between 1 /// and 12.</exception> /// <include file='DateExamples.xml' path='Examples/Recurrence/HelpEx[@name="Ex8"]/*' /> public void RecurYearly(DayOccurrence occur, DaysOfWeek daysOfWeek, int month, int recurInterval) { // Month should be valid if(month < 1 || month > 12) throw new ArgumentOutOfRangeException("month", month, LR.GetString("ExRecurBadMonth")); this.Frequency = RecurFrequency.Yearly; this.Interval = recurInterval; byMonth.Clear(); byWeekNo.Clear(); byYearDay.Clear(); byMonthDay.Clear(); byHour.Clear(); byMinute.Clear(); bySecond.Clear(); bySetPos.Clear(); byDay.Clear(); customProps.Clear(); byMonth.Add(month); // Set day(s) if((daysOfWeek & DaysOfWeek.Sunday) != 0) byDay.Add(new DayInstance(DayOfWeek.Sunday)); if((daysOfWeek & DaysOfWeek.Monday) != 0) byDay.Add(new DayInstance(DayOfWeek.Monday)); if((daysOfWeek & DaysOfWeek.Tuesday) != 0) byDay.Add(new DayInstance(DayOfWeek.Tuesday)); if((daysOfWeek & DaysOfWeek.Wednesday) != 0) byDay.Add(new DayInstance(DayOfWeek.Wednesday)); if((daysOfWeek & DaysOfWeek.Thursday) != 0) byDay.Add(new DayInstance(DayOfWeek.Thursday)); if((daysOfWeek & DaysOfWeek.Friday) != 0) byDay.Add(new DayInstance(DayOfWeek.Friday)); if((daysOfWeek & DaysOfWeek.Saturday) != 0) byDay.Add(new DayInstance(DayOfWeek.Saturday)); // If only one day was added, set its instance as it will be more efficient than using BYSETPOS if(byDay.Count == 1) { if(occur == DayOccurrence.Last) byDay[0].Instance = -1; else byDay[0].Instance = (int)occur; } else if(occur == DayOccurrence.Last) bySetPos.Add(-1); else bySetPos.Add((int)occur); }
/// <summary> /// Initialize a monthly recurrence pattern that occurs on a specific occurrence of a day of the week at /// the specified monthly interval (i.e. the 4th Tuesday every two months). /// </summary> /// <param name="occur">The occurrence of the day of the week on which to occur.</param> /// <param name="daysOfWeek">The days of the week on which to occur. This may be an individual week day /// or any combination of week days.</param> /// <param name="recurInterval">The interval between occurrences in months.</param> /// <remarks>This is a convenience method that mimics the monthly recurrence pattern in Microsoft /// Outlook. When called, it sets up the recurrence for a monthly pattern that recurs at the specified /// interval on the specified occurrence of the days of the week. All rule parts are cleared prior to /// setting the monthly options but other parameters such as the start date are left alone.</remarks> /// <include file='DateExamples.xml' path='Examples/Recurrence/HelpEx[@name="Ex6"]/*' /> public void RecurMonthly(DayOccurrence occur, DaysOfWeek daysOfWeek, int recurInterval) { this.Frequency = RecurFrequency.Monthly; this.Interval = recurInterval; byMonth.Clear(); byWeekNo.Clear(); byYearDay.Clear(); byMonthDay.Clear(); byHour.Clear(); byMinute.Clear(); bySecond.Clear(); bySetPos.Clear(); byDay.Clear(); customProps.Clear(); // Set day(s) if((daysOfWeek & DaysOfWeek.Sunday) != 0) byDay.Add(new DayInstance(DayOfWeek.Sunday)); if((daysOfWeek & DaysOfWeek.Monday) != 0) byDay.Add(new DayInstance(DayOfWeek.Monday)); if((daysOfWeek & DaysOfWeek.Tuesday) != 0) byDay.Add(new DayInstance(DayOfWeek.Tuesday)); if((daysOfWeek & DaysOfWeek.Wednesday) != 0) byDay.Add(new DayInstance(DayOfWeek.Wednesday)); if((daysOfWeek & DaysOfWeek.Thursday) != 0) byDay.Add(new DayInstance(DayOfWeek.Thursday)); if((daysOfWeek & DaysOfWeek.Friday) != 0) byDay.Add(new DayInstance(DayOfWeek.Friday)); if((daysOfWeek & DaysOfWeek.Saturday) != 0) byDay.Add(new DayInstance(DayOfWeek.Saturday)); // If only one day was added, set its instance as it will be more efficient than using BYSETPOS if(byDay.Count == 1) { if(occur == DayOccurrence.Last) byDay[0].Instance = -1; else byDay[0].Instance = (int)occur; } else if(occur == DayOccurrence.Last) bySetPos.Add(-1); else bySetPos.Add((int)occur); }
/// <summary> /// Add a new holiday object to the collection that occurs on a floating date /// </summary> /// <param name="month">The month of the holiday.</param> /// <param name="dow">The day of the week on which it occurs.</param> /// <param name="occur">The occurrence of the day of the week on which the floating holiday falls.</param> /// <param name="offset">The number of days before or after the calculated floating date on which the /// holiday actually occurs. See the <see cref="FloatingHoliday.Offset"/> property for more information /// about this parameter.</param> /// <param name="description">A description of the holiday.</param> public void AddFloating(DayOccurrence occur, DayOfWeek dow, int month, int offset, string description) { base.Add(new FloatingHoliday(occur, dow, month, offset, description)); }