Ejemplo n.º 1
0
        /// <summary>
        /// Parses enumerated days of week for a week and returns a list containing those DayOfWeeks.
        /// </summary>
        /// <returns>
        /// a list containing those DayOfWeeks.
        /// </returns>
        /// <exception cref="RuleInvalidDataException">
        /// If the DateValue is not in valid format.
        /// </exception>
        private IList <DayOfWeek> GetWeekDaysForDayOfWeek()
        {
            IList <DayOfWeek> ret = new List <DayOfWeek>();

            if (DateValue.Equals(String.Empty))
            {
                return(ret);
            }

            try
            {
                //A value like "Monday-Wednesday;Friday;Sunday" can be parsed here.
                //First split using ';'
                string[] firstSplit = DateValue.Split(';');
                for (int i = 0; i < firstSplit.Length; i++)
                {
                    //Now split using '-'
                    string[] secondSplit = firstSplit[i].Split('-');

                    //The parsed value must be a defined enum value
                    if (!Enum.IsDefined(typeof(DayOfWeek), secondSplit[0]))
                    {
                        throw new RuleInvalidDataException(secondSplit[0] + " is an invalid DayOfWeek value.");
                    }

                    //We have a DayOfWeek range
                    if (secondSplit.Length == 2)
                    {
                        //The parsed value must be a defined enum value
                        if (!Enum.IsDefined(typeof(DayOfWeek), secondSplit[1]))
                        {
                            throw new RuleInvalidDataException(secondSplit[1] + " is an invalid DayOfWeek value.");
                        }

                        DayOfWeek rangeStart = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), secondSplit[0]);
                        DayOfWeek rangeEnd   = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), secondSplit[1]);
                        for (DayOfWeek j = rangeStart; j <= rangeEnd; j++)
                        {
                            ret.Add(j);
                        }
                    }
                    //We have a normal single date
                    else
                    {
                        ret.Add((DayOfWeek)Enum.Parse(typeof(DayOfWeek), secondSplit[0]));
                    }
                }
            }
            catch (RuleInvalidDataException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new RuleInvalidDataException(DateValue + " is an invalid date value.", e);
            }

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates dates in the given format for the DayMonth DateType with the start and end dates.
        /// </summary>
        /// <param name="inputFormat">The inputDate format</param>
        /// <param name="displayFormat">The display date format</param>
        /// <param name="start">The start date from which to start searching</param>
        /// <param name="end">The end date at which to stop searching</param>
        /// <returns>An array of date strings after applying the rule from the start date to end date.
        /// The format of strings in this array is the display date format.
        /// </returns>
        /// <exception cref="RuleInvalidDataException">If the dates could not be generated due to either incorrect
        /// inputFormat/displayFormat or invalid DateValue/TimeValue</exception>
        private string[] GenerateDatesForDayMonthDateType(string inputFormat, string displayFormat,
                                                          DateTime start, DateTime end)
        {
            //Parse day and month
            int day, month;

            try
            {
                string[] dayAndMonth = DateValue.Split('-');
                month = int.Parse(dayAndMonth[0]);
                day   = int.Parse(dayAndMonth[1]);
            }
            catch (Exception e)
            {
                throw new RuleInvalidDataException(DateValue + " must be in the format [month]-[day].", e);
            }

            //Get the time parts
            IList <KeyValuePair <int, int> > hourMinPairs = ParseTimeValue();

            //Return the dates
            IList <DateTime> retList = new List <DateTime>();

            for (int year = start.Year; year <= end.Year; year++)
            {
                if (year % YearDivisor == 0)
                {
                    //One date each to be returned for each time value specified in TimeValue
                    foreach (KeyValuePair <int, int> hourMinPair in hourMinPairs)
                    {
                        if (DateTime.DaysInMonth(year, month) >= day)
                        {
                            //Create the date
                            DateTime dateFormed = new DateTime(year, month, day, hourMinPair.Key, hourMinPair.Value, 0);

                            //Add if inclusive of ranges
                            if (dateFormed >= start && dateFormed <= end)
                            {
                                retList.Add(dateFormed);
                            }
                        }
                    }
                }
            }

            return(FormatListAndReturnArray(retList, displayFormat));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses enumerated day numbers for a month returns a list containing those day numbers.
        /// </summary>
        /// <returns>
        /// a list containing those day numbers.
        /// </returns>
        /// <exception cref="RuleInvalidDataException">
        /// If the DateValue is not in valid format.
        /// </exception>
        private IList <int> GetDayNumbersForDaysOfMonth()
        {
            List <int> ret = new List <int>();

            if (DateValue.Equals(String.Empty))
            {
                return(ret);
            }

            try
            {
                //A value like "1-5;7;9-12;15" can be parsed here.
                //First split using ';'
                string[] firstSplit = DateValue.Split(';');
                for (int i = 0; i < firstSplit.Length; i++)
                {
                    //Now split using '-'
                    string[] secondSplit = firstSplit[i].Split('-');

                    //We have a day range
                    if (secondSplit.Length == 2)
                    {
                        int rangeStart = int.Parse(secondSplit[0]);
                        int rangeEnd   = int.Parse(secondSplit[1]);
                        for (int j = rangeStart; j <= rangeEnd; j++)
                        {
                            ret.Add(j);
                        }
                    }
                    //We have a normal single date
                    else
                    {
                        ret.Add(int.Parse(secondSplit[0]));
                    }
                }
            }
            catch (Exception e)
            {
                throw new RuleInvalidDataException(DateValue + " is an invalid date value.", e);
            }

            //Sort the day numbers before returning
            ret.Sort();

            return(ret);
        }