2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes.jvm / SqlDateTime.cs
1 // System.Data.SqlTypes.SqlDateTime\r
2 //
3 // Authors:
4 //      Konstantin Triger <kostat@mainsoft.com>
5 //      Boris Kirzner <borisk@mainsoft.com>
6 //      
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //\r
30 \r
31 namespace System.Data.SqlTypes\r
32 {\r
33 \r
34     /**\r
35      * <p>Title: </p>\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
40      * @version 1.0\r
41      */\r
42 \r
43     using System;\r
44 \r
45 \r
46     /*\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
50     */\r
51 \r
52     public struct SqlDateTime : INullable, IComparable\r
53     {\r
54         private DateTime _value;\r
55         private bool _isNull;\r
56 \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
63         \r
64         private SqlDateTime(bool isNull)\r
65         {\r
66             _isNull = isNull;\r
67             _value = DateTime.MinValue;\r
68         }\r
69 \r
70         public SqlDateTime(DateTime value) \r
71         {\r
72             _value = value;\r
73             _isNull = false;\r
74         }\r
75 \r
76         public SqlDateTime(int DateTicks, int TimeTicks)\r
77         {\r
78             throw new NotImplementedException("ctor SqlDateTime(int DateTicks, int TimeTicks) not implemented.");\r
79         }\r
80 \r
81         /**\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
86          */\r
87         public SqlDateTime(int year, int month, int day) \r
88         : this(year, month, day, 0, 0, 0, 0)\r
89         {\r
90         }\r
91 \r
92         /**\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
98          */\r
99         public SqlDateTime(int year, int month, int day, int hour)\r
100             : this(year, month, day, hour, 0, 0, 0)\r
101         {\r
102         }\r
103 \r
104         /**\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
111          */\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
114         { \r
115         }\r
116         /**\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
124          */\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
127         {\r
128         }\r
129 \r
130         /**\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
139          */\r
140         public SqlDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)\r
141         {\r
142             _value = new DateTime(year, month, day, hour, minute, second, millisecond);\r
143             _isNull = false;\r
144         }\r
145 \r
146         /**\r
147          * Indicates whether or not Value is null.\r
148          * @return true if Value is null, otherwise false.\r
149          */\r
150         public bool IsNull\r
151         {\r
152             get\r
153             {\r
154                 return _isNull;\r
155             }\r
156         }\r
157 \r
158         /**\r
159          * Gets the value of the SqlDateTime instance.\r
160          * @return the value of this instance\r
161          */\r
162         public DateTime Value\r
163         {\r
164             get\r
165             {\r
166                 if(IsNull)\r
167                     throw new SqlNullValueException();\r
168                 return _value;\r
169             }\r
170         }\r
171 \r
172         public int DayTicks\r
173         {\r
174             get\r
175             {\r
176                 /** @todo find the logic */\r
177                 return -1;\r
178             }\r
179         }\r
180 \r
181         /**\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
184          */\r
185         public int TimeTicks\r
186         {\r
187             get\r
188             {\r
189                 if (IsNull)\r
190                 {\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
195 \r
196                     return res;\r
197 \r
198                 }\r
199 \r
200                 throw new SqlNullValueException("The value of this instance is null");\r
201             }\r
202         }\r
203 \r
204         /**\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
212          */\r
213         public int CompareTo(Object obj)\r
214         {\r
215             if (obj == null)\r
216                 return 1;\r
217 \r
218             if (obj is SqlDateTime)\r
219             {\r
220                 SqlDateTime value = (SqlDateTime)obj;\r
221 \r
222                 if (value.IsNull)\r
223                     return 1;\r
224                 if (IsNull)\r
225                     return -1;\r
226 \r
227                 return _value.CompareTo(value._value);\r
228             }\r
229 \r
230             throw new ArgumentException("parameter obj is not SqlDateTime : " + obj.GetType().Name);\r
231 \r
232         }\r
233 \r
234         public override bool Equals(Object obj)\r
235         {\r
236             if (obj is SqlDateTime)\r
237             {\r
238                 SqlDateTime d = (SqlDateTime)obj;\r
239                 \r
240                 if (d.IsNull || d.IsNull)\r
241                     return false;\r
242 \r
243                 if (d._value  == _value)\r
244                     return true;\r
245 \r
246                 return _value.Equals(d._value);\r
247             }\r
248 \r
249             return false;\r
250         }\r
251 \r
252         \r
253         /**\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
259          */\r
260         public static SqlBoolean Equals(SqlDateTime d1, SqlDateTime d2)\r
261         {\r
262             return d1 == d2;\r
263         }\r
264 \r
265         \r
266         /**\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
272          */\r
273         public static SqlBoolean GreaterThan(SqlDateTime d1, SqlDateTime d2)\r
274         {\r
275             return d1 > d2;\r
276         }\r
277 \r
278         /**\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
284          */\r
285         public static SqlBoolean GreaterThanOrEqual(SqlDateTime d1, SqlDateTime d2)\r
286         {\r
287             return d1 >= d2;\r
288         }\r
289 \r
290         /**\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
296          */\r
297         public static SqlBoolean LessThan(SqlDateTime d1, SqlDateTime d2)\r
298         {\r
299             return d1 < d2;\r
300         }\r
301 \r
302         /**\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
308          */\r
309         public static SqlBoolean LessThanOrEqual(SqlDateTime d1, SqlDateTime d2)\r
310         {\r
311             return d1 <= d2;\r
312         }\r
313 \r
314         /**\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
320          */\r
321         public static SqlBoolean NotEquals(SqlDateTime d1, SqlDateTime d2)\r
322         {\r
323             return d1 != d2;\r
324         }\r
325 \r
326         /**\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
330          */\r
331         public static SqlDateTime Parse(String s)\r
332         {\r
333             DateTime val = DateTime.Parse(s);\r
334 \r
335             SqlDateTime res = new SqlDateTime(val);\r
336 \r
337             if (LessThanOrEqual(res, MinValue).IsTrue || GreaterThanOrEqual(res, MinValue).IsTrue)\r
338                 throw new ArgumentException("The DateTime is not valid");\r
339 \r
340             return res;\r
341 \r
342         }\r
343 \r
344 \r
345         public override String ToString()\r
346         {\r
347             if (IsNull)\r
348                 return "null";\r
349 \r
350             return _value.ToString();\r
351         }\r
352 \r
353         /**\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
356          */\r
357         public SqlString ToSqlString()\r
358         {\r
359             return new SqlString(ToString());\r
360         }\r
361         \r
362         public override int GetHashCode()\r
363         {\r
364             return _value.GetHashCode();\r
365         }\r
366 \r
367         public static SqlDateTime operator + (SqlDateTime x, TimeSpan t)\r
368         {\r
369             if (x.IsNull)\r
370                 return SqlDateTime.Null;\r
371                         \r
372             return new SqlDateTime (x.Value + t);\r
373         }\r
374 \r
375         public static SqlBoolean operator == (SqlDateTime x, SqlDateTime y)\r
376         {\r
377             if (x.IsNull || y.IsNull) \r
378                 return SqlBoolean.Null;\r
379             else\r
380                 return new SqlBoolean (x.Value == y.Value);\r
381         }\r
382 \r
383         public static SqlBoolean operator > (SqlDateTime x, SqlDateTime y)\r
384         {\r
385             if (x.IsNull || y.IsNull) \r
386                 return SqlBoolean.Null;\r
387             else\r
388                 return new SqlBoolean(x.CompareTo(y) > 0);\r
389         }\r
390 \r
391         public static SqlBoolean operator >= (SqlDateTime x, SqlDateTime y)\r
392         {\r
393             if (x.IsNull || y.IsNull) \r
394                 return SqlBoolean.Null;\r
395             else\r
396                 return new SqlBoolean(x.CompareTo(y) >= 0);\r
397         }\r
398 \r
399         public static SqlBoolean operator != (SqlDateTime x, SqlDateTime y)\r
400         {\r
401             if (x.IsNull || y.IsNull) \r
402                 return SqlBoolean.Null;\r
403             else\r
404                 return new SqlBoolean(!(x.Value == y.Value));\r
405         }\r
406 \r
407         public static SqlBoolean operator < (SqlDateTime x, SqlDateTime y)\r
408         {\r
409             if (x.IsNull || y.IsNull) \r
410                 return SqlBoolean.Null;\r
411             else\r
412                 return new SqlBoolean(x.CompareTo(y) < 0);\r
413         }\r
414 \r
415         public static SqlBoolean operator <= (SqlDateTime x, SqlDateTime y)\r
416         {\r
417             if (x.IsNull || y.IsNull) \r
418                 return SqlBoolean.Null;\r
419             else\r
420                 return new SqlBoolean(x.CompareTo(y) <= 0);\r
421         }\r
422 \r
423         public static SqlDateTime operator - (SqlDateTime x, TimeSpan t)\r
424         {\r
425             return new SqlDateTime(x.Value - t);\r
426         }\r
427 \r
428         public static explicit operator DateTime (SqlDateTime x)\r
429         {\r
430             return x.Value;\r
431         }\r
432 \r
433         public static explicit operator SqlDateTime (SqlString x)\r
434         {\r
435             return SqlDateTime.Parse(x.Value);\r
436         }\r
437 \r
438         public static implicit operator SqlDateTime (DateTime x)\r
439         {\r
440             return new SqlDateTime(x);\r
441         }\r
442 \r
443     }\r
444 }\r