5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Francesco Delfino (pluto@tipic.com)
7 // Dennis Hayes (dennish@raytek.com)
9 // (C) copyright 2002 Chris J Breisch
10 // 2002 Tipic, Inc (http://www.tipic.com)
13 * Copyright (c) 2002-2003 Mainsoft Corporation.
14 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
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.
35 * Class that converts objects to DateTime object
38 using System.ComponentModel;
40 namespace Microsoft.VisualBasic.CompilerServices {
41 [StandardModule, EditorBrowsable(EditorBrowsableState.Never)]
42 sealed public class DateType {
43 private DateType () {}
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
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
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
62 if (Value is string) return FromString((string)Value);
64 if(Value is DateTime)return (DateTime)Value;
65 throw new InvalidCastException("InvalidCast_From " + Value.GetType().Name + " ToDate");
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.
73 public static System.DateTime FromString (string Value) {
74 return FromString(Value, System.Globalization.CultureInfo.CurrentCulture);
78 * The method try to convert given string to DateTime by calling
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.
85 public static System.DateTime FromString (string Value, System.Globalization.CultureInfo culture) {
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);