Ejemplo n.º 1
0
        //
        // Check the word at the current index to see if it matches a month name.
        // Return -1 if a match is not found. Otherwise, a value from 1 to 12 is returned.
        //
        private static int GetMonthNumber(__DTString str, DateTimeFormatInfo dtfi)
        {
            //
            // Check the month name specified in dtfi.
            //
            int i;

            int monthInYear = (dtfi.GetMonthName(13).Length == 0 ? 12 : 13);
            int maxLen = 0;
            int result = -1;
            int index;
            String word = str.PeekCurrentWord();

            //
            // We have to match the month name with the longest length, 
            // since there are cultures which have more than one month names
            // with the same prefix.
            //
            for (i = 1; i <= monthInYear; i++) {
                String monthName = dtfi.GetMonthName(i);

                if ((index = str.CompareInfo.IndexOf(
                    word, monthName, CompareOptions.IgnoreCase)) >= 0) {
                    // This condition allows us to parse the month name for:
                    // 1. the general cases like "yyyy MMMM dd".
                    // 2. prefix in the month name like: "dd '\x05d1'MMMM yyyy" (Hebrew - Israel)
                    result = i;
                    maxLen = index + monthName.Length;
                } else if (str.StartsWith(monthName, true)) {
                    // The condition allows us to get the month name for cultures
                    // which has spaces in their month names.
                    // E.g. 
                    if (monthName.Length > maxLen) {
                        result = i;
                        maxLen = monthName.Length;
                    }
                }
                }
            if (result > -1) {
                str.Index += maxLen;
                return (result);
            }
            for (i = 1; i <= monthInYear; i++)
            {
                if (MatchWord(str, dtfi.GetAbbreviatedMonthName(i), false))
                {
                    return (i);
                }
            }

            //
            // Check the month name in the invariant culture.
            //
            for (i = 1; i <= 12; i++)
            {
                if (MatchWord(str, invariantInfo.GetMonthName(i), false))
                {
                    return (i);
                }
            }

            for (i = 1; i <= 12; i++)
            {
                if (MatchWord(str, invariantInfo.GetAbbreviatedMonthName(i), false))
                {
                    return (i);
                }
            }

            return (-1);
        }