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);
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
)
239 return new SqlDateTime (DateTime
.Parse (s
));
242 public SqlString
ToSqlString ()
244 return ((SqlString
)this);
247 public override string ToString ()
252 return value.ToString (CultureInfo
.InvariantCulture
);
255 public static SqlDateTime
operator + (SqlDateTime x
, TimeSpan t
)
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
;
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
;
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
;
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
;
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
;
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
;
308 return new SqlBoolean (x
.Value
<= y
.Value
);
311 public static SqlDateTime
operator - (SqlDateTime x
, TimeSpan t
)
315 return new SqlDateTime (x
.Value
- t
);
318 public static explicit operator DateTime (SqlDateTime x
)
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
);