コード例 #1
0
        // Currently, custom format and normal date time format except 'o' or 'O' 's' are NOT supported.
        public static TimestampParseResult TryParseExact(string input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out Timestamp result)
        {
            if (format != "o" && format != "O" && format != "s")
            {
                result = default(Timestamp);
                return(TimestampParseResult.UnsupportedFormat);
            }

            var numberFormat = NumberFormatInfo.GetInstance(formatProvider);

            var position = 0;

            if (!ParseWhitespace(input, ref position, (styles & DateTimeStyles.AllowLeadingWhite) != 0, /* isTrailing */ false))
            {
                result = default(Timestamp);
                return(TimestampParseResult.LeadingWhitespaceNotAllowed);
            }

            long year;

            if (!ParseYear(input, ref position, numberFormat, out year))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidYear);
            }

            if (!ParseDelimiter(input, ref position, DateDelimiter))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidYearMonthDeilimiter);
            }

            var isLeapYear = Timestamp.IsLeapYearInternal(year);

            int month;

            if (!ParseDigitRange(input, 2, ref position, 1, 12, out month))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidMonth);
            }

            if (!ParseDelimiter(input, ref position, DateDelimiter))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidMonthDayDelimiter);
            }

            int day;

            if (!ParseDay(input, ref position, month, isLeapYear, out day))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidDay);
            }

            if (!ParseDelimiter(input, ref position, DateTimeDelimiter))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidDateTimeDelimiter);
            }

            int hour;

            if (!ParseDigitRange(input, 2, ref position, 0, 23, out hour))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidHour);
            }

            if (!ParseDelimiter(input, ref position, TimeDelimiter))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidHourMinuteDelimiter);
            }

            int minute;

            if (!ParseDigitRange(input, 2, ref position, 0, 59, out minute))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidMinute);
            }

            if (!ParseDelimiter(input, ref position, TimeDelimiter))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidMinuteSecondDelimiter);
            }

            int second;

            if (!ParseDigitRange(input, 2, ref position, 0, 59, out second))
            {
                result = default(Timestamp);
                return(TimestampParseResult.InvalidSecond);
            }

            var nanosecond = 0;

            if (format != "s")
            {
                // "o" or "O"
                if (!ParseDelimiter(input, ref position, SubsecondDelimiter))
                {
                    result = default(Timestamp);
                    return(TimestampParseResult.InvalidSubsecondDelimiter);
                }

                if (!ParseDigitRange(input, 9, ref position, 0, 999999999, out nanosecond))
                {
                    result = default(Timestamp);
                    return(TimestampParseResult.InvalidNanoSecond);
                }
            }

            if (!ParseDelimiter(input, ref position, UtcSign))
            {
                result = default(Timestamp);
                return(TimestampParseResult.MissingUtcSign);
            }

            if (!ParseWhitespace(input, ref position, (styles & DateTimeStyles.AllowTrailingWhite) != 0, /* isTrailing */ true))
            {
                result = default(Timestamp);
                return(TimestampParseResult.TrailingWhitespaceNotAllowed);
            }

            if (position != input.Length)
            {
                result = default(Timestamp);
                return(TimestampParseResult.ExtraCharactors);
            }

            var components = new Timestamp.Value();

            components.Year        = year;
            components.Month       = month;
            components.Day         = day;
            components.Hour        = hour;
            components.Minute      = minute;
            components.Second      = second;
            components.Nanoseconds = unchecked (( uint )nanosecond);

            try
            {
                result = Timestamp.FromComponents(ref components, isLeapYear);
            }
            catch (OverflowException)
            {
                result = default(Timestamp);
                return(TimestampParseResult.YearOutOfRange);
            }

            return(TimestampParseResult.Success);
        }