Пример #1
0
        /// <summary>
        ///  Parses a string that represents a time span, with CultureInfo used to parse the string. 
        /// </summary>
        /// <param name="timeSpan">A string containing the value to be parsed. </param>
        /// <param name="result">A container for a successfully-parsed time span. </param>
        /// <param name="age">Specifies whether the timespan should be considered as an age or not.</param>
        /// <returns>True if the value was successfully converted; otherwise, false. .</returns>
        /// /// <remarks>
        /// If the string could be parsed, the result parameter is set to an 
        /// CuiTimeSpan object corresponding to the parsed timeSpanString. If it could not be parsed, the result is set to null.
        /// </remarks>
        public static bool TryParse(string timeSpan, out CuiTimeSpan result, bool age)
        {
            if (timeSpan == null)
            {
                throw new ArgumentNullException("timeSpan");
            }

            string parseExpression = BuildParseRegularExpression();
            Regex regEx = new Regex(parseExpression);
            Match m = regEx.Match(timeSpan);
            if (m.Success)
            {
                DateTime from = DateTime.Now;
                DateTime to = from;
                DateTime span = from;
                long value;

                if (ParseGroupIntegerValue(m.Groups["seconds"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddSeconds(value);
                }

                if (ParseGroupIntegerValue(m.Groups["minutes"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddMinutes(value);
                }

                if (ParseGroupIntegerValue(m.Groups["hours"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddHours(value);
                }

                if (ParseGroupIntegerValue(m.Groups["weeks"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddDays(value * 7);
                }

                if (ParseGroupIntegerValue(m.Groups["days"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddDays(value);
                }

                if (ParseGroupIntegerValue(m.Groups["months"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddMonths((int)value);
                }

                if (ParseGroupIntegerValue(m.Groups["years"], out value))
                {
                    value = age ? value * -1 : value;
                    span = span.AddYears((int)value);
                }

                if (age)
                {
                    from = span;
                }
                else
                {
                    to = span;
                }

                result = new CuiTimeSpan(from, to);
                return true;
            }

            result = null;
            return false;
        }
Пример #2
0
        /// <summary>
        ///  Parses a string that represents a time span, with CultureInfo used to parse the string.
        /// </summary>
        /// <param name="timeSpan">A string containing the value to be parsed. </param>
        /// <param name="result">A container for a successfully-parsed time span. </param>
        /// <param name="age">Specifies whether the timespan should be considered as an age or not.</param>
        /// <returns>True if the value was successfully converted; otherwise, false. .</returns>
        /// /// <remarks>
        /// If the string could be parsed, the result parameter is set to an
        /// CuiTimeSpan object corresponding to the parsed timeSpanString. If it could not be parsed, the result is set to null.
        /// </remarks>
        public static bool TryParse(string timeSpan, out CuiTimeSpan result, bool age)
        {
            if (timeSpan == null)
            {
                throw new ArgumentNullException("timeSpan");
            }

            string parseExpression = BuildParseRegularExpression();
            Regex  regEx           = new Regex(parseExpression);
            Match  m = regEx.Match(timeSpan);

            if (m.Success)
            {
                DateTime from = DateTime.Now;
                DateTime to   = from;
                DateTime span = from;
                long     value;

                if (ParseGroupIntegerValue(m.Groups["seconds"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddSeconds(value);
                }

                if (ParseGroupIntegerValue(m.Groups["minutes"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddMinutes(value);
                }

                if (ParseGroupIntegerValue(m.Groups["hours"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddHours(value);
                }

                if (ParseGroupIntegerValue(m.Groups["weeks"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddDays(value * 7);
                }

                if (ParseGroupIntegerValue(m.Groups["days"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddDays(value);
                }

                if (ParseGroupIntegerValue(m.Groups["months"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddMonths((int)value);
                }

                if (ParseGroupIntegerValue(m.Groups["years"], out value))
                {
                    value = age ? value * -1 : value;
                    span  = span.AddYears((int)value);
                }

                if (age)
                {
                    from = span;
                }
                else
                {
                    to = span;
                }

                result = new CuiTimeSpan(from, to);
                return(true);
            }

            result = null;
            return(false);
        }
Пример #3
0
 /// <summary>
 /// Parses a string that represents a time span.
 /// </summary>
 /// <param name="timeSpan">A string containing the value to be parsed. </param>
 /// <param name="result">A container for a successfully-parsed time span. </param>
 /// <returns>True if the value was successfully parsed; otherwise, false. </returns>
 /// <remarks>
 /// If the string could be parsed, the result parameter is set to an 
 /// CuiTimeSpan object corresponding to the parsed timeSpanString. If it could not be parsed, the result is set to null.
 /// </remarks>
 public static bool TryParse(string timeSpan, out CuiTimeSpan result)
 {
     return TryParse(timeSpan, out result, false);
 }       
Пример #4
0
 /// <summary>
 /// Parses a string that represents a time span.
 /// </summary>
 /// <param name="timeSpan">A string containing the value to be parsed. </param>
 /// <param name="result">A container for a successfully-parsed time span. </param>
 /// <returns>True if the value was successfully parsed; otherwise, false. </returns>
 /// <remarks>
 /// If the string could be parsed, the result parameter is set to an
 /// CuiTimeSpan object corresponding to the parsed timeSpanString. If it could not be parsed, the result is set to null.
 /// </remarks>
 public static bool TryParse(string timeSpan, out CuiTimeSpan result)
 {
     return(TryParse(timeSpan, out result, false));
 }