2 // System.ComponentModel.DateTimeConverter
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.ComponentModel.Design.Serialization;
34 using System.Globalization;
35 using System.Reflection;
37 namespace System.ComponentModel
39 public class DateTimeConverter : TypeConverter
41 public DateTimeConverter()
45 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
47 if (sourceType == typeof (string))
49 return base.CanConvertFrom (context, sourceType);
52 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
54 if (destinationType == typeof (InstanceDescriptor))
56 return base.CanConvertTo (context, destinationType);
59 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
61 if (value is string) {
62 string DateString = (string) value;
64 if (culture == null) {
65 return DateTime.Parse (DateString);
67 DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
68 return DateTime.Parse (DateString, info);
71 throw new FormatException (DateString + "is not a valid DateTime value.");
74 return base.ConvertFrom (context, culture, value);
77 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
80 if (destinationType == typeof (string) && value is DateTime) {
81 if (culture == null) {
82 culture = CultureInfo.CurrentCulture;
84 DateTime datetime = (DateTime) value;
85 if (datetime == DateTime.MinValue) {
89 DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
91 if (culture == CultureInfo.InvariantCulture) {
92 if (datetime.Equals (datetime.Date)) {
93 return datetime.ToString ("yyyy-mm-dd", culture);
95 return datetime.ToString (culture);
97 if (datetime == datetime.Date) {
98 return datetime.ToString (info.ShortDatePattern, culture);
100 return datetime.ToString (info.ShortDatePattern + " " +
101 info.ShortTimePattern, culture);
105 if (destinationType == typeof (InstanceDescriptor) && value is DateTime) {
106 DateTime cval = (DateTime) value;
107 ConstructorInfo ctor = typeof(DateTime).GetConstructor (new Type[] {typeof(long)});
108 return new InstanceDescriptor (ctor, new object[] {cval.Ticks});
110 return base.ConvertTo (context, culture, value, destinationType);