2 // Mono.Data.SybaseTypes.SybaseDateTime
5 // Tim Coleman <tim@timcoleman.com>
7 // Based on System.Data.SqlTypes.SqlDateTime
9 // (C) Ximian, Inc. 2002-2003
10 // (C) Copyright Tim Coleman, 2002-2003
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using Mono.Data.SybaseClient;
36 using System.Data.SqlTypes;
37 using System.Globalization;
39 namespace Mono.Data.SybaseTypes {
40 public struct SybaseDateTime : INullable, IComparable
43 private DateTime value;
45 private static readonly float DateTimeTicksPerHour = 3.6E+10f;
46 private static readonly DateTime Epoch = new DateTime (1900, 1, 1);
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;
59 public SybaseDateTime (DateTime value)
65 public SybaseDateTime (int dayTicks, int timeTicks)
67 this.value = new DateTime (Epoch.Ticks + (long) (dayTicks + timeTicks));
71 public SybaseDateTime (int year, int month, int day)
73 this.value = new DateTime (year, month, day);
77 public SybaseDateTime (int year, int month, int day, int hour, int minute, int second)
79 this.value = new DateTime (year, month, day, hour, minute, second);
83 public SybaseDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond)
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));
90 public SybaseDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond)
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));
101 public int DayTicks {
103 return (int) ((this.Value.Ticks - Epoch.Ticks) / (24 * DateTimeTicksPerHour));
108 get { return !notNull; }
111 public int TimeTicks {
114 throw new SybaseNullValueException ();
115 return (int) (value.Hour * SQLTicksPerHour + value.Minute * SQLTicksPerMinute + value.Second * SQLTicksPerSecond + value.Millisecond);
119 public DateTime Value {
122 throw new SybaseNullValueException ("The property contains Null.");
132 public int CompareTo (object value)
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)
141 return this.value.CompareTo (((SybaseDateTime)value).Value);
144 public override bool Equals (object value)
146 if (!(value is SybaseDateTime))
149 return (bool) (this == (SybaseDateTime)value);
152 public static SybaseBoolean Equals (SybaseDateTime x, SybaseDateTime y)
157 public override int GetHashCode ()
159 return value.GetHashCode ();
162 public static SybaseBoolean GreaterThan (SybaseDateTime x, SybaseDateTime y)
167 public static SybaseBoolean GreaterThanOrEqual (SybaseDateTime x, SybaseDateTime y)
172 public static SybaseBoolean LessThan (SybaseDateTime x, SybaseDateTime y)
177 public static SybaseBoolean LessThanOrEqual (SybaseDateTime x, SybaseDateTime y)
182 public static SybaseBoolean NotEquals (SybaseDateTime x, SybaseDateTime y)
187 public static SybaseDateTime Parse (string s)
189 return new SybaseDateTime (DateTime.Parse (s));
192 public SybaseString ToSybaseString ()
194 return ((SybaseString)this);
197 public override string ToString ()
202 return value.ToString ();
205 public static SybaseDateTime operator + (SybaseDateTime x, TimeSpan t)
208 return SybaseDateTime.Null;
209 return new SybaseDateTime (x.Value + t);
212 public static SybaseBoolean operator == (SybaseDateTime x, SybaseDateTime y)
214 if (x.IsNull || y.IsNull)
215 return SybaseBoolean.Null;
217 return new SybaseBoolean (x.Value == y.Value);
220 public static SybaseBoolean operator > (SybaseDateTime x, SybaseDateTime y)
222 if (x.IsNull || y.IsNull)
223 return SybaseBoolean.Null;
225 return new SybaseBoolean (x.Value > y.Value);
228 public static SybaseBoolean operator >= (SybaseDateTime x, SybaseDateTime y)
230 if (x.IsNull || y.IsNull)
231 return SybaseBoolean.Null;
233 return new SybaseBoolean (x.Value >= y.Value);
236 public static SybaseBoolean operator != (SybaseDateTime x, SybaseDateTime y)
238 if (x.IsNull || y.IsNull)
239 return SybaseBoolean.Null;
241 return new SybaseBoolean (!(x.Value == y.Value));
244 public static SybaseBoolean operator < (SybaseDateTime x, SybaseDateTime y)
246 if (x.IsNull || y.IsNull)
247 return SybaseBoolean.Null;
249 return new SybaseBoolean (x.Value < y.Value);
252 public static SybaseBoolean operator <= (SybaseDateTime x, SybaseDateTime y)
254 if (x.IsNull || y.IsNull)
255 return SybaseBoolean.Null;
257 return new SybaseBoolean (x.Value <= y.Value);
260 public static SybaseDateTime operator - (SybaseDateTime x, TimeSpan t)
263 return SybaseDateTime.Null;
264 return new SybaseDateTime (x.Value - t);
267 public static explicit operator DateTime (SybaseDateTime x)
272 public static explicit operator SybaseDateTime (SybaseString x)
274 return SybaseDateTime.Parse (x.Value);
277 public static implicit operator SybaseDateTime (DateTime x)
279 return new SybaseDateTime (x);