1 // System.Data.SqlTypes.SqlDateTime
\r
4 // Konstantin Triger <kostat@mainsoft.com>
5 // Boris Kirzner <borisk@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Data.SqlTypes
\r
36 * <p>Description: </p>
\r
37 * <p>Copyright: Copyright (c) 2002</p>
\r
38 * <p>Company: MainSoft</p>
\r
39 * @author Pavel Sandler
\r
47 * CURRENT LIMITATIONS:
\r
48 * 1. Doesn't support billiseconds
\r
49 * 2. public int DayTicks not implemented (I do not understand the logic)
\r
52 public struct SqlDateTime : INullable, IComparable
\r
54 private DateTime _value;
\r
55 private bool _isNull;
\r
57 public static readonly SqlDateTime MaxValue = new SqlDateTime(9999, 12, 31);
\r
58 public static readonly SqlDateTime MinValue = new SqlDateTime(1753, 1, 1);
\r
59 public static readonly SqlDateTime Null = new SqlDateTime(true);
\r
60 public static readonly int SqlTicksPerSecond = 300;
\r
61 public static readonly int SqlTicksPerMinute = 18000;
\r
62 public static readonly int SqlTicksPerHour = 1080000;
\r
64 private SqlDateTime(bool isNull)
\r
67 _value = DateTime.MinValue;
\r
70 public SqlDateTime(DateTime value)
\r
76 public SqlDateTime(int DateTicks, int TimeTicks)
\r
78 throw new NotImplementedException("ctor SqlDateTime(int DateTicks, int TimeTicks) not implemented.");
\r
82 * Constract a new SqlDateTime object
\r
83 * @param year the year
\r
84 * @param month the month (1-12)
\r
85 * @param day the day in the month
\r
87 public SqlDateTime(int year, int month, int day)
\r
88 : this(year, month, day, 0, 0, 0, 0)
\r
93 * Constract a new SqlDateTime object
\r
94 * @param year the year
\r
95 * @param month the month (1-12)
\r
96 * @param day the day in the month
\r
97 * @param hour the number of hours
\r
99 public SqlDateTime(int year, int month, int day, int hour)
\r
100 : this(year, month, day, hour, 0, 0, 0)
\r
105 * Constract a new SqlDateTime object
\r
106 * @param year the year
\r
107 * @param month the month (1-12)
\r
108 * @param day the day in the month
\r
109 * @param hour the number of hours
\r
110 * @param minute the number of minutes
\r
112 public SqlDateTime(int year, int month, int day, int hour, int minute)
\r
113 : this(year, month, day, hour, minute, 0, 0)
\r
117 * Constract a new SqlDateTime object
\r
118 * @param year the year
\r
119 * @param month the month (1-12)
\r
120 * @param day the day in the month
\r
121 * @param hour the number of hours
\r
122 * @param minute the number of minutes
\r
123 * @param second the number of seconds
\r
125 public SqlDateTime(int year, int month, int day, int hour, int minute, int second)
\r
126 : this(year, month, day, hour, minute, second, 0)
\r
131 * Constract a new SqlDateTime object
\r
132 * @param year the year
\r
133 * @param month the month (1-12)
\r
134 * @param day the day in the month
\r
135 * @param hour the number of hours
\r
136 * @param minute the number of minutes
\r
137 * @param second the number of seconds
\r
138 * @param millisecond the number of milliseconds
\r
140 public SqlDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
\r
142 _value = new DateTime(year, month, day, hour, minute, second, millisecond);
\r
147 * Indicates whether or not Value is null.
\r
148 * @return true if Value is null, otherwise false.
\r
159 * Gets the value of the SqlDateTime instance.
\r
160 * @return the value of this instance
\r
162 public DateTime Value
\r
167 throw new SqlNullValueException();
\r
172 public int DayTicks
\r
176 /** @todo find the logic */
\r
182 * Gets the number of ticks representing the time of this SqlDateTime structure.
\r
183 * @return The number of ticks representing the time of this SqlDateTime structure.
\r
185 public int TimeTicks
\r
191 int res = _value.Hour * SqlTicksPerHour;
\r
192 res += (_value.Minute * SqlTicksPerMinute);
\r
193 res += (_value.Second * SqlTicksPerSecond);
\r
194 res += (_value.Millisecond * SqlTicksPerSecond * 1000);
\r
200 throw new SqlNullValueException("The value of this instance is null");
\r
205 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
206 * @param obj The object to compare.
\r
207 * @return A signed number indicating the relative values of the instance and the object.
\r
208 * Less than zero This instance is less than object.
\r
209 * Zero This instance is the same as object.
\r
210 * Greater than zero This instance is greater than object -or-
\r
211 * object is a null reference.
\r
213 public int CompareTo(Object obj)
\r
218 if (obj is SqlDateTime)
\r
220 SqlDateTime value = (SqlDateTime)obj;
\r
227 return _value.CompareTo(value._value);
\r
230 throw new ArgumentException("parameter obj is not SqlDateTime : " + obj.GetType().Name);
\r
234 public override bool Equals(Object obj)
\r
236 if (obj is SqlDateTime)
\r
238 SqlDateTime d = (SqlDateTime)obj;
\r
240 if (d.IsNull || d.IsNull)
\r
243 if (d._value == _value)
\r
246 return _value.Equals(d._value);
\r
254 * Performs a logical comparison on two instances of SqlDateTime to determine if they are equal.
\r
255 * @param x A SqlDateTime instance.
\r
256 * @param y A SqlDateTime instance.
\r
257 * @return true if the two values are equal, otherwise false.
\r
258 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
260 public static SqlBoolean Equals(SqlDateTime d1, SqlDateTime d2)
\r
267 * Compares two instances of SqlDateTime to determine if the first is greater than the second.
\r
268 * @param x A SqlDateTime instance
\r
269 * @param y A SqlDateTime instance
\r
270 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
271 * If either instance of SqlDateTime is null, the Value of the SqlBoolean will be Null.
\r
273 public static SqlBoolean GreaterThan(SqlDateTime d1, SqlDateTime d2)
\r
279 * Compares two instances of SqlDateTime to determine if the first is greater than or equal to the second.
\r
280 * @param x A SqlDateTime instance
\r
281 * @param y A SqlDateTime instance
\r
282 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
283 * If either instance of SqlDateTime is null, the Value of the SqlBoolean will be Null.
\r
285 public static SqlBoolean GreaterThanOrEqual(SqlDateTime d1, SqlDateTime d2)
\r
291 * Compares two instances of SqlDecimal to determine if the first is less than the second.
\r
292 * @param x A SqlDateTime instance
\r
293 * @param y A SqlDateTime instance
\r
294 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
295 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
297 public static SqlBoolean LessThan(SqlDateTime d1, SqlDateTime d2)
\r
303 * Compares two instances of SqlDateTime to determine if the first is less than the second.
\r
304 * @param x A SqlDateTime instance
\r
305 * @param y A SqlDateTime instance
\r
306 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
307 * If either instance of SqlDateTime is null, the Value of the SqlBoolean will be Null.
\r
309 public static SqlBoolean LessThanOrEqual(SqlDateTime d1, SqlDateTime d2)
\r
315 * Compares two instances of SqlDateTime to determine if they are equal.
\r
316 * @param x A SqlDateTime instance
\r
317 * @param y A SqlDateTime instance
\r
318 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
319 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
321 public static SqlBoolean NotEquals(SqlDateTime d1, SqlDateTime d2)
\r
327 * Converts the String representation of a number to its DateTime equivalent.
\r
328 * @param s The String to be parsed.
\r
329 * @return A SqlDateTime containing the value represented by the String.
\r
331 public static SqlDateTime Parse(String s)
\r
333 DateTime val = DateTime.Parse(s);
\r
335 SqlDateTime res = new SqlDateTime(val);
\r
337 if (LessThanOrEqual(res, MinValue).IsTrue || GreaterThanOrEqual(res, MinValue).IsTrue)
\r
338 throw new ArgumentException("The DateTime is not valid");
\r
345 public override String ToString()
\r
350 return _value.ToString();
\r
354 * Converts this SqlDateTime structure to SqlString.
\r
355 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlDateTime structure.
\r
357 public SqlString ToSqlString()
\r
359 return new SqlString(ToString());
\r
362 public override int GetHashCode()
\r
364 return _value.GetHashCode();
\r
367 public static SqlDateTime operator + (SqlDateTime x, TimeSpan t)
\r
370 return SqlDateTime.Null;
\r
372 return new SqlDateTime (x.Value + t);
\r
375 public static SqlBoolean operator == (SqlDateTime x, SqlDateTime y)
\r
377 if (x.IsNull || y.IsNull)
\r
378 return SqlBoolean.Null;
\r
380 return new SqlBoolean (x.Value == y.Value);
\r
383 public static SqlBoolean operator > (SqlDateTime x, SqlDateTime y)
\r
385 if (x.IsNull || y.IsNull)
\r
386 return SqlBoolean.Null;
\r
388 return new SqlBoolean(x.CompareTo(y) > 0);
\r
391 public static SqlBoolean operator >= (SqlDateTime x, SqlDateTime y)
\r
393 if (x.IsNull || y.IsNull)
\r
394 return SqlBoolean.Null;
\r
396 return new SqlBoolean(x.CompareTo(y) >= 0);
\r
399 public static SqlBoolean operator != (SqlDateTime x, SqlDateTime y)
\r
401 if (x.IsNull || y.IsNull)
\r
402 return SqlBoolean.Null;
\r
404 return new SqlBoolean(!(x.Value == y.Value));
\r
407 public static SqlBoolean operator < (SqlDateTime x, SqlDateTime y)
\r
409 if (x.IsNull || y.IsNull)
\r
410 return SqlBoolean.Null;
\r
412 return new SqlBoolean(x.CompareTo(y) < 0);
\r
415 public static SqlBoolean operator <= (SqlDateTime x, SqlDateTime y)
\r
417 if (x.IsNull || y.IsNull)
\r
418 return SqlBoolean.Null;
\r
420 return new SqlBoolean(x.CompareTo(y) <= 0);
\r
423 public static SqlDateTime operator - (SqlDateTime x, TimeSpan t)
\r
425 return new SqlDateTime(x.Value - t);
\r
428 public static explicit operator DateTime (SqlDateTime x)
\r
433 public static explicit operator SqlDateTime (SqlString x)
\r
435 return SqlDateTime.Parse(x.Value);
\r
438 public static implicit operator SqlDateTime (DateTime x)
\r
440 return new SqlDateTime(x);
\r