Files
2026-06-04 11:42:34 +02:00

118 lines
5.8 KiB
C#

using System;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
namespace System.ComponentModel
{
/// <summary>Provides a type converter to convert <see cref="T:System.DateTime" /> objects to and from various other representations.</summary>
// Token: 0x0200006A RID: 106
public class DateTimeConverter : TypeConverter
{
/// <summary>Gets a value indicating whether this converter can convert an object in the given source type to a <see cref="T:System.DateTime" /> using the specified context.</summary>
/// <returns>true if this object can perform the conversion; otherwise, false.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="sourceType">A <see cref="T:System.Type" /> that represents the type you wish to convert from. </param>
// 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);
}
/// <summary>Gets a value indicating whether this converter can convert an object to the given destination type using the context.</summary>
/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="destinationType">A <see cref="T:System.Type" /> that represents the type you wish to convert to. </param>
// 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);
}
/// <summary>Converts the given value object to a <see cref="T:System.DateTime" />.</summary>
/// <returns>An <see cref="T:System.Object" /> that represents the converted <paramref name="value" />.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="culture">An optional <see cref="T:System.Globalization.CultureInfo" />. If not supplied, the current culture is assumed. </param>
/// <param name="value">The <see cref="T:System.Object" /> to convert. </param>
/// <exception cref="T:System.FormatException">
/// <paramref name="value" /> is not a valid value for the target type. </exception>
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
// 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);
}
/// <summary>Converts the given value object to a <see cref="T:System.DateTime" /> using the arguments.</summary>
/// <returns>An <see cref="T:System.Object" /> that represents the converted <paramref name="value" />.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="culture">An optional <see cref="T:System.Globalization.CultureInfo" />. If not supplied, the current culture is assumed. </param>
/// <param name="value">The <see cref="T:System.Object" /> to convert. </param>
/// <param name="destinationType">The <see cref="T:System.Type" /> to convert the value to. </param>
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
// 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);
}
}
}