Revision: 7705
Author: jlaba...@google.com
Date: Thu Mar 11 09:27:25 2010
Log: Date can add an hour to the day before Daylight Saving.
http://gwt-code-reviews.appspot.com/174801
Review by: r...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=7705
Modified:
/trunk/user/super/com/google/gwt/emul/java/util/Date.java
/trunk/user/test/com/google/gwt/emultest/java/util/DateTest.java
=======================================
--- /trunk/user/super/com/google/gwt/emul/java/util/Date.java Wed Nov 4
13:53:54 2009
+++ /trunk/user/super/com/google/gwt/emul/java/util/Date.java Thu Mar 11
09:27:25 2010
@@ -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 / 60) 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 / (60 * 60)) 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);
}-*/;
@@ -348,7 +350,9 @@
d.setDate(d.getDate() + 1);
var loff = d.getTimezoneOffset();
var timeDiff = noff - loff;
-
+ var timeDiffHours = ~~(timeDiff / 60);
+ var timeDiffMinutes = timeDiff % 60;
+
// If the time zone offset is changing, advance the hours and
// minutes from the initially requested time by the change amount
if (timeDiff > 0) {
@@ -358,12 +362,12 @@
var badHours = th...@java.util.date::jsdate.getHours();
var minute = th...@java.util.date::jsdate.getMinutes();
var second = th...@java.util.date::jsdate.getSeconds();
- if (badHours + timeDiff / 60 >= 24) {
+ if (badHours + timeDiffHours >= 24) {
day++;
}
var newTime = new Date(year, month, day,
- hours + timeDiff / 60,
- minute + timeDiff % 60, second);
+ hours + timeDiffHours,
+ minute + timeDiffMinutes, second);
th...@java.util.date::jsdate.setTime(newTime.getTime());
}
}
=======================================
--- /trunk/user/test/com/google/gwt/emultest/java/util/DateTest.java Wed
Nov 4 13:53:54 2009
+++ /trunk/user/test/com/google/gwt/emultest/java/util/DateTest.java Thu
Mar 11 09:27:25 2010
@@ -142,6 +142,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 = monthDayHour[2];
+ 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() {
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors