2005-05-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / DateType.cs
1 //
2 // DateType.cs
3 //
4 //      Author:
5 //      Chris J Breisch (cjbreisch@altavista.net) 
6 //      Francesco Delfino (pluto@tipic.com)
7 //      Dennis Hayes (dennish@raytek.com)
8 //
9 //      (C) copyright 2002 Chris J Breisch
10 //      2002 Tipic, Inc (http://www.tipic.com)
11 //
12  /*
13   * Copyright (c) 2002-2003 Mainsoft Corporation.
14   * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15   *
16   * Permission is hereby granted, free of charge, to any person obtaining a
17   * copy of this software and associated documentation files (the "Software"),
18   * to deal in the Software without restriction, including without limitation
19   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20   * and/or sell copies of the Software, and to permit persons to whom the
21   * Software is furnished to do so, subject to the following conditions:
22   * 
23   * The above copyright notice and this permission notice shall be included in
24   * all copies or substantial portions of the Software.
25   * 
26   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32   * DEALINGS IN THE SOFTWARE.
33   */
34 /**
35  * Class that converts objects to DateTime object
36  */
37 using System;
38 using System.ComponentModel;
39
40 namespace Microsoft.VisualBasic.CompilerServices {
41         [StandardModule, EditorBrowsable(EditorBrowsableState.Never)] 
42         sealed public class DateType {
43                 private DateType () {}
44
45                 // Methods
46                 /**
47                   * The method converts given object to DateTime by the following logic:
48                   * 1. If input object is null - return null
49                   * 2. If input object is String - run FromString method
50                   * 3. If input object is DateTime - run ToDateTime on this value
51                   * 4. Otherwise throw InvalidCastException.
52                   * @exception InvalidCastException - if given object is not nulll, String
53                   *  or DateTime.
54                   * @param value - The object that going to be converted
55                   * @return DateTime The DateTime value that converted from the source object
56                   * @see system.Convert#ToDateTime
57                   */
58                 public static DateTime FromObject (object Value) { 
59                         //if (Value == null)return null;//per Mainsoft code.
60                         if (Value == null)return DateTime.MinValue; //Can't return null for datetime type
61
62                         if (Value is string) return FromString((string)Value);
63
64                         if(Value is DateTime)return (DateTime)Value;
65                         throw new InvalidCastException("InvalidCast_From " + Value.GetType().Name + " ToDate");
66                 }
67
68                 /**
69                  * The method converts given string to DateTime using current CultureInfo.
70                  * @param value The value to convert.
71                  * @return DateTime The value that extracted from the input string.
72                  */
73                 public static System.DateTime FromString (string Value) {
74                         return FromString(Value, System.Globalization.CultureInfo.CurrentCulture);
75                 }
76
77                 /**
78                  * The method try to convert given string to DateTime by calling
79                  * DateTime.Parse.
80                  * @exception InvalidCastException - in case if date translation failed
81                  *  due to any Exception.
82                  * @param value - The string that converted to DateTime
83                  * @return DateTime The value that extracted from the input string.
84                  */
85                 public static System.DateTime FromString (string Value, System.Globalization.CultureInfo culture) { 
86                         string val = Value;
87                         if (Value != null
88                             && Value.Length > 2
89                             && Value.StartsWith("#")
90                             && Value.EndsWith("#"))
91                             val = Value.Substring(1, Value.Length - 1);
92                         // 15 = DateTymeStyles.AllowWhiteSpaces || DateTymeStyles.NoCurrentDateDefault
93                         return DateTime.Parse(val, culture,(System.Globalization.DateTimeStyles)15);
94                 }
95         }
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113