/// <summary>Converts the given object to the type of this converter, using the specified context and culture information.</summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param> /// <param name="value">The <see cref="T:System.Object"/> to convert.</param> /// <returns>An <see cref="T:System.Object"/> that represents the converted value.</returns> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var sval = value as string; if (sval != null) { if (string.IsNullOrEmpty(sval)) { return(TimeSpan2.Zero); } var fi = new TimeSpan2FormatInfo(culture); if (fi.TryParse(sval, null, out var ts)) { return((TimeSpan2)ts); } } try { var l = Convert.ToInt64(value, CultureInfo.CurrentUICulture); return(new TimeSpan2(l)); } catch { } return(base.ConvertFrom(context, culture, value)); }
/// <summary> /// Returns string representation of the value of this instance using the specified format. /// </summary> /// <param name="format">A TimeSpan format string.</param> /// <param name="formatProvider">An <see cref="T:System.IFormatProvider"/> object that supplies format information about the current instance.</param> /// <returns>A string representation of value of the current <see cref="TimeSpan2"/> object as specified by format.</returns> public string ToString(string format, IFormatProvider formatProvider) { var tfi = TimeSpan2FormatInfo.GetInstance(formatProvider); return(tfi.Format(format, this, formatProvider)); }
/// <summary> /// Converts the string representation of a time interval to its <see cref="TimeSpan2"/> equivalent by using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. /// </summary> /// <param name="input">A string that specifies the time interval to convert.</param> /// <param name="formats">A array of standard or custom format strings that defines the required format of <paramref name="input"/>.</param> /// <param name="formatProvider">An object that provides culture-specific formatting information.</param> /// <returns>A time interval that corresponds to <paramref name="input"/>, as specified by <paramref name="formats"/> and <paramref name="formatProvider"/>.</returns> public static TimeSpan2 ParseExact(string input, string[] formats, IFormatProvider formatProvider) { var fi = TimeSpan2FormatInfo.GetInstance(formatProvider); return(fi.ParseExact(input, formats, null)); }
/// <summary> /// Converts the string representation of a time interval to its <see cref="TimeSpan2"/> equivalent by using the specified formats and culture-specific format information, and returns a value that indicates whether the conversion succeeded. The format of the string representation must match one of the specified formats exactly. /// </summary> /// <param name="input">A string that specifies the time interval to convert.</param> /// <param name="formats">A array of standard or custom format strings that define the acceptable formats of <paramref name="input"/>.</param> /// <param name="formatProvider">An object that supplies culture-specific format information about <paramref name="input"/>.</param> /// <param name="result">When this method returns, contains an object that represents the time interval specified by <paramref name="input"/>, or <see cref="TimeSpan.Zero"/> if the conversion failed. This parameter is passed uninitialized.</param> /// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, <c>false</c>.</returns> public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, out TimeSpan2 result) { var fi = TimeSpan2FormatInfo.GetInstance(formatProvider); return(fi.TryParseExact(input, formats, null, out result.core)); }
/// <summary> /// Converts the specified string representation of a time span to its <see cref="TimeSpan2"/> equivalent using the specified culture-specific format information. /// </summary> /// <param name="value">A string containing a time span to parse.</param> /// <param name="formatProvider">An object that supplies culture-specific format information about <paramref name="value"/>.</param> /// <returns>A <see cref="TimeSpan2"/> equivalent to the time span contained in <paramref name="value"/> as specified by <paramref name="formatProvider"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <c>null</c>.</exception> /// <exception cref="FormatException"><paramref name="value"/> does not contain a valid string representation of a time span.</exception> public static TimeSpan2 Parse(string value, IFormatProvider formatProvider) { var fi = TimeSpan2FormatInfo.GetInstance(formatProvider); return(new TimeSpan2(fi.Parse(value, null))); }