[Mono-patches] r76710 - in trunk/mcs/class/System.Data: System.Data.SqlTypes Test/System.Data.SqlTypes

Raja R Harinath (rharinath@novell.com) mono-patches-list at lists.ximian.com
Fri May 4 09:59:14 EDT 2007


Author: raja
Date: 2007-05-04 09:59:14 -0400 (Fri, 04 May 2007)
New Revision: 76710

Modified:
   trunk/mcs/class/System.Data/System.Data.SqlTypes/ChangeLog
   trunk/mcs/class/System.Data/System.Data.SqlTypes/SqlDateTime.cs
   trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/ChangeLog
   trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/SqlDateTimeTest.cs
Log:
In System.Data.SqlTypes:
	* SqlDateTime.cs: Rewrite parts converting SQL ticks to and from
	DateTime ticks.  Attempt to understand what the damned 'bilisecond'
	means.

In Test/System.Data.SqlTypes:
	* SqlDateTimeTest.cs (TicksRoundTrip, EffingBilisecond): New tests.


Modified: trunk/mcs/class/System.Data/System.Data.SqlTypes/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/System.Data.SqlTypes/ChangeLog	2007-05-04 13:59:07 UTC (rev 76709)
+++ trunk/mcs/class/System.Data/System.Data.SqlTypes/ChangeLog	2007-05-04 13:59:14 UTC (rev 76710)
@@ -1,3 +1,9 @@
+2007-05-04  Raja R Harinath  <rharinath at novell.com>
+
+	* SqlDateTime.cs: Rewrite parts converting SQL ticks to and from
+	DateTime ticks.  Attempt to understand what the damned 'bilisecond'
+	means.
+
 2007-03-09  Nagappan A  <anagappan at novell.com>
 
 	* SqlBoolean.cs (GreaterThan, GreaterThanOrEqual, LessThan)

Modified: trunk/mcs/class/System.Data/System.Data.SqlTypes/SqlDateTime.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data.SqlTypes/SqlDateTime.cs	2007-05-04 13:59:07 UTC (rev 76709)
+++ trunk/mcs/class/System.Data/System.Data.SqlTypes/SqlDateTime.cs	2007-05-04 13:59:14 UTC (rev 76710)
@@ -58,6 +58,8 @@
 		public static readonly int SQLTicksPerHour = 1080000;
 		public static readonly int SQLTicksPerMinute = 18000;
 		public static readonly int SQLTicksPerSecond = 300;
+
+		static readonly DateTime zero_day = new DateTime (1900, 1, 1);
 	      
 		#endregion
 
@@ -84,8 +86,8 @@
 		public SqlDateTime (int dayTicks, int timeTicks) 
 		{
 			try {
-				DateTime temp = new DateTime (1900, 1, 1);
-				this.value = new DateTime (temp.Ticks + (long)(dayTicks + timeTicks));
+				long ms = SQLTicksToMilliseconds (timeTicks);
+				this.value = zero_day.AddDays (dayTicks).AddMilliseconds (ms);
 			} catch (ArgumentOutOfRangeException ex) {
 				throw new SqlTypeException (ex.Message);
 			}
@@ -115,14 +117,22 @@
 			CheckRange (this);
 		}
 
-		[MonoTODO ("Round milisecond")]
-		public SqlDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond) 
+		static int TimeSpanTicksToSQLTicks (long ticks)
 		{
+			return (int) ((ticks * SQLTicksPerSecond) / TimeSpan.TicksPerSecond);
+		}
+
+		static long SQLTicksToMilliseconds (int timeTicks)
+		{
+			return (long) (((timeTicks * 1000.0) / SQLTicksPerSecond) + 0.5);
+		}
+
+		public SqlDateTime (int year, int month, int day, int hour, int minute, int second, double millisecond)
+		{
 			try {
-				DateTime t = new DateTime(year, month, day, hour, minute, second);
-			
-				long ticks = (long) (t.Ticks + millisecond * 10000);
-				this.value = new DateTime (ticks);
+				long ticks = (long) (millisecond * TimeSpan.TicksPerMillisecond);
+				long ms = SQLTicksToMilliseconds (TimeSpanTicksToSQLTicks (ticks));
+				this.value = new DateTime (year, month, day, hour, minute, second).AddMilliseconds (ms);
 			} catch (ArgumentOutOfRangeException ex) {
 				throw new SqlTypeException (ex.Message);
 			}
@@ -130,14 +140,17 @@
 			CheckRange (this);
 		}
 
-		[MonoTODO ("Round bilisecond")]
-		public SqlDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond) // bilisecond??
+		// Some genius in MS came up with 'bilisecond', and gave it the ambiguous definition of one-"billionth"
+		// of a second.  I'm almost tempted to use a nanosecond or a picosecond depending on the current culture :-)
+		// But, wait!! it turns out it's a microsecond, in reality.  AAAAAAAAAAAARGH.  Did this misguided
+		// individual think that a millisecond is a millionth of a second and thus come up with the dastardly name
+		// and the very wrong definition?
+		public SqlDateTime (int year, int month, int day, int hour, int minute, int second, int bilisecond)
 		{
 			try {
-				DateTime t = new DateTime(year, month, day, hour, minute, second);
-			
-				long dateTick = (long) (t.Ticks + bilisecond * 10);
-				this.value = new DateTime (dateTick);
+				long ticks = bilisecond * 10;
+				long ms = SQLTicksToMilliseconds (TimeSpanTicksToSQLTicks (ticks));
+				this.value = new DateTime (year, month, day, hour, minute, second).AddMilliseconds (ms);
 			} catch (ArgumentOutOfRangeException ex) {
 				throw new SqlTypeException (ex.Message);
 			}
@@ -150,14 +163,7 @@
 		#region Properties
 
 		public int DayTicks {
-			get { 
-				float DateTimeTicksPerHour = 3.6E+10f;
-
-				DateTime temp = new DateTime (1900, 1, 1);
-				
-				int result = (int)((this.Value.Ticks - temp.Ticks) / (24 * DateTimeTicksPerHour));
-				return result;
-			}
+			get { return (Value - zero_day).Days; }
 		}
 
 		public bool IsNull { 
@@ -165,23 +171,14 @@
 		}
 
 		public int TimeTicks {
-			get {
-				if (this.IsNull)
-					throw new SqlNullValueException ();
-
-				return (int)(value.Hour * SQLTicksPerHour + 
-					     value.Minute * SQLTicksPerMinute +
-					     value.Second * SQLTicksPerSecond +
-					     value.Millisecond);
-			}
+			get { return TimeSpanTicksToSQLTicks (Value.TimeOfDay.Ticks); }
 		}
 
 		public DateTime Value {
 			get { 
 				if (this.IsNull) 
 					throw new SqlNullValueException ("The property contains Null.");
-				else 
-					return value; 
+				return value; 
 			}
 		}
 
@@ -302,7 +299,6 @@
 		{
 			if (x.IsNull)
 				return SqlDateTime.Null;
-			
 			return new SqlDateTime (x.Value + t);
 		}
 
@@ -310,48 +306,42 @@
 		{
 			if (x.IsNull || y.IsNull) 
 				return SqlBoolean.Null;
-			else
-				return new SqlBoolean (x.Value == y.Value);
+			return new SqlBoolean (x.Value == y.Value);
 		}
 
 		public static SqlBoolean operator > (SqlDateTime x, SqlDateTime y)
 		{
 			if (x.IsNull || y.IsNull) 
 				return SqlBoolean.Null;
-			else
-				return new SqlBoolean (x.Value > y.Value);
+			return new SqlBoolean (x.Value > y.Value);
 		}
 
 		public static SqlBoolean operator >= (SqlDateTime x, SqlDateTime y)
 		{
 			if (x.IsNull || y.IsNull) 
 				return SqlBoolean.Null;
-			else
-				return new SqlBoolean (x.Value >= y.Value);
+			return new SqlBoolean (x.Value >= y.Value);
 		}
 
 		public static SqlBoolean operator != (SqlDateTime x, SqlDateTime y)
 		{
 			if (x.IsNull || y.IsNull) 
 				return SqlBoolean.Null;
-			else
-				return new SqlBoolean (!(x.Value == y.Value));
+			return new SqlBoolean (!(x.Value == y.Value));
 		}
 
 		public static SqlBoolean operator < (SqlDateTime x, SqlDateTime y)
 		{
 			if (x.IsNull || y.IsNull) 
 				return SqlBoolean.Null;
-			else
-				return new SqlBoolean (x.Value < y.Value);
+			return new SqlBoolean (x.Value < y.Value);
 		}
 
 		public static SqlBoolean operator <= (SqlDateTime x, SqlDateTime y)
 		{
 			if (x.IsNull || y.IsNull) 
 				return SqlBoolean.Null;
-			else
-				return new SqlBoolean (x.Value <= y.Value);
+			return new SqlBoolean (x.Value <= y.Value);
 		}
 
 		public static SqlDateTime operator - (SqlDateTime x, TimeSpan t)

Modified: trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/ChangeLog	2007-05-04 13:59:07 UTC (rev 76709)
+++ trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/ChangeLog	2007-05-04 13:59:14 UTC (rev 76710)
@@ -1,3 +1,7 @@
+2007-05-04  Raja R Harinath  <rharinath at novell.com>
+
+	* SqlDateTimeTest.cs (TicksRoundTrip, EffingBilisecond): New tests.
+
 2007-03-09  Nagappan A  <anagappan at novell.com>
 
 	* SqlBooleanTest.cs (GetXsdTypeTest, GreaterThanTest)

Modified: trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/SqlDateTimeTest.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/SqlDateTimeTest.cs	2007-05-04 13:59:07 UTC (rev 76709)
+++ trunk/mcs/class/System.Data/Test/System.Data.SqlTypes/SqlDateTimeTest.cs	2007-05-04 13:59:14 UTC (rev 76710)
@@ -560,6 +560,32 @@
        			AssertEquals ("#Q05", 53, Result.Value.Minute);
 			AssertEquals ("#Q06", 4, Result.Value.Second);
 		}
+
+		[Test]
+		public void TicksRoundTrip ()
+		{
+			SqlDateTime d1 = new SqlDateTime (2007, 05, 04, 18, 02, 40, 398.25);
+			SqlDateTime d2 = new SqlDateTime (d1.DayTicks, d1.TimeTicks);
+
+			AssertEquals ("#R01", 39204, d1.DayTicks);
+			AssertEquals ("#R02", 19488119, d1.TimeTicks);
+			AssertEquals ("#R03", 633138985603970000, d1.Value.Ticks);
+			AssertEquals ("#R04", d1.DayTicks, d2.DayTicks);
+			AssertEquals ("#R05", d1.TimeTicks, d2.TimeTicks);
+			AssertEquals ("#R06", d1.Value.Ticks, d2.Value.Ticks);
+			AssertEquals ("#R07", d1, d2);
+		}
+
+		[Test]
+		public void EffingBilisecond ()
+		{
+			SqlDateTime d1 = new SqlDateTime (2007, 05, 04, 18, 02, 40, 398252);
+
+			AssertEquals ("#S01", 39204, d1.DayTicks);
+			AssertEquals ("#S02", 19488119, d1.TimeTicks);
+			AssertEquals ("#R03", 633138985603970000, d1.Value.Ticks);
+		}
+
 #if NET_2_0
 		[Test]
 		public void GetXsdTypeTest ()



More information about the Mono-patches mailing list