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.
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
40 namespace System.Data.SqlTypes
43 [SerializableAttribute]
44 [XmlSchemaProvider ("GetXsdType")]
46 public struct SqlDateTime : INullable, IComparable
53 private DateTime value;
56 public static readonly SqlDateTime MaxValue;
57 public static readonly SqlDateTime MinValue;
58 public static readonly SqlDateTime Null;
59 public static readonly int SQLTicksPerHour = 1080000;
60 public static readonly int SQLTicksPerMinute = 18000;
61 public static readonly int SQLTicksPerSecond = 300;
63 static readonly DateTime zero_day = new DateTime (1900, 1, 1);
71 DateTime t = new DateTime (9999, 12, 31, 23, 59, 59);
72 long ticks = (long) (t.Ticks + (997 * 10000));
73 MaxValue.value = new DateTime (ticks);
74 MaxValue.notNull = true;
76 MinValue.value = new DateTime (1753, 1, 1);
77 MinValue.notNull = true;
80 public SqlDateTime (DateTime value)
87 public SqlDateTime (int dayTicks, int timeTicks)
90 long ms = SQLTicksToMilliseconds (timeTicks);
91 this.value = zero_day.AddDays (dayTicks).AddMilliseconds (ms);
92 } catch (ArgumentOutOfRangeException ex) {
93 throw new SqlTypeException (ex.Message);
99 public SqlDateTime (int year, int month, int day)
102 this.value = new DateTime (year, month, day);
103 } catch (ArgumentOutOfRangeException ex) {
104 throw new SqlTypeException (ex.Message);
110 public SqlDateTime (int year, int month, int day, int hour, int minute, int second)
113 this.value = new DateTime (year, month, day, hour, minute, second);
114 } catch (ArgumentOutOfRangeException ex) {
115 throw new SqlTypeException (ex.Message);
121 static int TimeSpanTicksToSQLTicks (long ticks)
123 return (int) ((ticks * SQLTicksPerSecond) / TimeSpan.TicksPerSecond);
126 static long SQLTicksToMilliseconds (int timeTicks)
128 return (long) (((timeTicks * 1000.0) / SQLTicksPerSecond) + 0.5);
131 public SqlDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond)
134 long ticks = (long) (millisecond * TimeSpan.TicksPerMillisecond);
135 long ms = SQLTicksToMilliseconds (TimeSpanTicksToSQLTicks (ticks));
136 this.value = new DateTime (year, month, day, hour, minute, second).AddMilliseconds (ms);
137 } catch (ArgumentOutOfRangeException ex) {
138 throw new SqlTypeException (ex.Message);
144 // Some genius in MS came up with 'bilisecond', and gave it the ambiguous definition of one-"billionth"
145 // of a second. I'm almost tempted to use a nanosecond or a picosecond depending on the current culture :-)
146 // But, wait!! it turns out it's a microsecond, in reality. AAAAAAAAAAAARGH. Did this misguided
147 // individual think that a millisecond is a millionth of a second and thus come up with the dastardly name
148 // and the very wrong definition?
149 public SqlDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond)
152 long ticks = bilisecond * 10;
153 long ms = SQLTicksToMilliseconds (TimeSpanTicksToSQLTicks (ticks));
154 this.value = new DateTime (year, month, day, hour, minute, second).AddMilliseconds (ms);
155 } catch (ArgumentOutOfRangeException ex) {
156 throw new SqlTypeException (ex.Message);
166 public int DayTicks {
167 get { return (Value - zero_day).Days; }
171 get { return !notNull; }
174 public int TimeTicks {
175 get { return TimeSpanTicksToSQLTicks (Value.TimeOfDay.Ticks); }
178 public DateTime Value {
181 throw new SqlNullValueException ("The property contains Null.");
190 private static void CheckRange (SqlDateTime target)
194 if (target.value > MaxValue.value || target.value < MinValue.value)
195 throw new SqlTypeException (String.Format ("SqlDateTime overflow. Must be between {0} and {1}. Value was {2}", MinValue.Value, MaxValue.Value, target.value));
198 public int CompareTo (object value)
202 if (!(value is SqlDateTime))
203 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlDateTime"));
205 return CompareTo ((SqlDateTime) value);
211 int CompareTo (SqlDateTime value)
216 return this.value.CompareTo (value.Value);
219 public override bool Equals (object value)
221 if (!(value is SqlDateTime))
223 else if (this.IsNull)
224 return ((SqlDateTime)value).IsNull;
225 else if (((SqlDateTime)value).IsNull)
228 return (bool) (this == (SqlDateTime)value);
231 public static SqlBoolean Equals (SqlDateTime x, SqlDateTime y)
236 public override int GetHashCode ()
238 return value.GetHashCode ();
242 public static SqlDateTime Add (SqlDateTime x, TimeSpan t)
247 public static SqlDateTime Subtract (SqlDateTime x, TimeSpan t)
253 public static SqlBoolean GreaterThan (SqlDateTime x, SqlDateTime y)
258 public static SqlBoolean GreaterThanOrEqual (SqlDateTime x, SqlDateTime y)
263 public static SqlBoolean LessThan (SqlDateTime x, SqlDateTime y)
268 public static SqlBoolean LessThanOrEqual (SqlDateTime x, SqlDateTime y)
273 public static SqlBoolean NotEquals (SqlDateTime x, SqlDateTime y)
278 public static SqlDateTime Parse (string s)
281 throw new ArgumentNullException ("Argument cannot be null");
283 // try parsing in local culture
284 DateTimeFormatInfo fmtInfo = DateTimeFormatInfo.CurrentInfo;
286 return new SqlDateTime (DateTime.Parse (s, fmtInfo));
287 } catch (Exception) {
290 // try parsing in invariant culture
292 return new SqlDateTime (DateTime.Parse (s, CultureInfo.InvariantCulture));
293 } catch (Exception) {
296 throw new FormatException (String.Format ("String {0}" +
297 " is not recognized as valid DateTime.", s));
300 public SqlString ToSqlString ()
302 return ((SqlString) this);
305 public override string ToString ()
310 return value.ToString (CultureInfo.InvariantCulture);
313 public static SqlDateTime operator + (SqlDateTime x, TimeSpan t)
316 return SqlDateTime.Null;
317 return new SqlDateTime (x.Value + t);
320 public static SqlBoolean operator == (SqlDateTime x, SqlDateTime y)
322 if (x.IsNull || y.IsNull)
323 return SqlBoolean.Null;
324 return new SqlBoolean (x.Value == y.Value);
327 public static SqlBoolean operator > (SqlDateTime x, SqlDateTime y)
329 if (x.IsNull || y.IsNull)
330 return SqlBoolean.Null;
331 return new SqlBoolean (x.Value > y.Value);
334 public static SqlBoolean operator >= (SqlDateTime x, SqlDateTime y)
336 if (x.IsNull || y.IsNull)
337 return SqlBoolean.Null;
338 return new SqlBoolean (x.Value >= y.Value);
341 public static SqlBoolean operator != (SqlDateTime x, SqlDateTime y)
343 if (x.IsNull || y.IsNull)
344 return SqlBoolean.Null;
345 return new SqlBoolean (!(x.Value == y.Value));
348 public static SqlBoolean operator < (SqlDateTime x, SqlDateTime y)
350 if (x.IsNull || y.IsNull)
351 return SqlBoolean.Null;
352 return new SqlBoolean (x.Value < y.Value);
355 public static SqlBoolean operator <= (SqlDateTime x, SqlDateTime y)
357 if (x.IsNull || y.IsNull)
358 return SqlBoolean.Null;
359 return new SqlBoolean (x.Value <= y.Value);
362 public static SqlDateTime operator - (SqlDateTime x, TimeSpan t)
366 return new SqlDateTime (x.Value - t);
369 public static explicit operator DateTime (SqlDateTime x)
374 public static explicit operator SqlDateTime (SqlString x)
376 return SqlDateTime.Parse (x.Value);
379 public static implicit operator SqlDateTime (DateTime value)
381 return new SqlDateTime (value);
385 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
387 XmlQualifiedName qualifiedName = new XmlQualifiedName ("dateTime", "http://www.w3.org/2001/XMLSchema");
388 return qualifiedName;
392 XmlSchema IXmlSerializable.GetSchema ()
394 throw new NotImplementedException ();
398 void IXmlSerializable.ReadXml (XmlReader reader)
400 throw new NotImplementedException ();
404 void IXmlSerializable.WriteXml (XmlWriter writer)
406 throw new NotImplementedException ();