Revision: 7326
Author: [email protected]
Date: Thu Dec 17 09:24:07 2009
Log: Fix external issue 4110: DateTimeFormat rounds fractional seconds  
incorrectly

Review by: jlabanca


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

Modified:
  /trunk/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
  /trunk/user/test/com/google/gwt/i18n/client/DateTimeFormat_en_Test.java

=======================================
--- /trunk/user/src/com/google/gwt/i18n/client/DateTimeFormat.java      Tue Dec 
  
1 13:59:12 2009
+++ /trunk/user/src/com/google/gwt/i18n/client/DateTimeFormat.java      Thu Dec 
 
17 09:24:07 2009
@@ -1003,9 +1003,15 @@
     * @param date hold the date object to be formatted
     */
    private void formatFractionalSeconds(StringBuffer buf, int count, Date  
date) {
-    // Fractional seconds should be left-justified, ie. zero must be padded
-    // from left. For example, if value in milliseconds is 5, and count is  
3,
-    // the output need to be "005".
+    /*
+     * Fractional seconds should be left-justified, ie. zero must be  
padded from
+     * left. For example, if the value in milliseconds is 5, and the count  
is 3,
+     * the output will be "005".
+     *
+     * Values with less than three digits are rounded to the desired  
number of
+     * places, but the rounded values are truncated at 9 or 99 in order to  
avoid
+     * changing the values of seconds.
+     */
      long time = date.getTime();
      int value;
      if (time < 0) {
@@ -1014,10 +1020,10 @@
        value = (int) (time % 1000);
      }
      if (count == 1) {
-      value = (value + 50) / 100; // Round to 100ms.
-      buf.append(Integer.toString(value));
+      value = Math.min((value + 50) / 100, 9); // Round to 100ms, clamp to  
9
+      buf.append((char) ('0' + value));
      } else if (count == 2) {
-      value = (value + 5) / 10; // Round to 10ms.
+      value = Math.min((value + 5) / 10, 99); // Round to 10ms, clamp to 99
        zeroPaddingNumber(buf, value, 2);
      } else {
        zeroPaddingNumber(buf, value, 3);
@@ -1192,7 +1198,7 @@
        zeroPaddingNumber(buf, value % 100, 2);
      } else {
        // count != 2
-      buf.append(Integer.toString(value));
+      buf.append(value);
      }
    }

@@ -2044,6 +2050,6 @@
        }
        b *= NUMBER_BASE;
      }
-    buf.append(Integer.toString(value));
+    buf.append(value);
    }
  }
=======================================
--- /trunk/user/test/com/google/gwt/i18n/client/DateTimeFormat_en_Test.java     
 
Mon Nov 30 20:39:13 2009
+++ /trunk/user/test/com/google/gwt/i18n/client/DateTimeFormat_en_Test.java     
 
Thu Dec 17 09:24:07 2009
@@ -122,6 +122,48 @@
      Date date = new Date(2006 - 1900, 6, 27, 13, 10, 10);
      assertEquals("J", DateTimeFormat.getFormat("LLLLL").format(date));
    }
+
+  public void test_S() {
+    Date date = new Date(0);
+    assertEquals("0", DateTimeFormat.getFormat("S").format(date));
+
+    date = new Date(55);
+    assertEquals("1", DateTimeFormat.getFormat("S").format(date));
+
+    date = new Date(555);
+    assertEquals("6", DateTimeFormat.getFormat("S").format(date));
+
+    date = new Date(999);
+    assertEquals("9", DateTimeFormat.getFormat("S").format(date));
+  }
+
+  public void test_SS() {
+    Date date = new Date(0);
+    assertEquals("00", DateTimeFormat.getFormat("SS").format(date));
+
+    date = new Date(55);
+    assertEquals("06", DateTimeFormat.getFormat("SS").format(date));
+
+    date = new Date(555);
+    assertEquals("56", DateTimeFormat.getFormat("SS").format(date));
+
+    date = new Date(999);
+    assertEquals("99", DateTimeFormat.getFormat("SS").format(date));
+  }
+
+  public void test_SSS() {
+    Date date = new Date(0);
+    assertEquals("000", DateTimeFormat.getFormat("SSS").format(date));
+
+    date = new Date(55);
+    assertEquals("055", DateTimeFormat.getFormat("SSS").format(date));
+
+    date = new Date(555);
+    assertEquals("555", DateTimeFormat.getFormat("SSS").format(date));
+
+    date = new Date(999);
+    assertEquals("999", DateTimeFormat.getFormat("SSS").format(date));
+  }

    public void test_predefinedFormat() {
      Date date = new Date(2006 - 1900, 7, 4, 13, 49, 24);

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

Reply via email to