예제 #1
0
        private static string GetFormatString(TimeSpan formatTimeSpan, TimeSpanComponents components)
        {
            if (formatTimeSpan == null)
            {
                throw new ArgumentNullException("formatTimeSpan");
            }

            switch (components)
            {
            case TimeSpanComponents.Days:
                return(formatTimeSpan.Days == 0 ? string.Empty : formatTimeSpan.Days + "d");

            case TimeSpanComponents.Hours:
                return(formatTimeSpan.Hours == 0 ? string.Empty : formatTimeSpan.Hours + "h");

            case TimeSpanComponents.Minutes:
                return(formatTimeSpan.Minutes == 0 ? string.Empty : formatTimeSpan.Minutes + "m");

            case TimeSpanComponents.Seconds:
                return(formatTimeSpan.Seconds == 0 ? string.Empty : formatTimeSpan.Seconds + "s");

            case TimeSpanComponents.Milliseconds:
                return(formatTimeSpan.Milliseconds == 0 ? string.Empty : formatTimeSpan.Milliseconds + "ms");

            default:
                return(string.Empty);
            }
        }
        /// <summary>
        /// Parse a string formatted in HH:MM:ss or 1hr5min3sec into a TimeSpan
        /// </summary>
        /// <param name="timeSpanValue">string to parse</param>
        /// <returns>the parsed Time Span</returns>
        /// <param name="truncateSubSecond">true to indicate dropping subsecond values for the returned time span.
        /// False to include the subsecond value</param>
        /// <param name="defaultUnit">The default unit for times given without a unit.  This would typically be TimeSpan.FromMinutes(1) or TimeSpan.FromHours(1).
        /// If defaultUnit is not specified (null) then 1 minute will be used as the default unit.</param>
        public static TimeSpan ParseTimeSpan(string timeSpanValue, bool truncateSubSecond, TimeSpan?defaultUnit)
        {
            TimeSpanComponents components = new TimeSpanComponents();

            try
            {
                components.GetComponents(timeSpanValue, truncateSubSecond);

                // if time unit was not specified we are assuming that they are minutes.
                if ((components.dayFound == 0) && (components.hourFound == 0) && (components.minFound == 0) && (components.secFound == 0))
                {
                    double minutes = TimeSpanComponents.ConvertValue(timeSpanValue, 2);
                    GetTimeWithDefaultUnits(components, minutes, defaultUnit);
                }

                if (components.dayFound > 1 || components.hourFound > 1 || components.minFound > 1 || components.secFound > 1)
                {
                    throw new AutoChemException(string.Format(InvalidTimeDuplicatedParts, timeSpanValue));
                }
            }
            catch (AutoChemException ex)
            {
                Trace.TraceError(ex.ToString());
                throw;
            }
            catch (Exception ex)
            {
                Trace.TraceError(string.Format(InvalidTimeFormat, timeSpanValue, ex));
                throw new AutoChemException(InvalidTimeFormat);
            }

            // Note we are doing this manually to support more than 24hours, or more than 60 minutes, or seconds.
            return(components.BuildTimeSpan());
        }
        /// <summary>
        /// Parse a string formatted in HH:MM:ss or 1hr5min3sec into a TimeSpan
        /// </summary>
        /// <param name="timeSpanValue">string to parse</param>
        /// <returns>ok or failed to parse</returns>
        /// <param name="truncateSubSecond">true to indicate dropping subsecond values for the returned time span.
        /// False to include the subsecond value</param>
        /// <param name="defaultUnit">The default unit for times given without a unit.  This would typically be TimeSpan.FromMinutes(1) or TimeSpan.FromHours(1).
        /// If defaultUnit is not specified (null) then 1 minute will be used as the default unit.</param>
        /// <param name="parsedTimeSpan">Newly parsed Time Span</param>
        public static bool TryParseTimeSpan(string timeSpanValue, bool truncateSubSecond, TimeSpan?defaultUnit, out TimeSpan parsedTimeSpan)
        {
            parsedTimeSpan = TimeSpan.Zero;
            TimeSpanComponents components = new TimeSpanComponents();

            try
            {
                components.GetComponents(timeSpanValue, truncateSubSecond);

                if ((components.dayFound == 0) && (components.hourFound == 0) && (components.minFound == 0) && (components.secFound == 0))
                {
                    double minutes;
                    if (!TimeSpanComponents.TryConvertValue(timeSpanValue, 2, out minutes))
                    {
                        return(false);
                    }

                    GetTimeWithDefaultUnits(components, minutes, defaultUnit);
                }

                if (components.dayFound > 1 || components.hourFound > 1 || components.minFound > 1 || components.secFound > 1)
                {
                    return(false);
                }
            }
            catch (Exception exception)
            {
                GC.KeepAlive(exception);
                return(false);
            }

            // Note we are doing this manually to support more than 24hours, or more than 60 minutes, or seconds.
            parsedTimeSpan = components.BuildTimeSpan();
            return(true);
        }
 private static void GetTimeWithDefaultUnits(TimeSpanComponents components, double parsedMinutes, TimeSpan?defaultUnit)
 {
     if (defaultUnit.HasValue)
     {
         var timeSpan = defaultUnit.Value.Multiply(parsedMinutes);
         components.minutes = timeSpan.TotalMinutes;
     }
     else
     {
         components.minutes = parsedMinutes;
     }
 }
예제 #5
0
        private static string GetFormatString(TimeSpan formatTimeSpan, TimeSpanComponents components)
        {
            if (formatTimeSpan == null)
            throw new ArgumentNullException("formatTimeSpan");

             switch (components)
             {
            case TimeSpanComponents.Days:
               return formatTimeSpan.Days == 0 ? string.Empty : formatTimeSpan.Days + "d";

            case TimeSpanComponents.Hours:
               return formatTimeSpan.Hours == 0 ? string.Empty : formatTimeSpan.Hours + "h";

            case TimeSpanComponents.Minutes:
               return formatTimeSpan.Minutes == 0 ? string.Empty : formatTimeSpan.Minutes + "m";

            case TimeSpanComponents.Seconds:
               return formatTimeSpan.Seconds == 0 ? string.Empty : formatTimeSpan.Seconds + "s";

            case TimeSpanComponents.Milliseconds:
               return formatTimeSpan.Milliseconds == 0 ? string.Empty : formatTimeSpan.Milliseconds + "ms";

            default:
               return string.Empty;
             }
        }