2 // System.Data.SqlTypes.SqlDateTime
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
8 // (C) Copyright 2002 Tim Coleman
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:
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.
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlDateTime : INullable, IComparable
42 private DateTime value;
45 public static readonly SqlDateTime MaxValue = new SqlDateTime (9999,12,31,23,59,59, (double)997);
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;
56 public SqlDateTime (DateTime value)
63 public SqlDateTime (int dayTicks, int timeTicks)
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);
75 public SqlDateTime (int year, int month, int day)
78 this.value = new DateTime (year, month, day);
79 } catch (ArgumentOutOfRangeException ex) {
80 throw new SqlTypeException (ex.Message);
86 public SqlDateTime (int year, int month, int day, int hour, int minute, int second)
89 this.value = new DateTime (year, month, day, hour, minute, second);
90 } catch (ArgumentOutOfRangeException ex) {
91 throw new SqlTypeException (ex.Message);
97 [MonoTODO ("Round milisecond")]
98 public SqlDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond)
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);
112 [MonoTODO ("Round bilisecond")]
113 public SqlDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond) // bilisecond??
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);
131 public int DayTicks {
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));
143 get { return !notNull; }
146 public int TimeTicks {
149 throw new SqlNullValueException ();
151 return (int)(value.Hour * SQLTicksPerHour +
152 value.Minute * SQLTicksPerMinute +
153 value.Second * SQLTicksPerSecond +
158 public DateTime Value {
161 throw new SqlNullValueException ("The property contains Null.");
170 private static void CheckRange (SqlDateTime target)
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)
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)
187 return this.value.CompareTo (((SqlDateTime)value).Value);
190 public override bool Equals (object value)
192 if (!(value is SqlDateTime))
194 else if (this.IsNull && ((SqlDateTime)value).IsNull)
196 else if (((SqlDateTime)value).IsNull)
199 return (bool) (this == (SqlDateTime)value);
202 public static SqlBoolean Equals (SqlDateTime x, SqlDateTime y)
207 public override int GetHashCode ()
209 return value.GetHashCode ();
212 public static SqlBoolean GreaterThan (SqlDateTime x, SqlDateTime y)
217 public static SqlBoolean GreaterThanOrEqual (SqlDateTime x, SqlDateTime y)
222 public static SqlBoolean LessThan (SqlDateTime x, SqlDateTime y)
227 public static SqlBoolean LessThanOrEqual (SqlDateTime x, SqlDateTime y)
232 public static SqlBoolean NotEquals (SqlDateTime x, SqlDateTime y)
237 public static SqlDateTime Parse (string s)
240 throw new ArgumentNullException ("Argument cannot be null");
242 // try parsing in local culture
243 DateTimeFormatInfo fmtInfo = DateTimeFormatInfo.CurrentInfo;
245 return new SqlDateTime (DateTime.Parse (s, fmtInfo));
246 } catch (Exception) { }
248 // try parsing in invariant culture
250 return new SqlDateTime (DateTime.Parse (s, CultureInfo.InvariantCulture));
251 } catch (Exception) { }
253 // Not a recognizable format.
254 throw new FormatException (String.Format ("String {0} is not recognized as "+
255 " valid DateTime.", s));
259 public SqlString ToSqlString ()
261 return ((SqlString)this);
264 public override string ToString ()
269 return value.ToString (CultureInfo.InvariantCulture);
272 public static SqlDateTime operator + (SqlDateTime x, TimeSpan t)
275 return SqlDateTime.Null;
277 return new SqlDateTime (x.Value + t);
280 public static SqlBoolean operator == (SqlDateTime x, SqlDateTime y)
282 if (x.IsNull || y.IsNull)
283 return SqlBoolean.Null;
285 return new SqlBoolean (x.Value == y.Value);
288 public static SqlBoolean operator > (SqlDateTime x, SqlDateTime y)
290 if (x.IsNull || y.IsNull)
291 return SqlBoolean.Null;
293 return new SqlBoolean (x.Value > y.Value);
296 public static SqlBoolean operator >= (SqlDateTime x, SqlDateTime y)
298 if (x.IsNull || y.IsNull)
299 return SqlBoolean.Null;
301 return new SqlBoolean (x.Value >= y.Value);
304 public static SqlBoolean operator != (SqlDateTime x, SqlDateTime y)
306 if (x.IsNull || y.IsNull)
307 return SqlBoolean.Null;
309 return new SqlBoolean (!(x.Value == y.Value));
312 public static SqlBoolean operator < (SqlDateTime x, SqlDateTime y)
314 if (x.IsNull || y.IsNull)
315 return SqlBoolean.Null;
317 return new SqlBoolean (x.Value < y.Value);
320 public static SqlBoolean operator <= (SqlDateTime x, SqlDateTime y)
322 if (x.IsNull || y.IsNull)
323 return SqlBoolean.Null;
325 return new SqlBoolean (x.Value <= y.Value);
328 public static SqlDateTime operator - (SqlDateTime x, TimeSpan t)
332 return new SqlDateTime (x.Value - t);
335 public static explicit operator DateTime (SqlDateTime x)
340 public static explicit operator SqlDateTime (SqlString x)
342 return SqlDateTime.Parse (x.Value);
345 public static implicit operator SqlDateTime (DateTime x)
347 return new SqlDateTime (x);