public DayOfWeekAnnualReccurance(DayOfWeek dayOfWeek, WeekOfMonth week, Month month, bool substituteWeekend = true)
     : base(substituteWeekend)
 {
     DayOfWeek = dayOfWeek;
     Week = week;
     Month = month;
 }
Exemplo n.º 2
0
        public DynamicClockTransition(int hoursToChange, WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int month, TimeSpan transitionTimeOfDay)
        {
            if (month < 1 || month > 12)
            {
                throw new ArgumentOutOfRangeException("month", "Invalid month.");
            }

            if (!Enum.IsDefined(typeof(DayOfWeek), dayOfWeek))
            {
                throw new ArgumentException("Invalid day of week.", "dayOfWeek");
            }

            if (!Enum.IsDefined(typeof(WeekOfMonth), weekOfMonth))
            {
                throw new ArgumentException("Invalid week of month.", "weekOfMonth");
            }

            if (transitionTimeOfDay < TimeSpan.Zero || transitionTimeOfDay.Days > 0)
            {
                throw new ArgumentOutOfRangeException("transitionTimeOfDay", "The transition timespan must be a valid time of day.");
            }

            if (hoursToChange != -1 && hoursToChange != 1)
            {
                throw new ArgumentOutOfRangeException("hoursToChange", "The change should be -1 or +1 hours.");
            }

            Month               = month;
            DayOfWeek           = dayOfWeek;
            WeekOfMonth         = weekOfMonth;
            TransitionTimeOfDay = transitionTimeOfDay;
            HoursToChange       = hoursToChange;
        }
Exemplo n.º 3
0
        private DateTime GetNthDay(int year, int month, int hrs, int min, WeekOfMonth weekOfMonth)
        {
            DateTime specifiedDay;

            switch (weekOfMonth)
            {
            case WeekOfMonth.first:
                specifiedDay = new DateTime(year, month, 1, hrs, min, 0);
                break;

            case WeekOfMonth.second:
                specifiedDay = new DateTime(year, month, 2, hrs, min, 0);
                break;

            case WeekOfMonth.third:
                specifiedDay = new DateTime(year, month, 3, hrs, min, 0);
                break;

            case WeekOfMonth.fourth:
                specifiedDay = new DateTime(year, month, 4, hrs, min, 0);
                break;

            case WeekOfMonth.last:
            default:
                var lastday = DateTime.DaysInMonth(year, month);
                specifiedDay = new DateTime(year, month, lastday, hrs, min, 0);
                break;
            }
            return(specifiedDay);
        }
Exemplo n.º 4
0
 public DayOfWeekAnnualReccurance(DayOfWeek dayOfWeek, WeekOfMonth week, Month month, bool substituteWeekend = true)
     : base(substituteWeekend)
 {
     DayOfWeek = dayOfWeek;
     Week      = week;
     Month     = month;
 }
Exemplo n.º 5
0
        private static DateTime FindDayOfWeekOfMonth(DayOfWeek dayOfWeek, WeekOfMonth week, DateTime date)
        {
            var weekOrdinal = GetWeekOrdinalFromEnum(week);

            while (date.DayOfWeek.ToString() != dayOfWeek.ToString())
            {
                date = date.AddDays(1);
            }

            if (weekOrdinal < 4)
            {
                date = date.AddDays(weekOrdinal * 7);
            }
            else
            {
                if (date.AddDays(weekOrdinal * 7).Month != date.Month)
                {
                    date = date.AddDays(7 * (weekOrdinal - 1));
                }
                else
                {
                    date = date.AddDays(weekOrdinal * 7);
                }
            }

            return(date);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes an instance of <see cref="MonthlyWeekPattern"/> class.
        /// </summary>
        /// <param name="weekOfMonth">The week of the month when the event occurs.</param>
        /// <param name="daysOfWeek">The accepted days of the week when the event can occur.</param>
        /// <param name="interval">The interval of occurrences in number of months.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="interval"/> is less than 1.</exception>
        public MonthlyWeekPattern(WeekOfMonth weekOfMonth, DaysOfTheWeek daysOfWeek, int interval = 1)
        {
            Contract.Requires <ArgumentOutOfRangeException>(interval >= 1, nameof(interval));

            WeekOfMonth = weekOfMonth;
            DaysOfWeek  = daysOfWeek & DaysOfTheWeek.Any;
            Interval    = interval;
        }
Exemplo n.º 7
0
        private DateTime GetNthWeekendDay(int year, int month, int hrs, int min, WeekOfMonth weekOfMonth)
        {
            DateTime date = new DateTime(year, month, 1, hrs, min, 0);

            while (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                date = date.AddDays(1);
            }


            switch (weekOfMonth)
            {
            case WeekOfMonth.first:
                //returns the default value
                break;

            case WeekOfMonth.second:
                if (date.DayOfWeek == DayOfWeek.Saturday)
                {
                    date = date.AddDays(1);
                }
                else if (date.DayOfWeek == DayOfWeek.Sunday)
                {
                    date = date.AddDays(6);
                }
                break;

            case WeekOfMonth.third:
                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    date = date.AddDays(7);
                }

                break;

            case WeekOfMonth.fourth:
                if (date.DayOfWeek == DayOfWeek.Saturday)
                {
                    date = date.AddDays(8);
                }
                else if (date.DayOfWeek == DayOfWeek.Sunday)
                {
                    date = date.AddDays(13);
                }
                break;

            case WeekOfMonth.last:
            default:
                var lastday = DateTime.DaysInMonth(year, month);
                date = new DateTime(year, month, lastday, hrs, min, 0);
                while (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                {
                    date = date.AddDays(-1);
                }
                break;
            }
            return(date);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Convert a value to <see cref="int" />.
        /// </summary>
        /// <param name="weekOfMonth">The week of month.</param>
        /// <returns>Converted to int value.</returns>
        public static int ToInt32(this WeekOfMonth weekOfMonth)
        {
            if (weekOfMonth == WeekOfMonth.Last)
            {
                return(-1);
            }

            return((int)weekOfMonth);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes an instance of <see cref="YearlyWeekPattern"/> class.
        /// </summary>
        /// <param name="weekOfMonth">The week of the month when the event occurs.</param>
        /// <param name="daysOfWeek">The accepted days of the week when the event can occurs.</param>
        /// <param name="monthOfYear">The month when the event occurs.</param>
        /// <param name="interval">The interval of occurrences in number of years.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="monthOfYear"/> is less than 1 or greater than 12. Or,
        /// <paramref name="interval"/> is less than 1.
        /// </exception>
        public YearlyWeekPattern(WeekOfMonth weekOfMonth, DaysOfTheWeek daysOfWeek, int monthOfYear, int interval = 1)
        {
            Contract.Requires <ArgumentOutOfRangeException>(monthOfYear >= 1 && monthOfYear <= 12, nameof(monthOfYear));
            Contract.Requires <ArgumentOutOfRangeException>(interval >= 1, nameof(interval));

            MonthOfYear = monthOfYear;
            WeekOfMonth = weekOfMonth;
            DaysOfWeek  = daysOfWeek & DaysOfTheWeek.Any;
            Interval    = interval;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Get statistiques per weeks of current month.
 /// Each month has four weeks (1, 2, 3 and 4)
 /// </summary>
 /// <param name="week"></param>
 /// <returns></returns>
 public List<Entities.StandardEntity> GetStatistiques(WeekOfMonth week)
 {
     switch (week)
     {
         case WeekOfMonth.FirstWeek:
             return GetStatistiquesForFirstWeek();
         case WeekOfMonth.SecondWeek:
             return GetStatistiquesForSecondWeek();
         case WeekOfMonth.ThirdWeek:
             return GetStatistiquesForThirdWeek();
         case WeekOfMonth.FourthWeek:
             return GetStatistiquesForFourthWeek();
         default:
             return null;
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Gets a specific day in a specific week in a month and day of the week.
        /// </summary>
        /// <param name="month">The month datetime to evaluate.</param>
        /// <param name="weekOfMonth">The week of the month.</param>
        /// <param name="dayOfWeek">The day of the week.</param>
        /// <returns>The new adjusted date.</returns>
        public static DateTime GetDayFromWeekOfMonth(DateTime month, WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek)
        {
            DateTime currentDate = new DateTime(month.Year, month.Month, 1);
            DateTime newDate     = currentDate;
            Int32    currentWeek = (int)weekOfMonth;

            // No need to do any adjustments because the first of the month is the specified date already.
            if (weekOfMonth == WeekOfMonth.First && dayOfWeek == currentDate.DayOfWeek)
            {
                return(currentDate);
            }

            // This date already falls on the specified day of the week.
            // We can simply add the number of weeks from what was given.
            if (currentDate.DayOfWeek == dayOfWeek)
            {
                return(newDate.AddDays(currentWeek * 7));
            }

            if (currentDate.DayOfWeek < dayOfWeek)
            {
                // When the specified day of week is greater than the current date's day of week,
                // we can add the difference to get to the same day.
                newDate = newDate.AddDays(dayOfWeek - newDate.DayOfWeek);
            }
            else
            {
                // Go backwards to get to the first of the week.
                newDate = newDate.AddDays(-(int)newDate.DayOfWeek);

                // When we go outside the current month, make sure to increment a week.
                if (newDate.Month != currentDate.Month)
                {
                    currentWeek++;
                }

                // Add the number of days to get to the specified day of week.
                newDate = newDate.AddDays((int)dayOfWeek);
            }

            // Simply add the number of weeks to get to the specified date.
            newDate = newDate.AddDays(currentWeek * 7);

            // If adding too many days in the current month takes us to next month, go back a week.
            return(newDate.Month != currentDate.Month ? newDate.AddDays(-7) : newDate);
        }
Exemplo n.º 12
0
        private static int GetWeekOrdinalFromEnum(WeekOfMonth week)
        {
            switch (week)
            {
            case WeekOfMonth.First: return(0);

            case WeekOfMonth.Second: return(1);

            case WeekOfMonth.Third: return(2);

            case WeekOfMonth.Fourth: return(3);

            case WeekOfMonth.Last: return(4);

            default: throw new Exception("Error converting Week enum to ordinal!");
            }
        }
Exemplo n.º 13
0
        private DateTime GetNthSpecificDay(int year, int month, int hrs, int min, WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek)
        {
            DateTime date = new DateTime(year, month, 1, hrs, min, 0);

            while (date.DayOfWeek != dayOfWeek)
            {
                date = date.AddDays(1);
            }

            switch (weekOfMonth)
            {
            case WeekOfMonth.first:
                //returns the default value
                break;

            case WeekOfMonth.second:
                date = date.AddDays(7);
                break;

            case WeekOfMonth.third:
                date = date.AddDays(14);
                break;

            case WeekOfMonth.fourth:
                date = date.AddDays(21);
                break;

            case WeekOfMonth.last:
            default:
                var lastday = DateTime.DaysInMonth(year, month);
                date = new DateTime(year, month, lastday, hrs, min, 0);
                while (date.DayOfWeek != dayOfWeek)
                {
                    date = date.AddDays(-1);
                }
                break;
            }

            return(date);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Gets the next date from the year and month of current date.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="weekOfMonth">The week of month.</param>
        /// <param name="dayOfWeek">The day of week.</param>
        /// <returns></returns>
        public static DateTime Get(this DateTime value, WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek)
        {
            var startDate = new DateTime(value.Year, value.Month, 1);
            var date      = startDate;
            var i         = (int)weekOfMonth;

            if (date.DayOfWeek == dayOfWeek && weekOfMonth == WeekOfMonth.First)
            {
                return(date);
            }

            if (date.DayOfWeek == dayOfWeek)
            {
                return(date.AddDays((int)weekOfMonth * 7));
            }

            var e = (int)dayOfWeek;
            var s = (int)date.DayOfWeek;

            if (s < e)
            {
                date = date.AddDays(e - s);
            }
            else
            {
                date = date.AddDays(-s);

                if (date.Month != startDate.Month)
                {
                    i++;
                }

                date = date.AddDays(e);
            }

            date = date.AddDays(i * 7);

            return(date.Month != startDate.Month ? date.AddDays(-7) : date);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Returns the date for a specific occurrence of a given day of the week in a specified month of a the year.
        /// </summary>
        /// <param name="weekOfMonth">The week instance of the month.</param>
        /// <param name="dayOfWeek">The day of the week.</param>
        /// <param name="month">The month of the year.</param>
        /// <param name="year">The year.</param>
        /// <returns>A <see cref="DateTime"/> value.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="year"/> is less than 1 or greater than 9999. Or,
        /// <paramref name="month"/> is less than 1 or greater than 12.
        /// </exception>
        public static DateTime DateFor(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int month, int year)
        {
            Contract.Requires <ArgumentOutOfRangeException>(year >= 1 && year <= 9999, nameof(year));
            Contract.Requires <ArgumentOutOfRangeException>(month >= 1 && month <= 12, nameof(month));

            var firstOfMonth     = new DateTime(year, month, 1);
            var daysInWholeWeeks = ((int)weekOfMonth - 1) * DaysPerWeek;

            var daysFromStartOfMonth = dayOfWeek.DaysFromStartOfTheWeek() - firstOfMonth.DayOfWeek.DaysFromStartOfTheWeek();

            if (daysFromStartOfMonth < 0)
            {
                daysFromStartOfMonth += DaysPerWeek;
            }
            daysFromStartOfMonth += daysInWholeWeeks;
            if (weekOfMonth == WeekOfMonth.Last && daysFromStartOfMonth >= DateTime.DaysInMonth(year, month))
            {
                daysFromStartOfMonth -= DaysPerWeek;
            }

            return(firstOfMonth.AddDays(daysFromStartOfMonth));
        }
Exemplo n.º 16
0
        public DynamicClockTransition(int hoursToChange, WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int month, TimeSpan transitionTimeOfDay)
        {
            if (month < 1 || month > 12)
                throw new ArgumentOutOfRangeException("month", "Invalid month.");

            if (!Enum.IsDefined(typeof(DayOfWeek), dayOfWeek))
                throw new ArgumentException("Invalid day of week.", "dayOfWeek");

            if (!Enum.IsDefined(typeof(WeekOfMonth), weekOfMonth))
                throw new ArgumentException("Invalid week of month.", "weekOfMonth");

            if (transitionTimeOfDay < TimeSpan.Zero || transitionTimeOfDay.Days > 0)
                throw new ArgumentOutOfRangeException("transitionTimeOfDay", "The transition timespan must be a valid time of day.");

            if (hoursToChange != -1 && hoursToChange != 1)
                throw new ArgumentOutOfRangeException("hoursToChange", "The change should be -1 or +1 hours.");

            Month = month;
            DayOfWeek = dayOfWeek;
            WeekOfMonth = weekOfMonth;
            TransitionTimeOfDay = transitionTimeOfDay;
            HoursToChange = hoursToChange;
        }
Exemplo n.º 17
0
        internal static string ToSerializedValue(this WeekOfMonth value)
        {
            switch (value)
            {
            case WeekOfMonth.First:
                return("First");

            case WeekOfMonth.Second:
                return("Second");

            case WeekOfMonth.Third:
                return("Third");

            case WeekOfMonth.Fourth:
                return("Fourth");

            case WeekOfMonth.Last:
                return("Last");

            case WeekOfMonth.Invalid:
                return("Invalid");
            }
            return(null);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Gets the next date.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="year">The year.</param>
        /// <param name="monthOfYear">The month of year.</param>
        /// <param name="weekOfMonth">The week of month.</param>
        /// <param name="dayOfWeek">The day of week.</param>
        /// <returns></returns>
        public static DateTime Get(this DateTime value, int year, MonthOfYear monthOfYear, WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek)
        {
            var dateTime = new DateTime(year, (int)monthOfYear + 1, 1);

            return(dateTime.Get(weekOfMonth, dayOfWeek));
        }
Exemplo n.º 19
0
 /// <summary>
 /// Returns a yearly reoccurring pattern when events occur on one of the specified days of the week and a
 /// specific week of a month of the year.
 /// </summary>
 /// <param name="weekOfMonth">The week of the month when the event occurs.</param>
 /// <param name="daysOfWeek">The accepted days of the week when the event can occur.</param>
 /// <param name="monthOfYear">The month when the event occurs.</param>
 /// <param name="interval">The interval of occurrences in number of years.</param>
 /// <returns>An instance of <see cref="YearlyWeekPattern"/> class.</returns>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="interval"/> is less than 1.</exception>
 public static YearlyWeekPattern Yearly(WeekOfMonth weekOfMonth, DaysOfTheWeek daysOfWeek, MonthOfYear monthOfYear, int interval = 1)
 {
     return(new YearlyWeekPattern(weekOfMonth, daysOfWeek, (int)monthOfYear, interval));
 }
        public void SetCurrentWeek(WeekOfMonth currentWeek)
        {
            First.IsCurrent = false;
            Second.IsCurrent = false;
            Third.IsCurrent = false;
            Fourth.IsCurrent = false;

            switch (currentWeek)
            {
                case WeekOfMonth.FirstWeek:
                    First.IsCurrent = true;
                    break;
                case WeekOfMonth.SecondWeek:
                    Second.IsCurrent = true;
                    break;
                case WeekOfMonth.ThirdWeek:
                    Third.IsCurrent = true;
                    break;
                case WeekOfMonth.FourthWeek:
                    Fourth.IsCurrent = true;
                    break;
                default:
                    break;
            }
        }
Exemplo n.º 21
0
 /// <summary>
 /// Returns a yearly reoccurring pattern when events occur on a specific day of the week and a
 /// specific week of a month of the year.
 /// </summary>
 /// <param name="weekOfMonth">The week of the month when the event occurs.</param>
 /// <param name="dayOfWeek">The day of the week when the event occurs.</param>
 /// <param name="monthOfYear">The month when the event occurs.</param>
 /// <param name="interval">The interval of occurrences in number of years.</param>
 /// <returns>An instance of <see cref="YearlyWeekPattern"/> class.</returns>
 /// <exception cref="ArgumentOutOfRangeException">
 /// <paramref name="monthOfYear"/> is less than 1 or greater than 12. Or,
 /// <paramref name="interval"/> is less than 1.
 /// </exception>
 public static YearlyWeekPattern Yearly(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int monthOfYear, int interval = 1)
 {
     return(new YearlyWeekPattern(weekOfMonth, dayOfWeek.ToDaysOfWeek(), monthOfYear, interval));
 }
Exemplo n.º 22
0
        private DateTime GetNthWeekDay(int year, int month, int hrs, int min, WeekOfMonth weekOfMonth)
        {
            DateTime date = new DateTime(year, month, 1, hrs, min, 0);

            while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
            {
                date = date.AddDays(1);
            }
            switch (weekOfMonth)
            {
            case WeekOfMonth.first:
                break;

            case WeekOfMonth.second:
                date = date.AddDays(1);
                while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    date = date.AddDays(1);
                }
                break;

            case WeekOfMonth.third:
                date = date.AddDays(2);

                while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    date = date.AddDays(2);
                }
                break;

            case WeekOfMonth.fourth:
                switch (date.DayOfWeek)
                {
                case DayOfWeek.Monday:
                case DayOfWeek.Tuesday:
                    date = date.AddDays(3);
                    break;

                case DayOfWeek.Wednesday:
                    date = date.AddDays(5);
                    break;

                case DayOfWeek.Thursday:
                case DayOfWeek.Friday:
                    date = date.AddDays(5);
                    break;
                }
                break;

            case WeekOfMonth.last:
            default:
                var lastday = DateTime.DaysInMonth(year, month);
                date = new DateTime(year, month, lastday, hrs, min, 0);
                while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    date = date.AddDays(-1);
                }
                break;
            }
            return(date);
        }
Exemplo n.º 23
0
 /// <summary>
 /// Returns a monthly reoccurring pattern when events occur on a specific day of the week and a
 /// specific week of the month.
 /// </summary>
 /// <param name="weekOfMonth">The week of the month when the event occurs.</param>
 /// <param name="dayOfWeek">The day of the week when the event occurs.</param>
 /// <param name="interval">The interval of occurrences in number of months.</param>
 /// <returns>An instance of <see cref="MonthlyWeekPattern"/> class.</returns>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="interval"/> is less than 1.</exception>
 public static MonthlyWeekPattern Monthly(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int interval = 1)
 {
     return(new MonthlyWeekPattern(weekOfMonth, dayOfWeek.ToDaysOfWeek(), interval));
 }
Exemplo n.º 24
0
 public void Date_For_Nth_DayOfWeek_Occurrence_In_MonthOfYear_InvalidParam(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int month, int year)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => DateUtils.DateFor(weekOfMonth, dayOfWeek, month, year));
 }
Exemplo n.º 25
0
 public string Date_For_Nth_DayOfWeek_Occurrence_In_MonthOfYear(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, MonthOfYear month, int year)
 {
     return(DateUtils.DateFor(weekOfMonth, dayOfWeek, month, year).ToString("yyyy-MM-dd"));
 }
Exemplo n.º 26
0
        public IEnumerable <DateTime> Recurrences(WeekOfMonth weekOfMonth, DaysOfTheWeek daysOfWeek, MonthOfYear monthOfYear, int interval, DateTime start, int count)
        {
            var pattern = Recurrent.Yearly(weekOfMonth, daysOfWeek, monthOfYear, interval);

            return(pattern.GetRecurrencesStartingAt(start).Take(count).ToArray());
        }
Exemplo n.º 27
0
 /// <summary>
 /// Returns the date for a specific occurrence of a given day of the week in a specific month of the year.
 /// </summary>
 /// <param name="weekOfMonth">The week of the month.</param>
 /// <param name="dayOfWeek">The day of the week.</param>
 /// <param name="monthOfYear">The month of the year.</param>
 /// <param name="year">The year.</param>
 /// <returns>A <see cref="DateTime"/> value.</returns>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="year"/> is less than 1 or greater than 9999.</exception>
 public static DateTime DateFor(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, MonthOfYear monthOfYear, int year)
 {
     return(DateFor(weekOfMonth, dayOfWeek, (int)monthOfYear, year));
 }