Revision: 9506
Author: [email protected]
Date: Thu Jan  6 12:19:57 2011
Log: Fix a bug in Timestamp.compareTo where the result could be wrong if
the timestamps differed by more than 2^31 milliseconds.

Patch by: jat
Review by: jimbrooks

http://code.google.com/p/google-web-toolkit/source/detail?r=9506

Modified:
 /trunk/user/super/com/google/gwt/emul/java/sql/Timestamp.java
 /trunk/user/super/com/google/gwt/emul/java/util/Date.java
 /trunk/user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java

=======================================
--- /trunk/user/super/com/google/gwt/emul/java/sql/Timestamp.java Wed Jul 7 04:47:08 2010 +++ /trunk/user/super/com/google/gwt/emul/java/sql/Timestamp.java Thu Jan 6 12:19:57 2011
@@ -116,8 +116,8 @@
   }

   public int compareTo(Timestamp o) {
-    int delta = (int) (getTime() - o.getTime());
-    return delta == 0 ? getNanos() - o.getNanos() : delta;
+    int sign = Long.signum(getTime() - o.getTime());
+    return sign == 0 ? getNanos() - o.getNanos() : sign;
   }

   @Override
=======================================
--- /trunk/user/super/com/google/gwt/emul/java/util/Date.java Mon Mar 15 05:16:42 2010 +++ /trunk/user/super/com/google/gwt/emul/java/util/Date.java Thu Jan 6 12:19:57 2011
@@ -125,15 +125,7 @@
   }

   public int compareTo(Date other) {
-    long thisTime = getTime();
-    long otherTime = other.getTime();
-    if (thisTime < otherTime) {
-      return -1;
-    } else if (thisTime > otherTime) {
-      return 1;
-    } else {
-      return 0;
-    }
+    return Long.signum(getTime() - other.getTime());
   }

   @Override
=======================================
--- /trunk/user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java Thu Sep 9 05:17:25 2010 +++ /trunk/user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java Thu Jan 6 12:19:57 2011
@@ -35,6 +35,14 @@
   public String getModuleName() {
     return "com.google.gwt.emultest.EmulSuite";
   }
+
+  public void testCompareTo() {
+    Timestamp now = Timestamp.valueOf("2011-01-05 12:45:18.000000000");
+    // add 1.5 << 31 so coercing the ms difference to int results in a
+    // sign change
+    Timestamp later = new Timestamp(now.getTime() + (3L << 30));
+    assertTrue("later <= now", later.compareTo(now) > 0);
+  }

   /**
    * Timestamps have some non-obvious comparison semantics when compared to

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to