using System;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
namespace System.ComponentModel
{
/// Provides a type converter to convert objects to and from various other representations.
// Token: 0x0200006A RID: 106
public class DateTimeConverter : TypeConverter
{
/// Gets a value indicating whether this converter can convert an object in the given source type to a using the specified context.
/// true if this object can perform the conversion; otherwise, false.
/// An that provides a format context.
/// A that represents the type you wish to convert from.
// Token: 0x060003EB RID: 1003 RVA: 0x0000BE10 File Offset: 0x0000A010
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
/// Gets a value indicating whether this converter can convert an object to the given destination type using the context.
/// true if this converter can perform the conversion; otherwise, false.
/// An that provides a format context.
/// A that represents the type you wish to convert to.
// Token: 0x060003EC RID: 1004 RVA: 0x0000BE2C File Offset: 0x0000A02C
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
/// Converts the given value object to a .
/// An that represents the converted .
/// An that provides a format context.
/// An optional . If not supplied, the current culture is assumed.
/// The to convert.
///
/// is not a valid value for the target type.
/// The conversion cannot be performed.
// Token: 0x060003ED RID: 1005 RVA: 0x0000BE48 File Offset: 0x0000A048
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string text = (string)value;
try
{
if (text != null && text.Trim().Length == 0)
{
return DateTime.MinValue;
}
if (culture == null)
{
return DateTime.Parse(text);
}
DateTimeFormatInfo dateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
return DateTime.Parse(text, dateTimeFormatInfo);
}
catch
{
throw new FormatException(text + " is not a valid DateTime value.");
}
}
return base.ConvertFrom(context, culture, value);
}
/// Converts the given value object to a using the arguments.
/// An that represents the converted .
/// An that provides a format context.
/// An optional . If not supplied, the current culture is assumed.
/// The to convert.
/// The to convert the value to.
/// The conversion cannot be performed.
// Token: 0x060003EE RID: 1006 RVA: 0x0000BF10 File Offset: 0x0000A110
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 dateTimeFormatInfo = (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(dateTimeFormatInfo.ShortDatePattern, culture);
}
return dateTime.ToString(dateTimeFormatInfo.ShortDatePattern + " " + dateTimeFormatInfo.ShortTimePattern, culture);
}
}
else if (destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
ConstructorInfo constructor = typeof(DateTime).GetConstructor(new Type[] { typeof(long) });
return new global::System.ComponentModel.Design.Serialization.InstanceDescriptor(constructor, new object[] { dateTime.Ticks });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}