Reviewers: Dan Rice,

Description:
The emul version of java.util.Date can add an extra hour to the day
before daylight savings time if setMinutes() or setSeconds is called.
The problem is that we compare the new javascript hours to the expected
hours to see if the hours are equal, but the expected hours is a double
instead of an int so they are never equal.

Fix:
====
We now truncate the expected hours to an int.

Testing:
========
I added a unit test.

Please review this at http://gwt-code-reviews.appspot.com/174801

Affected files:
  user/super/com/google/gwt/emul/java/util/Date.java
  user/test/com/google/gwt/emultest/java/util/DateTest.java


Index: user/test/com/google/gwt/emultest/java/util/DateTest.java
===================================================================
--- user/test/com/google/gwt/emultest/java/util/DateTest.java (revision 7701)
+++ user/test/com/google/gwt/emultest/java/util/DateTest.java   (working copy)
@@ -143,6 +143,39 @@
     }
   }

+  /**
+ * Tests that if daylight savings time occurs tomorrow, the current date isn't
+   * affected.
+   */
+  public void testClockForwardNextDay() {
+    int[] monthDayHour = new int[3];
+    if (!findClockForwardTime(2009, monthDayHour)) {
+      return;
+    }
+
+    int month = monthDayHour[0];
+    int day = monthDayHour[1] - 1; // Day before.
+    int hour = 12; // Noon.
+    Date d = new Date(2009 - 1900, month, day, hour, 0, 0);
+    assertEquals(day, d.getDate());
+    assertEquals(hour, d.getHours());
+
+    // Change the minutes, which triggers fixDaylightSavings.
+    d.setMinutes(10);
+    assertEquals(day, d.getDate());
+    assertEquals(hour, d.getHours());
+
+    // Change the seconds, which triggers fixDaylightSavings.
+    d.setSeconds(10);
+    assertEquals(day, d.getDate());
+    assertEquals(hour, d.getHours());
+
+    // Change the minutes by more than an hour.
+    d.setMinutes(80);
+    assertEquals(day, d.getDate());
+    assertEquals(hour + 1, d.getHours());
+  }
+
   /** Testing for public java.lang.Object java.util.Date.clone(). */
   public void testClone() {

Index: user/super/com/google/gwt/emul/java/util/Date.java
===================================================================
--- user/super/com/google/gwt/emul/java/util/Date.java  (revision 7701)
+++ user/super/com/google/gwt/emul/java/util/Date.java  (working copy)
@@ -229,7 +229,8 @@

   public native void setMinutes(int minutes) /*-{
     th...@java.util.date::checkJsDate()();
-    var hours = th...@java.util.date::jsdate.getHours() + minutes / 60;
+    // Truncate minutes to int.
+    var hours = th...@java.util.date::jsdate.getHours() + ~~(minutes / 60);
     th...@java.util.date::jsdate.setMinutes(minutes);
     th...@java.util.date::fixDaylightSavings(I)(hours);
   }-*/;
@@ -243,7 +244,8 @@

   public native void setSeconds(int seconds) /*-{
     th...@java.util.date::checkJsDate()();
- var hours = th...@java.util.date::jsdate.getHours() + seconds / (60 * 60);
+    // Truncate seconds to int.
+ var hours = th...@java.util.date::jsdate.getHours() + ~~(seconds / (60 * 60));
     th...@java.util.date::jsdate.setSeconds(seconds);
     th...@java.util.date::fixDaylightSavings(I)(hours);
   }-*/;


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

Reply via email to