/// <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); }
/// <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()); }