Add licensing info
[mono.git] / mcs / class / Mono.Data.SybaseClient / Mono.Data.SybaseTypes / SybaseDateTime.cs
1 //
2 // Mono.Data.SybaseTypes.SybaseDateTime
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //
7 // Based on System.Data.SqlTypes.SqlDateTime
8 //
9 // (C) Ximian, Inc. 2002-2003
10 // (C) Copyright Tim Coleman, 2002-2003
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using Mono.Data.SybaseClient;
35 using System;
36 using System.Data.SqlTypes;
37 using System.Globalization;
38
39 namespace Mono.Data.SybaseTypes {
40         public struct SybaseDateTime : INullable, IComparable
41         {
42                 #region Fields
43                 private DateTime value;
44                 private bool notNull;
45                 private static readonly float DateTimeTicksPerHour = 3.6E+10f;
46                 private static readonly DateTime Epoch = new DateTime (1900, 1, 1);
47
48                 public static readonly SybaseDateTime MaxValue = new SybaseDateTime (9999, 12, 31, 23, 59, 59);
49                 public static readonly SybaseDateTime MinValue = new SybaseDateTime (1753,1,1);
50                 public static readonly SybaseDateTime Null;
51                 public static readonly int SQLTicksPerHour = 1080000;
52                 public static readonly int SQLTicksPerMinute = 18000;
53                 public static readonly int SQLTicksPerSecond = 300;
54
55                 #endregion
56
57                 #region Constructors
58
59                 public SybaseDateTime (DateTime value) 
60                 {
61                         this.value = value;
62                         notNull = true;
63                 }
64
65                 public SybaseDateTime (int dayTicks, int timeTicks) 
66                 {
67                         this.value = new DateTime (Epoch.Ticks + (long) (dayTicks + timeTicks));
68                         notNull = true;
69                 }
70
71                 public SybaseDateTime (int year, int month, int day) 
72                 {
73                         this.value = new DateTime (year, month, day);
74                         notNull = true;
75                 }
76
77                 public SybaseDateTime (int year, int month, int day, int hour, int minute, int second) 
78                 {
79                         this.value = new DateTime (year, month, day, hour, minute, second);
80                         notNull = true;
81                 }
82
83                 public SybaseDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond) 
84                 {
85                         DateTime t = new DateTime (year, month, day);
86                         this.value = new DateTime ((long) (t.Day * 24 * SQLTicksPerHour + hour * SQLTicksPerHour + minute * SQLTicksPerMinute + second * SQLTicksPerSecond + millisecond * 1000));
87                         notNull = true;
88                 }
89
90                 public SybaseDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond) 
91                 {
92                         DateTime t = new DateTime (year, month, day);
93                         this.value = new DateTime ((long) (t.Day * 24 * SQLTicksPerHour + hour * SQLTicksPerHour + minute * SQLTicksPerMinute + second * SQLTicksPerSecond + bilisecond));
94                         notNull = true;
95                 }
96
97                 #endregion
98
99                 #region Properties
100
101                 public int DayTicks {
102                         get { 
103                                 return (int) ((this.Value.Ticks - Epoch.Ticks) / (24 * DateTimeTicksPerHour));
104                         }
105                 }
106
107                 public bool IsNull { 
108                         get { return !notNull; }
109                 }
110
111                 public int TimeTicks {
112                         get { 
113                                 if (this.IsNull)
114                                         throw new SybaseNullValueException ();
115                                 return (int) (value.Hour * SQLTicksPerHour + value.Minute * SQLTicksPerMinute + value.Second * SQLTicksPerSecond + value.Millisecond);
116                         }
117                 }
118
119                 public DateTime Value {
120                         get { 
121                                 if (this.IsNull) 
122                                         throw new SybaseNullValueException ("The property contains Null.");
123                                 else 
124                                         return value; 
125                         }
126                 }
127
128                 #endregion
129
130                 #region Methods
131
132                 public int CompareTo (object value)
133                 {
134                         if (value == null)
135                                 return 1;
136                         else if (!(value is SybaseDateTime))
137                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SybaseTypes.SybaseDateTime"));
138                         else if (((SybaseDateTime)value).IsNull)
139                                 return 1;
140                         else
141                                 return this.value.CompareTo (((SybaseDateTime)value).Value);
142                 }
143
144                 public override bool Equals (object value)
145                 {
146                         if (!(value is SybaseDateTime))
147                                 return false;
148                         else
149                                 return (bool) (this == (SybaseDateTime)value);
150                 }
151
152                 public static SybaseBoolean Equals (SybaseDateTime x, SybaseDateTime y)
153                 {
154                         return (x == y);
155                 }
156
157                 public override int GetHashCode ()
158                 {
159                         return value.GetHashCode ();
160                 }
161
162                 public static SybaseBoolean GreaterThan (SybaseDateTime x, SybaseDateTime y)
163                 {
164                         return (x > y);
165                 }
166
167                 public static SybaseBoolean GreaterThanOrEqual (SybaseDateTime x, SybaseDateTime y)
168                 {
169                         return (x >= y);
170                 }
171
172                 public static SybaseBoolean LessThan (SybaseDateTime x, SybaseDateTime y)
173                 {
174                         return (x < y);
175                 }
176
177                 public static SybaseBoolean LessThanOrEqual (SybaseDateTime x, SybaseDateTime y)
178                 {
179                         return (x <= y);
180                 }
181
182                 public static SybaseBoolean NotEquals (SybaseDateTime x, SybaseDateTime y)
183                 {
184                         return (x != y);
185                 }
186
187                 public static SybaseDateTime Parse (string s)
188                 {
189                         return new SybaseDateTime (DateTime.Parse (s));
190                 }
191
192                 public SybaseString ToSybaseString ()
193                 {
194                         return ((SybaseString)this);
195                 }
196
197                 public override string ToString ()
198                 {       
199                         if (this.IsNull)
200                                 return String.Empty;
201                         else
202                                 return value.ToString ();
203                 }
204         
205                 public static SybaseDateTime operator + (SybaseDateTime x, TimeSpan t)
206                 {
207                         if (x.IsNull)
208                                 return SybaseDateTime.Null;
209                         return new SybaseDateTime (x.Value + t);
210                 }
211
212                 public static SybaseBoolean operator == (SybaseDateTime x, SybaseDateTime y)
213                 {
214                         if (x.IsNull || y.IsNull) 
215                                 return SybaseBoolean.Null;
216                         else
217                                 return new SybaseBoolean (x.Value == y.Value);
218                 }
219
220                 public static SybaseBoolean operator > (SybaseDateTime x, SybaseDateTime y)
221                 {
222                         if (x.IsNull || y.IsNull) 
223                                 return SybaseBoolean.Null;
224                         else
225                                 return new SybaseBoolean (x.Value > y.Value);
226                 }
227
228                 public static SybaseBoolean operator >= (SybaseDateTime x, SybaseDateTime y)
229                 {
230                         if (x.IsNull || y.IsNull) 
231                                 return SybaseBoolean.Null;
232                         else
233                                 return new SybaseBoolean (x.Value >= y.Value);
234                 }
235
236                 public static SybaseBoolean operator != (SybaseDateTime x, SybaseDateTime y)
237                 {
238                         if (x.IsNull || y.IsNull) 
239                                 return SybaseBoolean.Null;
240                         else
241                                 return new SybaseBoolean (!(x.Value == y.Value));
242                 }
243
244                 public static SybaseBoolean operator < (SybaseDateTime x, SybaseDateTime y)
245                 {
246                         if (x.IsNull || y.IsNull) 
247                                 return SybaseBoolean.Null;
248                         else
249                                 return new SybaseBoolean (x.Value < y.Value);
250                 }
251
252                 public static SybaseBoolean operator <= (SybaseDateTime x, SybaseDateTime y)
253                 {
254                         if (x.IsNull || y.IsNull) 
255                                 return SybaseBoolean.Null;
256                         else
257                                 return new SybaseBoolean (x.Value <= y.Value);
258                 }
259
260                 public static SybaseDateTime operator - (SybaseDateTime x, TimeSpan t)
261                 {
262                         if (x.IsNull)
263                                 return SybaseDateTime.Null;
264                         return new SybaseDateTime (x.Value - t);
265                 }
266
267                 public static explicit operator DateTime (SybaseDateTime x)
268                 {
269                         return x.Value;
270                 }
271
272                 public static explicit operator SybaseDateTime (SybaseString x)
273                 {
274                         return SybaseDateTime.Parse (x.Value);
275                 }
276
277                 public static implicit operator SybaseDateTime (DateTime x)
278                 {
279                         return new SybaseDateTime (x);
280                 }
281
282                 #endregion
283         }
284 }
285