New test.
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OracleDateTime.cs
1 //
2 // OracleDateTime.cs 
3 //
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
6 //
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
9 //
10 // Authors: Tim Coleman <tim@timcoleman.com>
11 //          Daniel Morgan <danielmorgan@verizon.net>
12 //
13 // Copyright (C) Tim Coleman, 2003
14 // Copyright (C) Daniel Morgan, 2005
15 //
16 // Licensed under the MIT/X11 License.
17 //
18
19 using System;
20 using System.Data.SqlTypes;
21 using System.Globalization;
22 using System.Text;
23
24 namespace System.Data.OracleClient {
25         public struct OracleDateTime : IComparable, INullable
26         {
27                 #region Fields
28
29                 public static readonly OracleDateTime MaxValue = new OracleDateTime (4712, 12, 31);
30                 public static readonly OracleDateTime MinValue = new OracleDateTime (1, 1, 1);
31                 public static readonly OracleDateTime Null = new OracleDateTime ();
32
33                 DateTime value;
34                 bool notNull;
35
36                 #endregion // Fields
37
38                 #region Constructors
39
40                 public OracleDateTime (DateTime dt)
41                 {
42                         value = dt; 
43                         notNull = true;
44                 }
45
46                 public OracleDateTime (long ticks)
47                         : this (new DateTime (ticks))
48                 {
49                 }
50
51                 public OracleDateTime (OracleDateTime from)
52                         : this (from.Value)
53                 {
54                 }
55
56                 public OracleDateTime (int year, int month, int day)
57                         : this (new DateTime (year, month, day))
58                 {
59                 }
60
61                 public OracleDateTime (int year, int month, int day, Calendar calendar)
62                         : this (new DateTime (year, month, day, calendar))
63                 {
64                 }
65
66                 public OracleDateTime (int year, int month, int day, int hour, int minute, int second)
67                         : this (new DateTime (year, month, day, hour, minute, second))
68                 {
69                 }
70
71                 public OracleDateTime (int year, int month, int day, int hour, int minute, int second, Calendar calendar)
72                         : this (new DateTime (year, month, day, hour, minute, second, calendar))
73                 {
74                 }
75
76                 public OracleDateTime (int year, int month, int day, int hour, int minute, int second, int millisecond)
77                         : this (new DateTime (year, month, day, hour, minute, second, millisecond))
78                 {
79                 }
80
81                 public OracleDateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar)
82                         : this (new DateTime (year, month, day, hour, minute, second, millisecond, calendar))
83                 {
84                 }
85
86                 #endregion // Constructors
87
88                 #region Properties
89
90                 public int Day {
91                         get { return value.Day; }
92                 }
93
94                 public int Hour {
95                         get { return value.Hour; }
96                 }
97
98                 public bool IsNull {
99                         get { return !notNull; }
100                 }
101
102                 public int Millisecond {
103                         get { return value.Millisecond; }
104                 }
105
106                 public int Minute {
107                         get { return value.Minute; }
108                 }
109
110                 public int Month {
111                         get { return value.Month; }
112                 }
113
114                 public int Second {
115                         get { return value.Second; }
116                 }
117
118                 public DateTime Value {
119                         get { return value; }
120                 }
121
122                 public int Year {
123                         get { return value.Year; }
124                 }
125
126                 #endregion // Properties
127
128                 #region Methods
129
130                 [MonoTODO]
131                 public int CompareTo (object obj)
132                 {
133                         throw new NotImplementedException ();
134                 }
135
136                 [MonoTODO]
137                 public override bool Equals (object value)
138                 {
139                         throw new NotImplementedException ();
140                 }
141
142                 public static OracleBoolean Equals (OracleDateTime x, OracleDateTime y)
143                 {
144                         if (x.IsNull || y.IsNull)
145                                 return OracleBoolean.Null;
146                         return new OracleBoolean (x.Value == y.Value);
147                 }
148
149                 [MonoTODO]
150                 public override int GetHashCode ()
151                 {
152                         throw new NotImplementedException ();
153                 }
154
155                 public static OracleBoolean GreaterThan (OracleDateTime x, OracleDateTime y)
156                 {
157                         if (x.IsNull || y.IsNull)
158                                 return OracleBoolean.Null;
159                         return new OracleBoolean (x.Value > y.Value);
160                 }
161
162                 public static OracleBoolean GreaterThanOrEqual (OracleDateTime x, OracleDateTime y)
163                 {
164                         if (x.IsNull || y.IsNull)
165                                 return OracleBoolean.Null;
166                         return new OracleBoolean (x.Value >= y.Value);
167                 }
168
169                 public static OracleBoolean LessThan (OracleDateTime x, OracleDateTime y)
170                 {
171                         if (x.IsNull || y.IsNull)
172                                 return OracleBoolean.Null;
173                         return new OracleBoolean (x.Value < y.Value);
174                 }
175
176                 public static OracleBoolean LessThanOrEqual (OracleDateTime x, OracleDateTime y)
177                 {
178                         if (x.IsNull || y.IsNull)
179                                 return OracleBoolean.Null;
180                         return new OracleBoolean (x.Value <= y.Value);
181                 }
182
183                 public static OracleBoolean NotEquals (OracleDateTime x, OracleDateTime y)
184                 {
185                         if (x.IsNull || y.IsNull)
186                                 return OracleBoolean.Null;
187                         return new OracleBoolean (x.Value != y.Value);
188                 }
189
190                 public static OracleDateTime Parse (string s)
191                 {
192                         return new OracleDateTime (DateTime.Parse (s));
193                 }
194
195                 public override string ToString ()
196                 {
197                         if (IsNull)
198                                 return "Null";
199                         return Value.ToString ();
200                 }
201
202                 #endregion // Methods
203
204                 #region Operators and Type Conversions
205
206                 public static OracleBoolean operator == (OracleDateTime x, OracleDateTime y)
207                 {
208                         return Equals (x, y);
209                 }
210
211                 public static OracleBoolean operator > (OracleDateTime x, OracleDateTime y)
212                 {
213                         return GreaterThan (x, y);
214                 }
215
216                 public static OracleBoolean operator >= (OracleDateTime x, OracleDateTime y)
217                 {
218                         return GreaterThanOrEqual (x, y);
219                 }
220
221                 public static OracleBoolean operator != (OracleDateTime x, OracleDateTime y)
222                 {
223                         return NotEquals (x, y);
224                 }
225
226                 public static OracleBoolean operator < (OracleDateTime x, OracleDateTime y)
227                 {
228                         return LessThan (x, y);
229                 }
230
231                 public static OracleBoolean operator <= (OracleDateTime x, OracleDateTime y)
232                 {
233                         return LessThanOrEqual (x, y);
234                 }
235
236                 public static explicit operator DateTime (OracleDateTime x)
237                 {
238                         return x.Value;
239                 }
240
241                 public static explicit operator OracleDateTime (string x)
242                 {
243                         return new OracleDateTime (DateTime.Parse (x));
244                 }
245
246                 private static string ConvertSystemDatePartToOracleDate (string sysPart) 
247                 {
248                         if (sysPart.Length == 0)
249                                 return "";
250                         else {
251                                 switch (sysPart) {
252                                 case "tt":  
253                                         return "AM";
254                                 case "yy":
255                                         return "YY";
256                                 case "yyyy":
257                                         return "YYYY";
258                                 case "MM":
259                                         return "MM";
260                                 case "MON":
261                                         return "MMM";
262                                 case "MMMM":
263                                         return "MONTH";
264                                 case "dd":
265                                         return "DD";
266                                 case "ddd":
267                                         return "DY";
268                                 case "dddd":
269                                         return "DAY";
270                                 case "hh":
271                                         return "HH";
272                                 case "HH":
273                                         return "HH24";
274                                 case "mm": 
275                                         return "MI";
276                                 case "ss":
277                                         return "SS";
278                                 default:
279                                         // ignore any others?
280                                         return "";
281                                 }
282                         }
283                 }
284
285                 private static string ConvertOracleDatePartToSystemDatePart (string oraPart) 
286                 {
287                         if (oraPart.Length == 0)
288                                 return "";
289                         else {
290                                 switch (oraPart) {
291                                 case "AM":  
292                                 case "PM":
293                                         // TODO: need to handle "A.M." and "P.M."
294                                         return "tt";
295                                 case "RR": // TODO: handle special year RR.  for now, treat it like yy
296                                         return "yy";
297                                 case "YY":
298                                         return "yy";
299                                 case "YYYY":
300                                         return "yyyy";
301                                 case "MM":
302                                         return "MM";
303                                 case "MON":
304                                         return "MMM";
305                                 case "MONTH":
306                                         return "MMMM";
307                                 case "DD":
308                                         return "dd";
309                                 case "DY":
310                                         return "ddd";
311                                 case "DAY":
312                                         return "dddd";
313                                 case "HH":
314                                 case "HH12":
315                                         return "hh";
316                                 case "HH24":
317                                         return "HH";
318                                 case "MI": 
319                                         return "mm";
320                                 case "SS":
321                                         return "ss";
322                                 default:
323                                         // ignore any others?
324                                         return "";
325                                 }
326                         }
327                 }
328
329                 internal static string ConvertSystemDateTimeFormatToOracleDate (string sysFormat) 
330                 {
331                         if (sysFormat == String.Empty)
332                                 return String.Empty;
333
334                         char[] chars = sysFormat.ToCharArray ();
335
336                         StringBuilder sb = new StringBuilder ();
337                         StringBuilder sfinal = new StringBuilder ();
338
339                         int i = 0;
340                         bool inQuote = false;
341                         char quoteChar = '\"';
342                         string sPart;
343                 
344                         for (i = 0; i < chars.Length; i++) {
345                                 char ch = chars[i];
346                                 
347                                 if (inQuote == true) {
348                                         sfinal.Append (ch);
349                                         if (ch == quoteChar)
350                                                 inQuote = false;
351                                 }
352                                 else {
353                                         switch (ch) {
354                                         case ' ':
355                                         case '.':
356                                         case ',':
357                                         case '/':
358                                         case '-':
359                                         case ':':
360                                                 if (sb.Length > 0) {
361                                                         sPart = ConvertSystemDatePartToOracleDate (sb.ToString ());
362                                                         if (sPart.Length > 0)
363                                                                 sfinal.Append (sPart);
364                                                 }
365                                                 sb = null;
366                                                 sb = new StringBuilder ();
367                                                 sfinal.Append (ch);
368                                                 break;
369                                         case '\"':
370                                                 sfinal.Append (ch);
371                                                 inQuote = true;
372                                                 quoteChar = '\"';
373                                                 break;
374                                         default:
375                                                 sb.Append (ch);
376                                                 break;
377                                         }
378                                 }
379                         }
380
381                         if (sb.Length > 0) {
382                                 sPart = ConvertSystemDatePartToOracleDate (sb.ToString ());
383                                 if (sPart.Length > 0)
384                                         sfinal.Append (sPart);
385                                 sb = null;
386                         }
387                         string returnStr = sfinal.ToString ();
388                         sfinal = null;
389                         return returnStr;
390                 }
391
392                 internal static string ConvertOracleDateFormatToSystemDateTime (string oraFormat) 
393                 {
394                         if (oraFormat == String.Empty)
395                                 return String.Empty;
396
397                         char[] chars = oraFormat.ToCharArray ();
398
399                         StringBuilder sb = new StringBuilder ();
400                         StringBuilder sfinal = new StringBuilder ();
401
402                         int i = 0;
403                         bool inQuote = false;
404                         char quoteChar = '\"';
405                         string sPart;
406                 
407                         for (i = 0; i < chars.Length; i++) {
408                                 char ch = chars[i];
409                                 
410                                 if (inQuote == true) {
411                                         sfinal.Append (ch);
412                                         if (ch == quoteChar)
413                                                 inQuote = false;
414                                 }
415                                 else {
416                                         switch (ch) {
417                                         case ' ':
418                                         case '.':
419                                         case ',':
420                                         case '/':
421                                         case '-':
422                                         case ':':
423                                                 if (sb.Length > 0) {
424                                                         sPart = ConvertOracleDatePartToSystemDatePart (sb.ToString ());
425                                                         if (sPart.Length > 0)
426                                                                 sfinal.Append (sPart);
427                                                 }
428                                                 sb = null;
429                                                 sb = new StringBuilder ();
430                                                 sfinal.Append (ch);
431                                                 break;
432                                         case '\"':
433                                                 sfinal.Append (ch);
434                                                 inQuote = true;
435                                                 quoteChar = '\"';
436                                                 break;
437                                         default:
438                                                 sb.Append (ch);
439                                                 break;
440                                         }
441                                 }
442                         }
443
444                         if (sb.Length > 0) {
445                                 sPart = ConvertOracleDatePartToSystemDatePart (sb.ToString ());
446                                 if (sPart.Length > 0)
447                                         sfinal.Append (sPart);
448                                 sb = null;
449                         }
450                         string returnStr = sfinal.ToString ();
451                         sfinal = null;
452                         return returnStr;
453                 }
454
455                 #endregion // Operators and Type Conversions
456         }
457 }
458