public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var str = value as string; if (str != null) { string text = str.Trim(); try { object result; if (AllowHex && text[0] == '#') { result = FromString(text.Substring(1), 16); return result; } if ((AllowHex && text.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) || text.StartsWith("&h", StringComparison.OrdinalIgnoreCase)) { result = FromString(text.Substring(2), 16); return result; } culture = culture ?? CultureInfo.CurrentCulture; var formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); result = FromString(text, formatInfo); return result; } catch (Exception innerException) { throw new Exception(text, innerException); } } return base.ConvertFrom(context, culture, value); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (culture == null) culture = CultureInfo.CurrentCulture; string text = value as string; if (text != null) { try { if (SupportHex) { if (text.Length >= 1 && text[0] == '#') { return ConvertFromString (text.Substring (1), 16); } if (text.StartsWith ("0x") || text.StartsWith ("0X")) { return ConvertFromString (text, 16); } } NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo)); return ConvertFromString (text, numberFormatInfo); } catch (Exception e) { // LAMESPEC MS wraps the actual exception in an Exception throw new Exception (value.ToString() + " is not a valid " + "value for " + InnerType.Name + ".", e); } } return base.ConvertFrom (context, culture, value); }
/// <summary> /// converts datetime from format yyyy/MM/dd HH:mm:ss /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <returns></returns> public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { string DateString = (string)value; try { if (culture == null) { return(DateTime.Parse(DateString)); } else { DateTimeFormatInfo info = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); info.ShortDatePattern = "yyyy/MM/dd HH:mm:ss"; return(DateTime.Parse(DateString, info)); } } catch { throw new FormatException(DateString + " is not a valid DateTime value. The format should be yyyy/MM/dd HH:mm:ss"); } } return(base.ConvertFrom(context, culture, value)); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string textValue = ((string) value).Trim(); Int64 returnValue; try { if (textValue[0] == '#') { return Convert.ToInt64(textValue.Substring(1), 0x10); } if (textValue.StartsWith("0x") || textValue.StartsWith("0X") || textValue.StartsWith("&h") || textValue.StartsWith("&H")) { return Convert.ToInt64(textValue.Substring(2), 0x10); } if (culture == null) { culture = CultureInfo.CurrentCulture; } NumberFormatInfo formatInfo = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo)); returnValue = Int64.Parse(textValue, NumberStyles.Integer, formatInfo); } catch (Exception exception) { throw new Exception("Failed to ConvertFrom: " + textValue, exception); } if (IsValid(context, returnValue) == false) { throw new Exception("Value is not in the valid range of numbers."); } return returnValue; } return base.ConvertFrom(context, culture, value); }
/// <devdoc> /// <para>Converts the given value object to a <see cref='System.DateTime'/> /// object.</para> /// </devdoc> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = ((string)value).Trim(); if (text.Length == 0) { return DateTimeOffset.MinValue; } try { // See if we have a culture info to parse with. If so, then use it. // DateTimeFormatInfo formatInfo = null; if (culture != null ) { formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); } if (formatInfo != null) { return DateTimeOffset.Parse(text, formatInfo); } else { return DateTimeOffset.Parse(text, culture); } } catch (FormatException e) { throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "DateTimeOffset"), e); } } return base.ConvertFrom(context, culture, value); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string str = ((string) value).Trim(); try { if (this.AllowHex && (str[0] == '#')) { return this.FromString(str.Substring(1), 0x10); } if ((this.AllowHex && str.StartsWith("0x")) || ((str.StartsWith("0X") || str.StartsWith("&h")) || str.StartsWith("&H"))) { return this.FromString(str.Substring(2), 0x10); } if (culture == null) { culture = CultureInfo.CurrentCulture; } NumberFormatInfo format = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo)); return this.FromString(str, format); } catch (Exception exception) { throw this.FromStringError(str, exception); } } return base.ConvertFrom(context, culture, value); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string s = ((string) value).Trim(); if (s.Length == 0) { return DateTime.MinValue; } try { DateTimeFormatInfo provider = null; if (culture != null) { provider = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo)); } if (provider != null) { return DateTime.Parse(s, provider); } return DateTime.Parse(s, culture); } catch (FormatException exception) { throw new FormatException(SR.GetString("ConvertInvalidPrimitive", new object[] { (string) value, "DateTime" }), exception); } } return base.ConvertFrom(context, culture, value); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var s = value as string; if (s != null) { s = s.Trim(); if (s.Length == 0) { return DateTime.MinValue; } try { DateTimeFormatInfo provider = null; if (culture != null) { provider = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo)); } if (provider != null) { return DateTime.Parse(s, provider); } return DateTime.Parse(s, culture); } catch (FormatException exception) { throw new FormatException("Invalid DateTime!"); } } return base.ConvertFrom(context, culture, value); }
/// <summary> /// Initializes a new instance of the <see cref="BirthDateFormatInfo" /> class. /// </summary> /// <param name="representMissingComponentsWithX"> /// If set to <c>true</c> represent missing components with X; otherwise, use 0.</param> /// <param name="culture">The culture.</param> public BirthDateFormatInfo(CultureInfo culture = null, bool representMissingComponentsWithX = true) { if (culture == null) culture = CultureInfo.CurrentCulture; RepresentMissingComponentsWithX = representMissingComponentsWithX; Culture = culture; DateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if ((destinationType == typeof(string)) && (value is DateTime)) { string shortDatePattern; DateTime time = (DateTime) value; if (time == DateTime.MinValue) { return string.Empty; } if (culture == null) { culture = CultureInfo.CurrentCulture; } DateTimeFormatInfo format = null; format = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo)); if (culture == CultureInfo.InvariantCulture) { if (time.TimeOfDay.TotalSeconds == 0.0) { return time.ToString("yyyy-MM-dd", culture); } return time.ToString(culture); } if (time.TimeOfDay.TotalSeconds == 0.0) { shortDatePattern = format.ShortDatePattern; } else { shortDatePattern = format.ShortDatePattern + " " + format.ShortTimePattern; } return time.ToString(shortDatePattern, CultureInfo.CurrentCulture); } if ((destinationType == typeof(InstanceDescriptor)) && (value is DateTime)) { DateTime time2 = (DateTime) value; if (time2.Ticks == 0L) { ConstructorInfo member = typeof(DateTime).GetConstructor(new Type[] { typeof(long) }); if (member != null) { return new InstanceDescriptor(member, new object[] { time2.Ticks }); } } ConstructorInfo constructor = typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }); if (constructor != null) { return new InstanceDescriptor(constructor, new object[] { time2.Year, time2.Month, time2.Day, time2.Hour, time2.Minute, time2.Second, time2.Millisecond }); } } return base.ConvertTo(context, culture, value, destinationType); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="culture">The culture used when converting.</param> /// <param name="text">The string to convert to an object.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString( CultureInfo culture, string text ) { var formatProvider = (IFormatProvider)culture.GetFormat( typeof( DateTimeFormatInfo ) ) ?? culture; DateTime dt; if( DateTime.TryParse( text, formatProvider, DateTimeStyles.None, out dt ) ) { return dt; } return base.ConvertFromString( culture, text ); }
/// <summary> /// Converts the specified object to a <see cref="T:System.DateTime" /> /// with the specified culture with the specified format context. /// </summary> /// <param name="context"> /// The format context that is used to convert the specified type. /// </param> /// <param name="culture">The culture to use for the result.</param> /// <param name="value">The value to convert.</param> /// <returns> /// A <see cref="T:System.DateTime" /> object that represents /// <paramref name="value" />. /// </returns> /// <exception cref="System.FormatException"> /// The conversion cannot be performed. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The culture is null. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The value is null. /// </exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (culture == null) { throw new ArgumentNullException("culture"); } if (value == null) { throw new ArgumentNullException("value"); } DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat(typeof(DateTimeFormatInfo)); return DateTime.ParseExact(value.ToString(), info.ShortDatePattern, culture); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (((destinationType == typeof(string)) && (value != null)) && this.TargetType.IsInstanceOfType(value)) { if (culture == null) { culture = CultureInfo.CurrentCulture; } NumberFormatInfo format = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); return this.ToString(value, format); } if (destinationType.IsPrimitive) { return Convert.ChangeType(value, destinationType, culture); } return base.ConvertTo(context, culture, value, destinationType); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (culture == null) culture = CultureInfo.CurrentCulture; string text = value as string; if (text != null) { try { if (SupportHex) { if (text.Length >= 1 && text[0] == '#') { return ConvertFromString (text.Substring (1), 16); } if (text.StartsWith ("0x") || text.StartsWith ("0X")) { return ConvertFromString (text, 16); } } NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat(typeof(NumberFormatInfo)); if(text.EndsWith("mm")) { text = text.Replace("mm",string.Empty); double val = (double) ConvertFromString (text, numberFormatInfo); return val.mm(); }else if(text.EndsWith("cm")){ text = text.Replace("cm",string.Empty); double val = (double) ConvertFromString (text, numberFormatInfo); return val.cm(); }else if(text.EndsWith("in")){ text = text.Replace("in",string.Empty); double val = (double) ConvertFromString (text, numberFormatInfo); return val.inch(); }else if(text.EndsWith("pt")){ text = text.Replace("pt",string.Empty); double val = (double) ConvertFromString (text, numberFormatInfo); return val.pt(); } return ConvertFromString (text, numberFormatInfo); } catch (Exception e) { // LAMESPEC MS wraps the actual exception in an Exception throw new Exception (value.ToString() + Catalog.GetString(" is not a valid value for ") + InnerType.Name + ".", e); } } return base.ConvertFrom (context, culture, value); }
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string DateString = (string) value; try { if (DateString != null && DateString.Trim ().Length == 0) { return DateTime.MinValue; } else if (culture == null) { return DateTime.Parse (DateString); } else { DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo)); return DateTime.Parse (DateString, info); } } catch { throw new FormatException (DateString + " is not a valid DateTime value."); } } return base.ConvertFrom (context, culture, value); }
/// <summary> /// converts datetime from format yyyy/MM/dd HH:mm:ss /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <returns></returns> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { var info = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); var DateString = (string)value; try { return DateTime.Parse(DateString, info); } catch { throw new FormatException( string.Format("{0} is not a valid DateTime value. The format should be {1}", DateString, RegionalSettingsManager.DateTimeFormat)); } } return base.ConvertFrom(context, culture, value); }
public object ConvertTo(IValueContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (culture == null) { throw new ArgumentNullException("culture"); } DateTime? d = value as DateTime?; if (!d.HasValue || destinationType != typeof(string)) { throw new NotSupportedException(); } DateTimeFormatInfo dateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); return d.Value.ToString(dateTimeFormatInfo.ShortDatePattern, culture); }
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string s = ((string) value).Trim (); if (s.Length == 0) return DateTimeOffset.MinValue; DateTimeOffset retval; if (culture == null) { if (DateTimeOffset.TryParse (s, out retval)) return retval; } else { DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo)); if (DateTimeOffset.TryParse (s, info, DateTimeStyles.None, out retval)) return retval; } throw new FormatException (s + " is not a valid DateTimeOffset value."); } return base.ConvertFrom (context, culture, value); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value == null) throw new ArgumentNullException ("value"); if (culture == null) culture = CultureInfo.CurrentCulture; #if NET_2_0 if (destinationType == typeof (string) && value is IConvertible) return ((IConvertible) value).ToType (destinationType, culture); #else if (destinationType == typeof (string) && value.GetType () == InnerType) { NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo)); return ConvertToString (value, numberFormatInfo); } #endif if (destinationType.IsPrimitive) return Convert.ChangeType (value, destinationType, culture); return base.ConvertTo (context, culture, value, destinationType); }
/// <summary> /// Format the URI of the SmartUri base class. /// </summary> private void UpdatePayload() { if (Coordinate == null) return; // Make sure we always use the "en" culture to have "." as the decimal separator. var culture = new CultureInfo("en"); var newUri = string.Format((IFormatProvider)culture.GetFormat(typeof(NumberFormatInfo)), GeoTagTypeUris[GeoType], Latitude, Longitude); // Here Maps: add app ID if (IsHereMapsType(GeoType)) { // Add title if present if (!String.IsNullOrEmpty(PlaceTitle)) newUri += String.Format(HereMapsTitleUri, System.Uri.EscapeDataString(PlaceTitle)); // App ID has to be present if (!String.IsNullOrEmpty(AppId)) newUri += String.Format(HereMapsAppIdUri, AppId); } Uri = newUri; }
/// <devdoc> /// <para>Converts the given value object to a <see cref='System.DateTimeOffset'/> /// object /// using the arguments.</para> /// </devdoc> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { // logic is exactly as in DateTimeConverter, only the offset pattern ' zzz' is added to the default // ConvertToString pattern. if (destinationType == typeof(string) && value is DateTimeOffset) { DateTimeOffset dto = (DateTimeOffset) value; if (dto == DateTimeOffset.MinValue) { return string.Empty; } if (culture == null) { culture = CultureInfo.CurrentCulture; } DateTimeFormatInfo formatInfo = null; formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); string format; if (culture == CultureInfo.InvariantCulture) { // note: the y/m/d format used when there is or isn't a time component is not consistent // when the culture is invariant, because for some reason InvariantCulture's date format is // MM/dd/yyyy. However, this matches the behavior of DateTimeConverter so it is preserved here. if (dto.TimeOfDay.TotalSeconds == 0) { // pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds==0 // but with ' zzz' offset pattern added. return dto.ToString("yyyy-MM-dd zzz", culture); } else { return dto.ToString(culture); } } if (dto.TimeOfDay.TotalSeconds == 0) { // pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds==0 // but with ' zzz' offset pattern added. format = formatInfo.ShortDatePattern + " zzz"; } else { // pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds!=0 // but with ' zzz' offset pattern added. format = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern + " zzz"; } return dto.ToString(format, CultureInfo.CurrentCulture); } if (destinationType == typeof(InstanceDescriptor) && value is DateTimeOffset) { DateTimeOffset dto = (DateTimeOffset)value; if (dto.Ticks == 0) { // Make a special case for the empty DateTimeOffset // ConstructorInfo ctr = typeof(DateTimeOffset).GetConstructor(new Type[] {typeof(Int64)}); if (ctr != null) { return new InstanceDescriptor(ctr, new object[] { dto.Ticks }); } } ConstructorInfo ctor = typeof(DateTimeOffset).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(TimeSpan) }); if (ctor != null) { return new InstanceDescriptor(ctor, new object[] { dto.Year, dto.Month, dto.Day, dto.Hour, dto.Minute, dto.Second, dto.Millisecond, dto.Offset }); } } return base.ConvertTo(context, culture, value, destinationType); }
/// <devdoc> /// <para>Converts the given value object to a 64-bit signed integer object.</para> /// </devdoc> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = ((string)value).Trim(); try { if (AllowHex && text[0] == '#') { return FromString(text.Substring(1), 16); } else if (AllowHex && text.StartsWith("0x") || text.StartsWith("0X") || text.StartsWith("&h") || text.StartsWith("&H")) { return FromString(text.Substring(2), 16); } else { if (culture == null) { culture = CultureInfo.CurrentCulture; } NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); return FromString(text, formatInfo); } } catch (Exception e) { throw FromStringError(text, e); } } return base.ConvertFrom(context, culture, value); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) throw new ArgumentNullException("destinationType"); if (destinationType == typeof(string) && value != null && NumberType.IsInstanceOfType(value)) { culture = culture ?? CultureInfo.CurrentCulture; var formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); return ToString(value, formatInfo); } return base.ConvertTo(context, culture, value, destinationType); }
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value is DateTimeOffset) { DateTimeOffset dt_offset = (DateTimeOffset) value; if (destinationType == typeof (string)) { if (dt_offset == DateTimeOffset.MinValue) return String.Empty; if (culture == null) culture = CultureInfo.CurrentCulture; // InvariantCulture gets special handling. if (culture == CultureInfo.InvariantCulture) { if (dt_offset.DateTime == dt_offset.Date) return dt_offset.ToString (InvariantDatePattern + " " + OffsetPattern); return dt_offset.ToString (culture); } DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo)); if (dt_offset.DateTime == dt_offset.Date) return dt_offset.ToString (info.ShortDatePattern + " " + OffsetPattern); // No need to pass CultureInfo, as we already consumed the proper patterns. return dt_offset.ToString (info.ShortDatePattern + " " + info.ShortTimePattern + " " + OffsetPattern, null); } if (destinationType == typeof (InstanceDescriptor)) { ConstructorInfo ctor = typeof (DateTimeOffset).GetConstructor ( GetDateTimeOffsetArgumentTypes ()); object [] ctor_args = new object [] { dt_offset.Year, dt_offset.Month, dt_offset.Day, dt_offset.Hour, dt_offset.Minute, dt_offset.Second, dt_offset.Millisecond, dt_offset.Offset }; return new InstanceDescriptor (ctor, ctor_args); } } return base.ConvertTo (context, culture, value, destinationType); }
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value is DateTime) { DateTime datetime = (DateTime) value; if (destinationType == typeof (string)) { if (culture == null) culture = CultureInfo.CurrentCulture; if (datetime == DateTime.MinValue) return string.Empty; DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo)); if (culture == CultureInfo.InvariantCulture) { if (datetime.Equals (datetime.Date)) { return datetime.ToString ("yyyy-MM-dd", culture); } return datetime.ToString (culture); } else { if (datetime == datetime.Date) { return datetime.ToString (info.ShortDatePattern, culture); } else { return datetime.ToString (info.ShortDatePattern + " " + info.ShortTimePattern, culture); } } } else if (destinationType == typeof (InstanceDescriptor)) { ConstructorInfo ctor = typeof(DateTime).GetConstructor (new Type[] {typeof(long)}); return new InstanceDescriptor (ctor, new object[] { datetime.Ticks }); } } return base.ConvertTo (context, culture, value, destinationType); }
protected string FormatFS(decimal value, int nTotal, int nDecimals) { CultureInfo culture = new CultureInfo(CultureInfo.CurrentCulture.Name); NumberFormatInfo numInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); numInfo.NumberDecimalDigits = nDecimals; numInfo.NumberDecimalSeparator = "."; string decimals = new string('0',nDecimals); string format = "0." + decimals; string output = value.ToString(format, numInfo); return output; /* int posDecimal = output.IndexOf(numInfo.NumberDecimalSeparator); string sNumber = output.Substring(0, posDecimal); sNumber = sNumber.PadLeft(nTotal - nDecimals, '0'); string sDecimals = output.Substring(posDecimal + 1); sDecimals = sDecimals.PadRight(nDecimals, '0'); return String.Concat(sNumber, sDecimals); */ }
/// <devdoc> /// <para>Converts the given value object to a <see cref='System.DateTime'/> /// object /// using the arguments.</para> /// </devdoc> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is DateTime) { DateTime dt = (DateTime) value; if (dt == DateTime.MinValue) { return string.Empty; } if (culture == null) { culture = CultureInfo.CurrentCulture; } DateTimeFormatInfo formatInfo = null; formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); string format; if (culture == CultureInfo.InvariantCulture) { if (dt.TimeOfDay.TotalSeconds == 0) { return dt.ToString("yyyy-MM-dd", culture); } else { return dt.ToString(culture); } } if (dt.TimeOfDay.TotalSeconds == 0) { format = formatInfo.ShortDatePattern; } else { format = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern; } return dt.ToString(format, CultureInfo.CurrentCulture); } if (destinationType == typeof(InstanceDescriptor) && value is DateTime) { DateTime dt = (DateTime)value; if (dt.Ticks == 0) { // Make a special case for the empty DateTime // ConstructorInfo ctr = typeof(DateTime).GetConstructor(new Type[] {typeof(Int64)}); if (ctr != null) { return new InstanceDescriptor(ctr, new object[] { dt.Ticks }); } } ConstructorInfo ctor = typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int)}); if (ctor != null) { return new InstanceDescriptor(ctor, new object[] { dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond}); } } return base.ConvertTo(context, culture, value, destinationType); }
/// <devdoc> /// <para>Converts the given value object to a <see cref='System.DateTime'/> /// object /// using the arguments.</para> /// </devdoc> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is DateTime) { DateTime dt = (DateTime)value; if (dt == DateTime.MinValue) { return string.Empty; } if (culture == null) { culture = CultureInfo.CurrentCulture; } DateTimeFormatInfo formatInfo = null; formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); string format; if (culture == CultureInfo.InvariantCulture) { if (dt.TimeOfDay.TotalSeconds == 0) { return dt.ToString("yyyy-MM-dd", culture); } else { return dt.ToString(culture); } } if (dt.TimeOfDay.TotalSeconds == 0) { format = formatInfo.ShortDatePattern; } else { format = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern; } return dt.ToString(format, CultureInfo.CurrentCulture); } return base.ConvertTo(context, culture, value, destinationType); }
public void PosTest3() { CultureInfo myCultureInfo = new CultureInfo("en-US"); Assert.True(myCultureInfo is IFormatProvider); Assert.Null(myCultureInfo.GetFormat(typeof(string))); }
public void PosTest2() { CultureInfo myCultureInfo = new CultureInfo("en-US"); Assert.True(myCultureInfo is IFormatProvider); Assert.True(myCultureInfo.GetFormat(typeof(DateTimeFormatInfo)) is DateTimeFormatInfo); }
public void PosTest1() { CultureInfo myCultureInfo = new CultureInfo("en-US"); Assert.True(myCultureInfo is IFormatProvider); Assert.True(myCultureInfo.GetFormat(typeof(NumberFormatInfo)) is NumberFormatInfo); }