public static TimeSpan? TryParseExactNullable(string value, IEnumerable<string> formats, IFormatProvider provider, TimeSpanStyles styles)
        {
            TimeSpan result;
            if (!TimeSpan.TryParseExact(value, formats.ToArray(), provider, styles, out result))
                return null;

            return result;
        }
        public static TimeSpan? TryParseExactNullable(string value, string format, IFormatProvider provider, TimeSpanStyles styles)
        {
            TimeSpan result;
            if (!TimeSpan.TryParseExact(value, format, provider, styles, out result))
                return null;

            return result;
        }
 internal static TimeSpan ParseExactMultiple(string input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles)
 {
     TimeSpanResult result = new TimeSpanResult();
     result.Init(TimeSpanThrowStyle.All);
     if (!TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref result))
     {
         throw result.GetTimeSpanParseException();
     }
     return result.parsedTimeSpan;
 }
Exemplo n.º 4
0
 public TimeSpan ParseExactCulture(IEnumerable <string> formats, TimeSpanStyles styles, TimeSpan defaultValue = default(TimeSpan))
 {
     return(ParseExact(formats, CultureInfo.CurrentCulture, styles, defaultValue));
 }
 private static bool TryParseExactTimeSpan(string input, string format, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result)
 {
     if (input == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "input");
         return false;
     }
     if (format == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "format");
         return false;
     }
     if (format.Length == 0)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
         return false;
     }
     if (format.Length != 1)
     {
         return TryParseByFormat(input, format, styles, ref result);
     }
     TimeSpanStandardStyles none = TimeSpanStandardStyles.None;
     if (((format[0] == 'c') || (format[0] == 't')) || (format[0] == 'T'))
     {
         return TryParseTimeSpanConstant(input, ref result);
     }
     if (format[0] == 'g')
     {
         none = TimeSpanStandardStyles.Localized;
     }
     else if (format[0] == 'G')
     {
         none = TimeSpanStandardStyles.RequireFull | TimeSpanStandardStyles.Localized;
     }
     else
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
         return false;
     }
     return TryParseTimeSpan(input, none, formatProvider, ref result);
 }
Exemplo n.º 6
0
 public static Boolean TryParseTimeSpanExactMultiple(String value, String[] formats, IFormatProvider provider, TimeSpanStyles styles, out TimeSpan result)
 {
     return FormatProvider.TimeSpanParse.TryParseExactMultiple(value, formats, provider, styles, out result);
 }
Exemplo n.º 7
0
        //
        //  TryParseExactMultipleTimeSpan
        //
        //  Actions: Common private ParseExactMultiple method called by both ParseExactMultiple and TryParseExactMultiple
        // 
        private static Boolean TryParseExactMultipleTimeSpan(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result) {
            if (input == null) {
                result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input));
                return false;
            }
            if (formats == null) {
                result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(formats));
                return false;
            }

            if (input.Length == 0) {
                result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
                return false;
            }

            if (formats.Length == 0) {
                result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
                return false;
            }

            //
            // Do a loop through the provided formats and see if we can parse succesfully in
            // one of the formats.
            //
            for (int i = 0; i < formats.Length; i++) {
                if (formats[i] == null || formats[i].Length == 0) {
                    result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
                    return false;
                }

                // Create a new non-throwing result each time to ensure the runs are independent.
                TimeSpanResult innerResult = new TimeSpanResult();
                innerResult.Init(TimeSpanThrowStyle.None);

                if(TryParseExactTimeSpan(input, formats[i], formatProvider, styles, ref innerResult)) {
                    result.parsedTimeSpan = innerResult.parsedTimeSpan;
                    return true;
                }
            }

            result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
            return (false);
        }
 public TimeSpan? ParseExactInvariant(IEnumerable<string> formats, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return ParseExact(formats, CultureInfo.InvariantCulture, styles);
 }
 public TimeSpan ParseExact(IEnumerable<string> formats, IFormatProvider provider, TimeSpanStyles styles, TimeSpan defaultValue = default(TimeSpan))
 {
     return TimeSpanStringParser.TryParseExactDefault(_input, formats, provider, styles, defaultValue);
 }
Exemplo n.º 10
0
 public static bool TryParseExact(string input, string[] formats, IFormatProvider provider, TimeSpanStyles styles, out DateTimeSpan result) =>
 DateTimeSpanParse.TryParseExactMultiple(input, formats, provider, styles, out result);
        /// <summary>
        /// The <see cref="TimeSpanStyles"/> to use when type converting.
        /// This is used when doing <see cref="TimeSpan"/> converting.
        /// </summary>
        /// <param name="timeSpanStyles">The time span styles.</param>
        public virtual MemberMap TimespanStyles(TimeSpanStyles timeSpanStyles)
        {
            memberMap.Data.TypeConverterOptions.TimeSpanStyle = timeSpanStyles;

            return(memberMap);
        }
Exemplo n.º 12
0
        public static bool TryParseExact([NotNullWhen(true)] string?input, [NotNullWhen(true)] string?format, IFormatProvider?formatProvider, TimeSpanStyles styles, out TimeSpan result)
        {
            ValidateStyles(styles, nameof(styles));
            if (input == null || format == null)
            {
                result = default;
                return(false);
            }

            return(TimeSpanParse.TryParseExact(input, format, formatProvider, styles, out result));
        }
Exemplo n.º 13
0
        /// <summary>
        /// 将字符串分析为Timespan
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TimeSpan ToTimeSpanExact(this string value, string[] format, IFormatProvider formatProvider = null, TimeSpanStyles styles = TimeSpanStyles.None)
        {
            TimeSpan ts;

            if (TimeSpan.TryParseExact(value, format, formatProvider, styles, out ts))
            {
                return(ts);
            }

            return(TimeSpan.Zero);
        }
Exemplo n.º 14
0
 public static TimeSpan ParseExact(ReadOnlySpan <char> input, ReadOnlySpan <char> format, IFormatProvider?formatProvider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     ValidateStyles(styles, nameof(styles));
     return(TimeSpanParse.ParseExact(input, format, formatProvider, styles));
 }
Exemplo n.º 15
0
 public static TimeSpan ParseExact(string input, string format, IFormatProvider?formatProvider, TimeSpanStyles styles)
 {
     ValidateStyles(styles, nameof(styles));
     if (input == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
     }
     if (format == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
     }
     return(TimeSpanParse.ParseExact(input, format, formatProvider, styles));
 }
Exemplo n.º 16
0
 public static bool TryParseExact(ReadOnlySpan <char> input, string?[]?formats, IFormatProvider?formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     ValidateStyles(styles, nameof(styles));
     return(TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result));
 }
Exemplo n.º 17
0
 public static bool TryParseExact(string?input, string?[]?formats, IFormatProvider?formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     ValidateStyles(styles, nameof(styles));
     if (input == null)
     {
         result = default;
         return(false);
     }
     return(TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result));
 }
Exemplo n.º 18
0
 public TimeSpan ParseExact(string format, IFormatProvider provider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return(TimeSpan.ParseExact(_input, format, provider, styles));
 }
 public static TimeSpan TryParseExactDefault(string value, IEnumerable<string> formats, IFormatProvider provider, TimeSpanStyles styles, TimeSpan defaultValue)
 {
     TimeSpan result;
     return !TimeSpan.TryParseExact(value, formats.ToArray(), provider, styles, out result) ? defaultValue : result;
 }
Exemplo n.º 20
0
 public TimeSpan ParseExact(IEnumerable <string> formats, IFormatProvider provider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return(TimeSpan.ParseExact(_input, formats.ToArray(), provider, styles));
 }
 public TimeSpan? ParseExactCulture(string format, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return ParseExact(format, CultureInfo.CurrentCulture, styles);
 }
Exemplo n.º 22
0
 public TimeSpan ParseExactCulture(string format, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return(ParseExact(format, CultureInfo.CurrentCulture, styles));
 }
Exemplo n.º 23
0
	void TryParseExactHelper (string input, string [] formats, bool error, string expected, IFormatProvider formatProvider = null,
			TimeSpanStyles styles = TimeSpanStyles.None)
	{
		TimeSpan result;
		bool success;

		success = TimeSpan.TryParseExact (input, formats, formatProvider, styles, out result);
		Assert.AreEqual (!error, success);
		if (!error)
			Assert.AreEqual (expected, result.ToString ());
	}
Exemplo n.º 24
0
 public TimeSpan ParseExactInvariant(string format, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return(ParseExact(format, CultureInfo.InvariantCulture, styles));
 }
Exemplo n.º 25
0
 public static TimeSpan ParseTimeSpanExact(String value, String format, IFormatProvider provider, TimeSpanStyles styles)
 {
     return FormatProvider.TimeSpanParse.ParseExact(value, format, provider, styles);
 }
Exemplo n.º 26
0
 public TimeSpan ParseExactInvariant(IEnumerable <string> formats, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return(ParseExact(formats, CultureInfo.InvariantCulture, styles));
 }
Exemplo n.º 27
0
        //
        //  TryParseExactTimeSpan
        //
        //  Actions: Common private ParseExact method called by both ParseExact and TryParseExact
        // 
        private static Boolean TryParseExactTimeSpan(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result) {
            if (input == null) {
                result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input));
                return false;
            }
            if (format == null) {
                result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(format));
                return false;
            }
            if (format.Length == 0) {
                result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
                return false;
            }

            if (format.Length == 1) {
                TimeSpanStandardStyles style = TimeSpanStandardStyles.None;

                if (format[0] == 'c' || format[0] == 't' || format[0] == 'T') {
                    // fast path for legacy style TimeSpan formats.
                    return TryParseTimeSpanConstant(input, ref result);
                }
                else if (format[0] == 'g') {
                    style = TimeSpanStandardStyles.Localized;
                }
                else if (format[0] == 'G') {
                    style = TimeSpanStandardStyles.Localized | TimeSpanStandardStyles.RequireFull;
                }
                else {
                    result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
                    return false;
                }
                return TryParseTimeSpan(input, style, formatProvider, ref result);
            }
           
            return TryParseByFormat(input, format, styles, ref result);
        }
Exemplo n.º 28
0
 public static TimeSpan ParseExact(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles)
 {
     ValidateStyles(styles, nameof(styles));
     return(FormatProvider.ParseTimeSpanExactMultiple(input, formats, formatProvider, styles));
 }
Exemplo n.º 29
0
        internal static Boolean TryParseExactMultiple(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result) {
            TimeSpanResult parseResult = new TimeSpanResult();
            parseResult.Init(TimeSpanThrowStyle.None);

            if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) {
                result = parseResult.parsedTimeSpan;
                return true;
            }
            else {
                result = default(TimeSpan);
                return false;
            }
        }
 public TimeSpan ParseExact(string format, IFormatProvider provider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return TimeSpan.ParseExact(_input, format, provider, styles);
 }
 private static bool TryParseExactMultipleTimeSpan(string input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result)
 {
     if (input == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "input");
         return false;
     }
     if (formats == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "formats");
         return false;
     }
     if (input.Length == 0)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
         return false;
     }
     if (formats.Length == 0)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
         return false;
     }
     for (int i = 0; i < formats.Length; i++)
     {
         if ((formats[i] == null) || (formats[i].Length == 0))
         {
             result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
             return false;
         }
         TimeSpanResult result2 = new TimeSpanResult();
         result2.Init(TimeSpanThrowStyle.None);
         if (TryParseExactTimeSpan(input, formats[i], formatProvider, styles, ref result2))
         {
             result.parsedTimeSpan = result2.parsedTimeSpan;
             return true;
         }
     }
     result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
     return false;
 }
 public TimeSpan ParseExactCulture(IEnumerable<string> formats, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return ParseExact(formats, CultureInfo.CurrentCulture, styles);
 }
Exemplo n.º 33
0
 public static Boolean TryParseExact(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     ValidateStyles(styles, nameof(styles));
     return(FormatProvider.TryParseTimeSpanExactMultiple(input, formats, formatProvider, styles, out result));
 }
Exemplo n.º 34
0
 public NullableTimeSpanConverter(string format, IFormatProvider formatProvider, TimeSpanStyles timeSpanStyles)
     : base(new TimeSpanConverter(format, formatProvider, timeSpanStyles))
 {
 }
 public TimeSpan ParseExact(IEnumerable<string> formats, IFormatProvider provider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return TimeSpan.ParseExact(_input, formats.ToArray(), provider, styles);
 }
Exemplo n.º 36
0
 public static TimeSpan ParseTimeSpanExact(String value, String format, IFormatProvider provider, TimeSpanStyles styles)
 {
     return(FormatProvider.TimeSpanParse.ParseExact(value, format, provider, styles));
 }
 public static TimeSpan TryParseExactDefault(string value, string format, IFormatProvider provider, TimeSpanStyles styles, TimeSpan defaultValue)
 {
     TimeSpan result;
     return !TimeSpan.TryParseExact(value, format, provider, styles, out result) ? defaultValue : result;
 }
Exemplo n.º 38
0
 public static TimeSpan ParseTimeSpanExactMultiple(String value, String[] formats, IFormatProvider provider, TimeSpanStyles styles)
 {
     return(FormatProvider.TimeSpanParse.ParseExactMultiple(value, formats, provider, styles));
 }
Exemplo n.º 39
0
 public static Boolean TryParseTimeSpanExactMultiple(String value, String[] formats, IFormatProvider provider, TimeSpanStyles styles, out TimeSpan result)
 {
     return(FormatProvider.TimeSpanParse.TryParseExactMultiple(value, formats, provider, styles, out result));
 }
Exemplo n.º 40
0
 public static TimeSpan ParseExact(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles)
 {
     TimeSpanParse.ValidateStyles(styles, "styles");
     return(TimeSpanParse.ParseExactMultiple(input, formats, formatProvider, styles));
 }
 public TimeSpan? ParseExact(IEnumerable<string> formats, IFormatProvider provider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return TimeSpanStringParser.TryParseExactNullable(_input, formats, provider, styles);
 }
Exemplo n.º 42
0
 public static Boolean TryParseExact(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     TimeSpanParse.ValidateStyles(styles, "styles");
     return(TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result));
 }
 public TimeSpan? ParseExactInvariant(string format, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     return ParseExact(format, CultureInfo.InvariantCulture, styles);
 }
 /// <summary>
 /// Sets the time span styles that are permitted when parsing the option values to <see cref="TimeSpan" /> values.
 /// The default is <see cref="TimeSpanStyles.None" />.
 /// </summary>
 /// <param name="timeSpanStyles">The time span styles that are permitted when parsing the option values to <see cref="TimeSpan" /> values.</param>
 /// <returns>A reference to this instance for further configuration.</returns>
 public TimeSpanListOptionSetup <TCommandOptions> Styles(TimeSpanStyles timeSpanStyles)
 {
     this.optionParser.TimeSpanStyles = timeSpanStyles;
     return(this);
 }
Exemplo n.º 45
0
	void ParseExactHelper (string input, string [] formats, bool format_error, bool overflow_error, string expected, 
        IFormatProvider formatProvider = null, TimeSpanStyles timeSpanStyles = TimeSpanStyles.None)
	{
		bool overflow_exc = false;
		bool format_exc = false;
		TimeSpan result = TimeSpan.Zero;

		try {
			result = TimeSpan.ParseExact (input, formats, formatProvider, timeSpanStyles);
		} catch (OverflowException) {
			overflow_exc = true;
		} catch (FormatException) {
			format_exc = true;
		}

		Assert.AreEqual (format_error, format_exc, "A1");
		Assert.AreEqual (overflow_error, overflow_exc, "A2");
		if (!overflow_exc && !format_exc)
			Assert.AreEqual (expected, result.ToString ());
	}
Exemplo n.º 46
0
 public static TimeSpan ParseExact(string input, [StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] string[] formats, IFormatProvider?formatProvider, TimeSpanStyles styles)
 {
     ValidateStyles(styles, nameof(styles));
     if (input == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
     }
     return(TimeSpanParse.ParseExactMultiple(input, formats, formatProvider, styles));
 }
 public TimeSpan ParseExact(IEnumerable<string> formats, TimeSpanStyles styles, TimeSpan defaultValue = default(TimeSpan))
 {
     return ParseExact(formats, CultureInfo.CurrentCulture, styles, defaultValue);
 }
Exemplo n.º 48
0
 public static TimeSpan ParseExact(ReadOnlySpan <char> input, [StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] string[] formats, IFormatProvider?formatProvider, TimeSpanStyles styles = TimeSpanStyles.None)
 {
     ValidateStyles(styles, nameof(styles));
     return(TimeSpanParse.ParseExactMultiple(input, formats, formatProvider, styles));
 }
 public TimeSpan ParseExactCulture(string format, TimeSpanStyles styles, TimeSpan defaultValue = default(TimeSpan))
 {
     return ParseExact(format, CultureInfo.CurrentCulture, styles, defaultValue);
 }
Exemplo n.º 50
0
 public static bool TryParseExact(ReadOnlySpan <char> input, [StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] ReadOnlySpan <char> format, IFormatProvider?formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     ValidateStyles(styles, nameof(styles));
     return(TimeSpanParse.TryParseExact(input, format, formatProvider, styles, out result));
 }
Exemplo n.º 51
0
 public static TimeSpan ParseTimeSpanExactMultiple(String value, String[] formats, IFormatProvider provider, TimeSpanStyles styles)
 {
     return FormatProvider.TimeSpanParse.ParseExactMultiple(value, formats, provider, styles);
 }
Exemplo n.º 52
0
 public static bool TryParseExact([NotNullWhen(true)] string?input, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] string?[]?formats, IFormatProvider?formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     ValidateStyles(styles, nameof(styles));
     if (input == null)
     {
         result = default;
         return(false);
     }
     return(TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result));
 }
Exemplo n.º 53
0
		/// <summary>
		/// 将字符串分析为Timespan
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static TimeSpan ToTimeSpanExact(this string value, string[] format, IFormatProvider formatProvider = null, TimeSpanStyles styles = TimeSpanStyles.None)
		{
			TimeSpan ts;
			if (TimeSpan.TryParseExact(value, format, formatProvider, styles, out ts))
				return ts;

			return TimeSpan.Zero;
		}
Exemplo n.º 54
0
 public static bool TryParseExact(ReadOnlySpan <char> input, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] string?[]?formats, IFormatProvider?formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     ValidateStyles(styles, nameof(styles));
     return(TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result));
 }
Exemplo n.º 55
0
        //
        //  TryParseByFormat
        //
        //  Actions: Parse the TimeSpan instance using the specified format.  Used by TryParseExactTimeSpan.
        // 
        private static Boolean TryParseByFormat(String input, String format, TimeSpanStyles styles, ref TimeSpanResult result) {
            Debug.Assert(input != null, "input != null");
            Debug.Assert(format != null, "format != null");

            bool seenDD = false;      // already processed days?
            bool seenHH = false;      // already processed hours?
            bool seenMM = false;      // already processed minutes?
            bool seenSS = false;      // already processed seconds?
            bool seenFF = false;      // already processed fraction?
            int dd = 0;               // parsed days
            int hh = 0;               // parsed hours
            int mm = 0;               // parsed minutes
            int ss = 0;               // parsed seconds
            int leadingZeroes = 0;    // number of leading zeroes in the parsed fraction
            int ff = 0;               // parsed fraction
            int i = 0;                // format string position
            int tokenLen = 0;         // length of current format token, used to update index 'i'

            TimeSpanTokenizer tokenizer = new TimeSpanTokenizer();
            tokenizer.Init(input, -1);

            while (i < format.Length) {
                char ch = format[i];
                int nextFormatChar;
                switch (ch) {
                    case 'h':
                        tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                        if (tokenLen > 2 || seenHH || !ParseExactDigits(ref tokenizer, tokenLen, out hh)) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        seenHH = true;
                        break;
                    case 'm':
                        tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                        if (tokenLen > 2 || seenMM || !ParseExactDigits(ref tokenizer, tokenLen, out mm)) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        seenMM = true;
                        break;
                    case 's':
                        tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                        if (tokenLen > 2 || seenSS || !ParseExactDigits(ref tokenizer, tokenLen, out ss)) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        seenSS = true;
                        break;
                    case 'f':
                        tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                        if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF || !ParseExactDigits(ref tokenizer, tokenLen, tokenLen, out leadingZeroes, out ff)) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        seenFF = true;
                        break;
                    case 'F':
                        tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                        if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        ParseExactDigits(ref tokenizer, tokenLen, tokenLen, out leadingZeroes, out ff);
                        seenFF = true;
                        break;
                    case 'd':
                        tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                        int tmp = 0;
                        if (tokenLen > 8 || seenDD || !ParseExactDigits(ref tokenizer, (tokenLen<2) ? 1 : tokenLen, (tokenLen<2) ? 8 : tokenLen, out tmp, out dd)) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        seenDD = true;
                        break;
                    case '\'':
                    case '\"':
                        StringBuilder enquotedString = new StringBuilder();
                        if (!DateTimeParse.TryParseQuoteString(format, i, enquotedString, out tokenLen)) {
                            result.SetFailure(ParseFailureKind.FormatWithParameter, "Format_BadQuote", ch);
                            return false;
                        }
                        if (!ParseExactLiteral(ref tokenizer, enquotedString)) {
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        break;
                    case '%':
                        // Optional format character.
                        // For example, format string "%d" will print day 
                        // Most of the cases, "%" can be ignored.
                        nextFormatChar = DateTimeFormat.ParseNextChar(format, i);
                        // nextFormatChar will be -1 if we already reach the end of the format string.
                        // Besides, we will not allow "%%" appear in the pattern.
                        if (nextFormatChar >= 0 && nextFormatChar != (int)'%') {
                            tokenLen = 1; // skip the '%' and process the format character
                            break;
                        }
                        else {
                            // This means that '%' is at the end of the format string or
                            // "%%" appears in the format string.
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                    case '\\':
                        // Escaped character.  Can be used to insert character into the format string.
                        // For example, "\d" will insert the character 'd' into the string.
                        //
                        nextFormatChar = DateTimeFormat.ParseNextChar(format, i);
                        if (nextFormatChar >= 0 && tokenizer.NextChar == (char)nextFormatChar) {
                            tokenLen = 2;
                        } 
                        else {
                            // This means that '\' is at the end of the format string or the literal match failed.
                            result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                            return false;
                        }
                        break;
                    default:
                        result.SetFailure(ParseFailureKind.Format, "Format_InvalidString");
                        return false;
                }
                i += tokenLen;
            }


            if (!tokenizer.EOL) {
                // the custom format didn't consume the entire input
                result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
                return false;
            }
           
            long ticks = 0;
            bool positive = (styles & TimeSpanStyles.AssumeNegative) == 0;
            if (TryTimeToTicks(positive, new TimeSpanToken(dd),
                                         new TimeSpanToken(hh),
                                         new TimeSpanToken(mm),
                                         new TimeSpanToken(ss),
                                         new TimeSpanToken(leadingZeroes, ff),
                                         out ticks)) {
                if (!positive) ticks = -ticks;
                result.parsedTimeSpan._ticks = ticks;
                return true;
            }
            else {
                result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge");
                return false;

            }
        }
Exemplo n.º 56
0
 public static TimeSpan ParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles)
 {
     TimeSpanParse.ValidateStyles(styles, nameof(styles));
     return(TimeSpanParse.ParseExact(input, format, formatProvider, styles));
 }
Exemplo n.º 57
0
        internal static TimeSpan ParseExactMultiple(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles) {
            TimeSpanResult parseResult = new TimeSpanResult();
            parseResult.Init(TimeSpanThrowStyle.All);

            if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) {
                return parseResult.parsedTimeSpan;
            }
            else {
                throw parseResult.GetTimeSpanParseException();
            }
        }
 internal static bool TryParseExactMultiple(string input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
 {
     TimeSpanResult result2 = new TimeSpanResult();
     result2.Init(TimeSpanThrowStyle.None);
     if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref result2))
     {
         result = result2.parsedTimeSpan;
         return true;
     }
     result = new TimeSpan();
     return false;
 }
Exemplo n.º 59
0
 // ---- SECTION:  members for internal support ---------*
 internal static void ValidateStyles(TimeSpanStyles style, String parameterName) {
     if (style != TimeSpanStyles.None && style != TimeSpanStyles.AssumeNegative)
         throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTimeSpanStyles"), parameterName);
 }
Exemplo n.º 60
0
 public TimeSpan ParseExactCulture(string format, TimeSpanStyles styles, TimeSpan defaultValue = default(TimeSpan))
 {
     return(ParseExact(format, CultureInfo.CurrentCulture, styles, defaultValue));
 }