[google-web-toolkit commit] r5218 - DatePicker throws an IllegalStateException when it is used in a time zone where daylight ...

28 views
Skip to first unread message

codesite...@google.com

unread,
Apr 13, 2009, 4:48:05 PM4/13/09
to gwt...@gmail.com
Author: jlab...@google.com
Date: Mon Apr 13 13:46:34 2009
New Revision: 5218

Added:

trunk/user/test/com/google/gwt/user/datepicker/client/CalendarUtilTest.java
(contents, props changed)
Modified:
trunk/user/src/com/google/gwt/user/datepicker/client/CalendarUtil.java
trunk/user/src/com/google/gwt/user/datepicker/client/DatePicker.java
trunk/user/test/com/google/gwt/user/UISuite.java
trunk/user/test/com/google/gwt/user/client/ui/DateBoxTest.java

Log:
DatePicker throws an IllegalStateException when it is used in a time zone
where daylight savings time occurs at midnight. The problem is that, as
the DatePicker populates the dates in the calendar, it uses a time of
00:00:00 for all dates. However, in countries where DST occurs at
midnight, the dates are coerced to the next day, resulting in an invalid
state.

With this patch, DatePicker uses a time of noon instead of midnight. DST
always occurs between midnight and 5am, so noon is safe. This patch also
fixes a broken test that uses date.toString() instead of formatting the
date for the DateBox being testing.

Patch by: jlabanca
Review by: rjrjr

Modified:
trunk/user/src/com/google/gwt/user/datepicker/client/CalendarUtil.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/datepicker/client/CalendarUtil.java
(original)
+++ trunk/user/src/com/google/gwt/user/datepicker/client/CalendarUtil.java
Mon Apr 13 13:46:34 2009
@@ -97,15 +97,11 @@
* @return the different
*/
public static int getDaysBetween(Date start, Date finish) {
- if (hasTime(start)) {
- start = copyDate(start);
- resetTime(start);
- }
-
- if (hasTime(finish)) {
- finish = copyDate(finish);
- resetTime(finish);
- }
+ // Convert the dates to the same time
+ start = copyDate(start);
+ resetTime(start);
+ finish = copyDate(finish);
+ resetTime(finish);

long aTime = start.getTime();
long bTime = finish.getTime();
@@ -127,6 +123,22 @@
}

/**
+ * Check if two dates represent the same date of the same year, even if
they
+ * have different times.
+ *
+ * @param date0 a date
+ * @param date1 a second date
+ * @return true if the dates are the same
+ */
+ public static boolean isSameDate(Date date0, Date date1) {
+ assert date0 != null : "date0 cannot be null";
+ assert date1 != null : "date1 cannot be null";
+ return date0.getYear() == date1.getYear()
+ && date0.getMonth() == date1.getMonth()
+ && date0.getDate() == date1.getDate();
+ }
+
+ /**
* Sets a date object to be at the beginning of the month and no time
* specified.
*
@@ -137,11 +149,6 @@
date.setDate(1);
}

- static boolean hasTime(Date start) {
- return start.getHours() != 0 || start.getMinutes() != 0
- || start.getSeconds() != 0;
- }
-
/**
* Is a day in the week a weekend?
*
@@ -162,7 +169,9 @@
msec = (msec / 1000) * 1000;
date.setTime(msec);

- date.setHours(0);
+ // Daylight savings time occurs at midnight in some time zones, so we
reset
+ // the time to noon instead.
+ date.setHours(12);
date.setMinutes(0);
date.setSeconds(0);
}

Modified:
trunk/user/src/com/google/gwt/user/datepicker/client/DatePicker.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/datepicker/client/DatePicker.java
(original)
+++ trunk/user/src/com/google/gwt/user/datepicker/client/DatePicker.java
Mon Apr 13 13:46:34 2009
@@ -458,7 +458,8 @@
CalendarView r = getView();
Date first = r.getFirstDate();
Date last = r.getLastDate();
- return (date != null && (first.equals(date) || last.equals(date) ||
(first.before(date) && last.after(date))));
+ return (date != null && (CalendarUtil.isSameDate(first, date)
+ || CalendarUtil.isSameDate(last, date) || (first.before(date) &&
last.after(date))));
}

@Override

Modified: trunk/user/test/com/google/gwt/user/UISuite.java
==============================================================================
--- trunk/user/test/com/google/gwt/user/UISuite.java (original)
+++ trunk/user/test/com/google/gwt/user/UISuite.java Mon Apr 13 13:46:34
2009
@@ -85,6 +85,7 @@
import com.google.gwt.user.client.ui.WidgetSubclassingTest;
import com.google.gwt.user.client.ui.WidgetTest;
import com.google.gwt.user.client.ui.impl.ClippedImagePrototypeTest;
+import com.google.gwt.user.datepicker.client.CalendarUtilTest;
import com.google.gwt.user.datepicker.client.DateChangeEventTest;
import com.google.gwt.user.rebind.ui.ImageBundleGeneratorTest;
import com.google.gwt.xml.client.XMLTest;
@@ -109,6 +110,7 @@
suite.addTestSuite(CompositeTest.class);
suite.addTestSuite(CookieTest.class);
suite.addTestSuite(CustomButtonTest.class);
+ suite.addTestSuite(CalendarUtilTest.class);
suite.addTestSuite(DateBoxTest.class);
suite.addTestSuite(DatePickerTest.class);
suite.addTestSuite(DeckPanelTest.class);

Modified: trunk/user/test/com/google/gwt/user/client/ui/DateBoxTest.java
==============================================================================
--- trunk/user/test/com/google/gwt/user/client/ui/DateBoxTest.java
(original)
+++ trunk/user/test/com/google/gwt/user/client/ui/DateBoxTest.java Mon Apr
13 13:46:34 2009
@@ -50,10 +50,10 @@
new DateValueChangeTester(db2) {
@Override
protected void fire(java.util.Date d) {
- db2.getTextBox().setText(d.toString());
+ db2.getTextBox().setText(db2.getFormat().format(db2, d));
NativeEvent e = Document.get().createBlurEvent();
db2.getTextBox().getElement().dispatchEvent(e);
- };
+ }
}.run();

// Checks that setting the date picker's date works correctly.
@@ -63,7 +63,7 @@
@Override
protected void fire(java.util.Date d) {
db3.getDatePicker().setValue(d, true);
- };
+ }
}.run();
}
}

Added:
trunk/user/test/com/google/gwt/user/datepicker/client/CalendarUtilTest.java
==============================================================================
--- (empty file)
+++
trunk/user/test/com/google/gwt/user/datepicker/client/CalendarUtilTest.java
Mon Apr 13 13:46:34 2009
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.gwt.user.datepicker.client;
+
+import junit.framework.TestCase;
+
+import java.util.Date;
+
+/**
+ * Tests {@link CalendarUtil}.
+ */
+@SuppressWarnings("deprecation")
+public class CalendarUtilTest extends TestCase {
+ public void testAddDaysToDate() {
+ Date start = new Date(99, 5, 15, 3, 30, 5);
+
+ // Add 0 days
+ CalendarUtil.addDaysToDate(start, 0);
+ assertEquals(new Date(99, 5, 15, 3, 30, 5), start);
+
+ // Add 5 days
+ CalendarUtil.addDaysToDate(start, 5);
+ assertEquals(new Date(99, 5, 20, 3, 30, 5), start);
+
+ // Subtract 10 days
+ CalendarUtil.addDaysToDate(start, -10);
+ assertEquals(new Date(99, 5, 10, 3, 30, 5), start);
+ }
+
+ public void testAddMonthsToDate() {
+ Date start = new Date(99, 5, 15, 3, 30, 5);
+
+ // Add 0 months
+ CalendarUtil.addMonthsToDate(start, 0);
+ assertEquals(new Date(99, 5, 15, 3, 30, 5), start);
+
+ // Add 5 months
+ CalendarUtil.addMonthsToDate(start, 5);
+ assertEquals(new Date(99, 10, 15, 3, 30, 5), start);
+
+ // Subtract 6 months
+ CalendarUtil.addMonthsToDate(start, -6);
+ assertEquals(new Date(99, 4, 15, 3, 30, 5), start);
+ }
+
+ public void testCopyDate() {
+ Date original = new Date(99, 5, 15, 3, 30, 5);
+
+ // Copy the date
+ Date copy = CalendarUtil.copyDate(original);
+ assertEquals(original, copy);
+ assertEquals(99, original.getYear());
+
+ // Mutate the copy
+ copy.setYear(70);
+ assertEquals(99, original.getYear());
+ assertEquals(70, copy.getYear());
+ }
+
+ public void testGetDaysBetween() {
+ // Same date, same time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 15, 3, 30, 5);
+ assertEquals(0, CalendarUtil.getDaysBetween(d0, d1));
+ }
+
+ // Same date, different time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 15, 4, 20, 5);
+ assertEquals(0, CalendarUtil.getDaysBetween(d0, d1));
+ }
+
+ // Three days ahead, same time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 18, 3, 30, 5);
+ assertEquals(3, CalendarUtil.getDaysBetween(d0, d1));
+ }
+
+ // Three days ahead, different time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 18, 4, 20, 5);
+ assertEquals(3, CalendarUtil.getDaysBetween(d0, d1));
+ }
+
+ // Three days behind, sametime
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 12, 3, 30, 5);
+ assertEquals(-3, CalendarUtil.getDaysBetween(d0, d1));
+ }
+ }
+
+ public void testIsSameDate() {
+ // Same date, same time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 15, 3, 30, 5);
+ assertTrue(CalendarUtil.isSameDate(d0, d1));
+ }
+
+ // Same date, different time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 15, 4, 20, 5);
+ assertTrue(CalendarUtil.isSameDate(d0, d1));
+ }
+
+ // Different date, same time
+ {
+ Date d0 = new Date(99, 5, 15, 3, 30, 5);
+ Date d1 = new Date(99, 5, 18, 3, 30, 5);
+ assertFalse(CalendarUtil.isSameDate(d0, d1));
+ }
+ }
+
+ public void testSetToFirstDayOfMonth() {
+ // Start in middle of month
+ {
+ Date date = new Date(99, 5, 15);
+ CalendarUtil.setToFirstDayOfMonth(date);
+ assertTrue(CalendarUtil.isSameDate(new Date(99, 5, 1), date));
+ }
+
+ // Start on first day of month
+ {
+ Date date = new Date(99, 5, 1);
+ CalendarUtil.setToFirstDayOfMonth(date);
+ assertTrue(CalendarUtil.isSameDate(new Date(99, 5, 1), date));
+ }
+ }
+}

Reply all
Reply to author
Forward
0 new messages