(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlDateTime.cs
blob24634b1d0f7906642b1906c9d6cffc50e1289177
1 //
2 // System.Data.SqlTypes.SqlDateTime
3 //
4 // Author:
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 using System;
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlDateTime : INullable, IComparable
41 #region Fields
42 private DateTime value;
43 private bool notNull;
45 public static readonly SqlDateTime MaxValue = new SqlDateTime (9999,12,31,23,59,59);
46 public static readonly SqlDateTime MinValue = new SqlDateTime (1753,1,1);
47 public static readonly SqlDateTime Null;
48 public static readonly int SQLTicksPerHour = 1080000;
49 public static readonly int SQLTicksPerMinute = 18000;
50 public static readonly int SQLTicksPerSecond = 300;
52 #endregion
54 #region Constructors
56 public SqlDateTime (DateTime value)
58 this.value = value;
59 notNull = true;
60 CheckRange (this);
63 public SqlDateTime (int dayTicks, int timeTicks)
65 try {
66 DateTime temp = new DateTime (1900, 1, 1);
67 this.value = new DateTime (temp.Ticks + (long)(dayTicks + timeTicks));
68 } catch (ArgumentOutOfRangeException ex) {
69 throw new SqlTypeException (ex.Message);
71 notNull = true;
72 CheckRange (this);
75 public SqlDateTime (int year, int month, int day)
77 try {
78 this.value = new DateTime (year, month, day);
79 } catch (ArgumentOutOfRangeException ex) {
80 throw new SqlTypeException (ex.Message);
82 notNull = true;
83 CheckRange (this);
86 public SqlDateTime (int year, int month, int day, int hour, int minute, int second)
88 try {
89 this.value = new DateTime (year, month, day, hour, minute, second);
90 } catch (ArgumentOutOfRangeException ex) {
91 throw new SqlTypeException (ex.Message);
93 notNull = true;
94 CheckRange (this);
97 [MonoTODO ("Round milisecond")]
98 public SqlDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond)
100 try {
101 DateTime t = new DateTime(year, month, day, hour, minute, second);
103 long ticks = (long) (t.Ticks + millisecond * 10000);
104 this.value = new DateTime (ticks);
105 } catch (ArgumentOutOfRangeException ex) {
106 throw new SqlTypeException (ex.Message);
108 notNull = true;
109 CheckRange (this);
112 [MonoTODO ("Round bilisecond")]
113 public SqlDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond) // bilisecond??
115 try {
116 DateTime t = new DateTime(year, month, day, hour, minute, second);
118 long dateTick = (long) (t.Ticks + bilisecond * 10);
119 this.value = new DateTime (dateTick);
120 } catch (ArgumentOutOfRangeException ex) {
121 throw new SqlTypeException (ex.Message);
123 notNull = true;
124 CheckRange (this);
127 #endregion
129 #region Properties
131 public int DayTicks {
132 get {
133 float DateTimeTicksPerHour = 3.6E+10f;
135 DateTime temp = new DateTime (1900, 1, 1);
137 int result = (int)((this.Value.Ticks - temp.Ticks) / (24 * DateTimeTicksPerHour));
138 return result;
142 public bool IsNull {
143 get { return !notNull; }
146 public int TimeTicks {
147 get {
148 if (this.IsNull)
149 throw new SqlNullValueException ();
151 return (int)(value.Hour * SQLTicksPerHour +
152 value.Minute * SQLTicksPerMinute +
153 value.Second * SQLTicksPerSecond +
154 value.Millisecond);
158 public DateTime Value {
159 get {
160 if (this.IsNull)
161 throw new SqlNullValueException ("The property contains Null.");
162 else
163 return value;
167 #endregion
169 #region Methods
170 private static void CheckRange (SqlDateTime target)
172 if (target.IsNull)
173 return;
174 if (target.value > MaxValue.value || target.value < MinValue.value)
175 throw new SqlTypeException (String.Format ("SqlDateTime overflow. Must be between {0} and {1}. Value was {2}", MinValue.Value, MaxValue.Value, target.value));
178 public int CompareTo (object value)
180 if (value == null)
181 return 1;
182 else if (!(value is SqlDateTime))
183 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlDateTime"));
184 else if (((SqlDateTime)value).IsNull)
185 return 1;
186 else
187 return this.value.CompareTo (((SqlDateTime)value).Value);
190 public override bool Equals (object value)
192 if (!(value is SqlDateTime))
193 return false;
194 else if (this.IsNull && ((SqlDateTime)value).IsNull)
195 return true;
196 else if (((SqlDateTime)value).IsNull)
197 return false;
198 else
199 return (bool) (this == (SqlDateTime)value);
202 public static SqlBoolean Equals (SqlDateTime x, SqlDateTime y)
204 return (x == y);
207 public override int GetHashCode ()
209 return value.GetHashCode ();
212 public static SqlBoolean GreaterThan (SqlDateTime x, SqlDateTime y)
214 return (x > y);
217 public static SqlBoolean GreaterThanOrEqual (SqlDateTime x, SqlDateTime y)
219 return (x >= y);
222 public static SqlBoolean LessThan (SqlDateTime x, SqlDateTime y)
224 return (x < y);
227 public static SqlBoolean LessThanOrEqual (SqlDateTime x, SqlDateTime y)
229 return (x <= y);
232 public static SqlBoolean NotEquals (SqlDateTime x, SqlDateTime y)
234 return (x != y);
237 public static SqlDateTime Parse (string s)
239 return new SqlDateTime (DateTime.Parse (s));
242 public SqlString ToSqlString ()
244 return ((SqlString)this);
247 public override string ToString ()
249 if (this.IsNull)
250 return "Null";
251 else
252 return value.ToString (CultureInfo.InvariantCulture);
255 public static SqlDateTime operator + (SqlDateTime x, TimeSpan t)
257 if (x.IsNull)
258 return SqlDateTime.Null;
260 return new SqlDateTime (x.Value + t);
263 public static SqlBoolean operator == (SqlDateTime x, SqlDateTime y)
265 if (x.IsNull || y.IsNull)
266 return SqlBoolean.Null;
267 else
268 return new SqlBoolean (x.Value == y.Value);
271 public static SqlBoolean operator > (SqlDateTime x, SqlDateTime y)
273 if (x.IsNull || y.IsNull)
274 return SqlBoolean.Null;
275 else
276 return new SqlBoolean (x.Value > y.Value);
279 public static SqlBoolean operator >= (SqlDateTime x, SqlDateTime y)
281 if (x.IsNull || y.IsNull)
282 return SqlBoolean.Null;
283 else
284 return new SqlBoolean (x.Value >= y.Value);
287 public static SqlBoolean operator != (SqlDateTime x, SqlDateTime y)
289 if (x.IsNull || y.IsNull)
290 return SqlBoolean.Null;
291 else
292 return new SqlBoolean (!(x.Value == y.Value));
295 public static SqlBoolean operator < (SqlDateTime x, SqlDateTime y)
297 if (x.IsNull || y.IsNull)
298 return SqlBoolean.Null;
299 else
300 return new SqlBoolean (x.Value < y.Value);
303 public static SqlBoolean operator <= (SqlDateTime x, SqlDateTime y)
305 if (x.IsNull || y.IsNull)
306 return SqlBoolean.Null;
307 else
308 return new SqlBoolean (x.Value <= y.Value);
311 public static SqlDateTime operator - (SqlDateTime x, TimeSpan t)
313 if (x.IsNull)
314 return x;
315 return new SqlDateTime (x.Value - t);
318 public static explicit operator DateTime (SqlDateTime x)
320 return x.Value;
323 public static explicit operator SqlDateTime (SqlString x)
325 return SqlDateTime.Parse (x.Value);
328 public static implicit operator SqlDateTime (DateTime x)
330 return new SqlDateTime (x);
333 #endregion