System.DateTimeParse.DoStrictParse C# (CSharp) Méthode

DoStrictParse() private static méthode

private static DoStrictParse ( String s, String formatParam, DateTimeStyles styles, DateTimeFormatInfo dtfi, DateTimeResult &result ) : bool
s String
formatParam String
styles DateTimeStyles
dtfi DateTimeFormatInfo
result DateTimeResult
Résultat bool
        private static bool DoStrictParse(
            String s,
            String formatParam,
            DateTimeStyles styles,
            DateTimeFormatInfo dtfi,
            ref DateTimeResult result) {

            bool bTimeOnly = false;
            ParsingInfo parseInfo = new ParsingInfo();
            parseInfo.Init();

            parseInfo.calendar = dtfi.Calendar;
            parseInfo.fAllowInnerWhite = ((styles & DateTimeStyles.AllowInnerWhite) != 0);
            parseInfo.fAllowTrailingWhite = ((styles & DateTimeStyles.AllowTrailingWhite) != 0);

            if (formatParam.Length == 1) {
                formatParam = ExpandPredefinedFormat(formatParam, ref dtfi, ref parseInfo, ref result);
            }
            result.calendar = parseInfo.calendar;

            if (parseInfo.calendar.ID == Calendar.CAL_HEBREW) {
                parseInfo.parseNumberDelegate = m_hebrewNumberParser;
                parseInfo.fCustomNumberParser = true;
            }

            // Reset these values to negative one so that we could throw exception
            // if we have parsed every item twice.
            result.Hour = result.Minute = result.Second = -1;

            __DTString format = new __DTString(formatParam, dtfi, false);
            __DTString str = new __DTString(s, dtfi, false);

            if (parseInfo.fAllowTrailingWhite) {
                // Trim trailing spaces if AllowTrailingWhite.
                format.TrimTail();
                format.RemoveTrailingInQuoteSpaces();
                str.TrimTail();
            }

            if ((styles & DateTimeStyles.AllowLeadingWhite) != 0) {
                format.SkipWhiteSpaces();
                format.RemoveLeadingInQuoteSpaces();
                str.SkipWhiteSpaces();
            }

            //
            // Scan every character in format and match the pattern in str.
            //
            while (format.GetNext()) {
                // We trim inner spaces here, so that we will not eat trailing spaces when
                // AllowTrailingWhite is not used.
                if (parseInfo.fAllowInnerWhite) {
                    str.SkipWhiteSpaces();
                }
                if (!ParseByFormat(ref str, ref format, ref parseInfo, dtfi, ref result)) {
                   return (false);
                }
            }
            if (str.Index < str.Value.Length - 1) {
                // There are still remaining character in str.
                result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
                return false;
            }

            if (parseInfo.fUseTwoDigitYear && ((dtfi.FormatFlags & DateTimeFormatFlags.UseHebrewRule) == 0)) {
                // A two digit year value is expected. Check if the parsed year value is valid.
                if (result.Year >= 100) {
                    result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
                    return false;
                }
                result.Year = parseInfo.calendar.ToFourDigitYear(result.Year);
            }

            if (parseInfo.fUseHour12) {
                if (parseInfo.timeMark == TM.NotSet) {
                    // hh is used, but no AM/PM designator is specified.
                    // Assume the time is AM.
                    // Don't throw exceptions in here becasue it is very confusing for the caller.
                    // I always got confused myself when I use "hh:mm:ss" to parse a time string,
                    // and ParseExact() throws on me (because I didn't use the 24-hour clock 'HH').
                    parseInfo.timeMark = TM.AM;
                }
                if (result.Hour > 12) {
                    // AM/PM is used, but the value for HH is too big.
                    result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
                    return false;
                }
                if (parseInfo.timeMark == TM.AM) {
                    if (result.Hour == 12) {
                        result.Hour = 0;
                    }
                } else {
                    result.Hour = (result.Hour == 12) ? 12 : result.Hour + 12;
                }
            }

            // Check if the parased string only contains hour/minute/second values.
            bTimeOnly = (result.Year == -1 && result.Month == -1 && result.Day == -1);
            CheckDefaultDateTime(ref result, ref parseInfo.calendar, styles);

            if (!bTimeOnly && dtfi.HasYearMonthAdjustment) {
                if (!dtfi.YearMonthAdjustment(ref result.Year, ref result.Month, ((result.flags & ParseFlags.ParsedMonthName) != 0))) {
                    result.SetFailure(ParseFailureKind.FormatBadDateTimeCalendar, "Format_BadDateTimeCalendar", null);
                    return false;
                }
            }
            if (!parseInfo.calendar.TryToDateTime(result.Year, result.Month, result.Day,
                    result.Hour, result.Minute, result.Second, 0, result.era, out result.parsedDate)) {
                result.SetFailure(ParseFailureKind.FormatBadDateTimeCalendar, "Format_BadDateTimeCalendar", null);
                return false;
            }
            if (result.fraction > 0) {
                result.parsedDate = result.parsedDate.AddTicks((long)Math.Round(result.fraction * Calendar.TicksPerSecond));
            }

            //
            // We have to check day of week before we adjust to the time zone.
            // It is because the value of day of week may change after adjusting
            // to the time zone.
            //
            if (parseInfo.dayOfWeek != -1) {
                //
                // Check if day of week is correct.
                //
                if (parseInfo.dayOfWeek != (int)parseInfo.calendar.GetDayOfWeek(result.parsedDate)) {
                    result.SetFailure(ParseFailureKind.Format, "Format_BadDayOfWeek", null);
                    return false;
                }
            }


            if (!DetermineTimeZoneAdjustments(ref result, styles, bTimeOnly)) {
                return false;
            }
            return true;
        }