Revision: 6929
Author: [email protected]
Date: Mon Nov 16 13:52:38 2009
Log: Fix formatting of milliseconds in pre-1970 (i.e., negative) dates

Review by: jat


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

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 Jul 
 
14 10:17:53 2009
+++ /trunk/user/src/com/google/gwt/i18n/client/DateTimeFormat.java      Mon Nov 
 
16 13:52:38 2009
@@ -989,7 +989,13 @@
      // 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".
-    int value = (int) (date.getTime() % 1000);
+    long time = date.getTime();
+    int value;
+    if (time < 0) {
+      value = 1000 - (int) (-time % 1000);
+    } else {
+      value = (int) (time % 1000);
+    }
      if (count == 1) {
        value = (value + 50) / 100; // Round to 100ms.
        buf.append(Integer.toString(value));
=======================================
--- /trunk/user/test/com/google/gwt/i18n/client/DateTimeFormat_en_Test.java     
 
Mon Nov 24 14:39:21 2008
+++ /trunk/user/test/com/google/gwt/i18n/client/DateTimeFormat_en_Test.java     
 
Mon Nov 16 13:52:38 2009
@@ -22,7 +22,7 @@
  import java.util.Date;

  /**
- * Tests formatting functionality in {...@link DateTimeFormat} for the German
+ * Tests formatting functionality in {...@link DateTimeFormat} for the English
   * language.
   */
  public class DateTimeFormat_en_Test extends GWTTestCase {
@@ -415,4 +415,16 @@
      assertEquals("10/29/2006 01:00:00 PST", DateTimeFormat.getFormat(
          "MM/dd/yyyy HH:mm:ss z").format(date, usPacific));
    }
-}
+
+  public void testPre1970Milliseconds() {
+    Date date = new Date(-631151998945L); // Jan 1, 1950 00:00:01.055 UTC
+
+    long midnight = Date.UTC(1950 - 1900, 0, 1, 0, 0, 1);
+    assertEquals(-631151998945L, midnight + 55);
+
+    TimeZone utc = TimeZone.createTimeZone(0);
+    assertEquals("055", DateTimeFormat.getFormat("SSS").format(date, utc));
+    assertEquals("06", DateTimeFormat.getFormat("SS").format(date, utc));
+    assertEquals("1", DateTimeFormat.getFormat("S").format(date, utc));
+  }
+}

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

Reply via email to