Beispiel #1
0
        public static bool CanParse(string s)
        {
            bool returnValue = false;

            NthParser parsedValue = null;

            returnValue = NthParser.TryParse(s, out parsedValue);

            return(returnValue);
        }
Beispiel #2
0
        public static NthParser Parse(string s)
        {
            NthParser returnValue = NthParser.Default;

            if (!NthParser.TryParse(s, out returnValue))
            {
                throw new FormatException();
            }

            return(returnValue);
        }
Beispiel #3
0
        private DateTime _GetDate(int year, int month, Division division, int n)
        {
            DateTime returnValue = DateTime.MinValue;

            //
            // Get the first day of the month and then adjust the date
            //
            DateTime firstDayOfMonth = new DateTime(year, month, 1);
            int      dayOfWeek       = (int)division;

            int dayDifference = 0;

            if ((int)firstDayOfMonth.DayOfWeek > dayOfWeek)
            {
                dayDifference = 7 - ((int)firstDayOfMonth.DayOfWeek - dayOfWeek);
                returnValue   = firstDayOfMonth.Add(TimeSpan.FromDays(dayDifference));
            }
            else
            {
                dayDifference = dayOfWeek - (int)firstDayOfMonth.DayOfWeek;
                returnValue   = firstDayOfMonth.Add(TimeSpan.FromDays(dayDifference));
            }

            //
            // Get the maximum number of DayOfWeek's in the month
            //
            int maxDayOfWeek = NthParser.DayOfWeekCount((DayOfWeek)dayOfWeek, month, year);
            int daysToAdd    = 0;

            if (n > maxDayOfWeek)
            {
                daysToAdd = (maxDayOfWeek - 1) * 7;
            }
            else
            {
                daysToAdd = (n - 1) * 7;
            }

            //
            // Add the days to get the Nth occurrence
            //
            returnValue = returnValue.AddDays(daysToAdd);

            return(returnValue);
        }
Beispiel #4
0
        public static bool TryParse(string s, out NthParser result)
        {
            bool returnValue = false;

            result = NthParser.Default;


            //
            // Join the Ordinal enumerator names with the | character into a string
            // and build the Ordinal pattern
            //
            string ordinalSection = String.Join("|", Enum.GetNames(typeof(Ordinal))).Replace("_", " ");
            string ordinalPattern = string.Format(@"(?<Ordinal>({0})|([0-9]{{1,3}})(st|nd|rd|th))", ordinalSection);

            //
            // Join the Division enumerator names with the | character into a string
            // and build the Division pattern
            //
            string section2        = String.Join("|", Enum.GetNames(typeof(Division))).Replace("_", " ");
            string divisionPattern = string.Format("(?<Division>({0}))", section2);

            //
            // Join the Period enumerator names with the | character into a string
            // and build the Period pattern
            //
            string section3      = String.Join("|", Enum.GetNames(typeof(Period))).Replace("_", " ");
            string periodPattern = string.Format("(?<Period>({0}))", section3);

            string          pattern = string.Format(@"({0}\s{1}\s(of|in)\s{2})", ordinalPattern, divisionPattern, periodPattern);
            Regex           regex   = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matches = regex.Matches(s);

            if (matches.Count > 0)
            {
                GroupCollection groups = matches[0].Groups;

                if (groups.Count >= 3)
                {
                    //
                    // Used to convert strings to title case
                    //
                    CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
                    TextInfo    textInfo    = cultureInfo.TextInfo;

                    //
                    // Get the Ordinal
                    //
                    string  interval    = textInfo.ToTitleCase(groups["Ordinal"].Value).Replace(" ", "_");
                    Ordinal parsedValue = Ordinal.Other;

                    if (Enum.TryParse <Ordinal>(interval, out parsedValue))
                    {
                        result.Ordinal = parsedValue;
                    }
                    else
                    {
                        //
                        // Need to parse the integer. The right two characters will
                        // always be st, nd, rd or th
                        //
                        string intText = interval.Substring(0, interval.Length - 2);
                        result.N = int.Parse(intText);
                    }

                    //
                    // Get the Division
                    //
                    string division = textInfo.ToTitleCase(groups["Division"].Value).Replace(" ", "_");
                    result.Division = (Division)Enum.Parse(typeof(Division), division, true);

                    //
                    // Get the Period
                    //
                    string period = textInfo.ToTitleCase(groups["Period"].Value).Replace(" ", "_");
                    result.Period = (Period)Enum.Parse(typeof(Period), period, true);

                    returnValue = true;
                }
            }

            return(returnValue);
        }