use MOONLIGHT symbol
[mono.git] / mcs / class / System / System.ComponentModel / DateTimeConverter.cs
1 //
2 // System.ComponentModel.DateTimeConverter
3 //
4 // Authors:
5 //  Martin Willemoes Hansen (mwh@sysrq.dk)
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System.ComponentModel.Design.Serialization;
34 using System.Globalization;
35 using System.Reflection;
36
37 namespace System.ComponentModel
38 {
39         public class DateTimeConverter : TypeConverter
40         {
41                 public DateTimeConverter()
42                 {
43                 }
44
45                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
46                 {
47                         if (sourceType == typeof (string)) 
48                                 return true;
49                         return base.CanConvertFrom (context, sourceType);
50                 }
51
52                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
53                 {
54                         if (destinationType == typeof (InstanceDescriptor))
55                                 return true;
56                         return base.CanConvertTo (context, destinationType);
57                 }
58
59                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
60                 {
61                         if (value is string) {
62                                 string DateString = (string) value;
63                                 try {
64                                         if (DateString != null && DateString.Trim ().Length == 0) {
65                                                 return DateTime.MinValue;
66                                         } else if (culture == null) {
67                                                 return DateTime.Parse (DateString);
68                                         } else {
69                                                 DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
70                                                 return DateTime.Parse (DateString, info);
71                                         }
72                                 } catch {
73                                         throw new FormatException (DateString + " is not a valid DateTime value.");
74                                 }
75                         }
76                         return base.ConvertFrom (context, culture, value);
77                 }
78
79                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
80                                                   Type destinationType)
81                 {
82                         if (value is DateTime) {
83                                 DateTime datetime = (DateTime) value;
84                                 if (destinationType == typeof (string)) {
85                                         if (culture == null)
86                                                 culture = CultureInfo.CurrentCulture;
87
88                                         if (datetime == DateTime.MinValue)
89                                                 return string.Empty;
90
91                                         DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
92
93                                         if (culture == CultureInfo.InvariantCulture) {
94                                                 if (datetime.Equals (datetime.Date)) {
95                                                         return datetime.ToString ("yyyy-MM-dd", culture);
96                                                 }
97                                                 return datetime.ToString (culture);
98                                         } else {
99                                                 if (datetime == datetime.Date) {
100                                                         return datetime.ToString (info.ShortDatePattern, culture);
101                                                 } else {
102                                                         return datetime.ToString (info.ShortDatePattern + " " + 
103                                                                 info.ShortTimePattern, culture);
104                                                 }
105                                         }
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 });
109                                 }
110                         }
111                         return base.ConvertTo (context, culture, value, destinationType);
112                 }
113         }
114 }