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 (DateString
!= null && DateString
.Trim ().Length
== 0) {
65 return DateTime
.MinValue
;
66 } else if (culture
== null) {
67 return DateTime
.Parse (DateString
);
69 DateTimeFormatInfo info
= (DateTimeFormatInfo
) culture
.GetFormat (typeof (DateTimeFormatInfo
));
70 return DateTime
.Parse (DateString
, info
);
73 throw new FormatException (DateString
+ " is not a valid DateTime value.");
76 return base.ConvertFrom (context
, culture
, value);
79 public override object ConvertTo (ITypeDescriptorContext context
, CultureInfo culture
, object value,
82 if (value is DateTime
) {
83 DateTime datetime
= (DateTime
) value;
84 if (destinationType
== typeof (string)) {
86 culture
= CultureInfo
.CurrentCulture
;
88 if (datetime
== DateTime
.MinValue
)
91 DateTimeFormatInfo info
= (DateTimeFormatInfo
) culture
.GetFormat (typeof (DateTimeFormatInfo
));
93 if (culture
== CultureInfo
.InvariantCulture
) {
94 if (datetime
.Equals (datetime
.Date
)) {
95 return datetime
.ToString ("yyyy-MM-dd", culture
);
97 return datetime
.ToString (culture
);
99 if (datetime
== datetime
.Date
) {
100 return datetime
.ToString (info
.ShortDatePattern
, culture
);
102 return datetime
.ToString (info
.ShortDatePattern
+ " " +
103 info
.ShortTimePattern
, culture
);
106 } else if (destinationType
== typeof (InstanceDescriptor
)) {
107 ConstructorInfo ctor
= typeof(DateTime
).GetConstructor (new Type
[] {typeof(long)}
);
108 return new InstanceDescriptor (ctor
, new object[] { datetime.Ticks }
);
111 return base.ConvertTo (context
, culture
, value, destinationType
);