OSDN Git Service

2004-07-15 Bryce McKinlay <mckinlay@redhat.com>
authorbryce <bryce@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 15 Jul 2004 22:14:45 +0000 (22:14 +0000)
committerbryce <bryce@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 15 Jul 2004 22:14:45 +0000 (22:14 +0000)
PR libgcj/16574
* java/sql/Timestamp.java (dateFormat): Renamed from sdf.
(decimalFormat): New static variable.
(sbuf): Likewise.
(getTime): New. Override Date.getTime().
(toString): Synchronize. Use decimalFormat to format nanos value
correctly. Truncate extra zeros.
(before): Compare getNanos() only if getTime() is equal.
(after): Likewise.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@84784 138bc75d-0d04-0410-961f-82ee72b054a4

libjava/ChangeLog
libjava/java/sql/Timestamp.java

index ae51a6d..d6d6358 100644 (file)
@@ -1,3 +1,15 @@
+2004-07-15  Bryce McKinlay  <mckinlay@redhat.com>
+
+       PR libgcj/16574
+       * java/sql/Timestamp.java (dateFormat): Renamed from sdf.
+       (decimalFormat): New static variable.
+       (sbuf): Likewise.
+       (getTime): New. Override Date.getTime().
+       (toString): Synchronize. Use decimalFormat to format nanos value
+       correctly. Truncate extra zeros.
+       (before): Compare getNanos() only if getTime() is equal.
+       (after): Likewise.
+       
 2004-07-14  Nathanael Nerode  <neroden@gcc.gnu.org>
 
        * acinclude.m4: "Inline" LIBGCJ_CONFIGURE macro into...
index 22108f5..e480fff 100644 (file)
@@ -39,6 +39,7 @@ exception statement from your version. */
 package java.sql;
 
 import java.text.ParseException;
+import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 
 /**
@@ -58,8 +59,10 @@ public class Timestamp extends java.util.Date
   /**
    * Used for parsing and formatting this date.
    */
-  private static SimpleDateFormat sdf =
+  private static SimpleDateFormat dateFormat =
     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+  private static DecimalFormat decimalFormat = new DecimalFormat("000000000");
+  private static StringBuffer sbuf = new StringBuffer(29);
 
   /**
     * The nanosecond value for this object
@@ -96,7 +99,7 @@ public class Timestamp extends java.util.Date
 
     try
       {
-       java.util.Date d = (java.util.Date)sdf.parseObject(str);
+       java.util.Date d = (java.util.Date) dateFormat.parseObject(str);
 
        if (d == null)
          throw new IllegalArgumentException(str);
@@ -133,14 +136,24 @@ public class Timestamp extends java.util.Date
 
   /**
    * This method initializes a new instance of this class with the
-   * specified time value representing the number of seconds since 
+   * specified time value representing the number of milliseconds since 
    * Jan 1, 1970 at 12:00 midnight GMT.
    *
    * @param time The time value to intialize this <code>Time</code> to.
    */
   public Timestamp(long date)
   {
-    super(date);
+    super(date - (date % 1000));
+    nanos = (int) (date % 1000) * 1000000;
+  }
+
+  /**
+   * Return the value of this Timestamp as the number of milliseconds 
+   * since Jan 1, 1970 at 12:00 midnight GMT.
+   */
+  public long getTime()
+  {
+    return super.getTime() + (nanos / 1000000);
   }
 
   /**
@@ -150,7 +163,17 @@ public class Timestamp extends java.util.Date
    */
   public String toString()
   {
-    return sdf.format(this) + "." + getNanos();
+    synchronized (dateFormat)
+      {
+        sbuf.setLength(0);
+       dateFormat.format(this, sbuf, null);
+       sbuf.append('.');
+       decimalFormat.format(nanos, sbuf, null);
+       int end = sbuf.length() - 1;
+       while (end > 20 && sbuf.charAt(end) == '0')
+         end--;
+       return sbuf.substring(0, end + 1);
+      }
   }
 
   /**
@@ -182,12 +205,10 @@ public class Timestamp extends java.util.Date
    */
   public boolean before(Timestamp ts)
   {
-    if (ts.getTime() > getTime())
+    long time1 = getTime();
+    long time2 = ts.getTime();
+    if (time1 < time2 || (time1 == time2 && getNanos() < ts.getNanos()))
       return true;
-
-    if (ts.getNanos() > getNanos())
-      return true;
-
     return false;
   }
 
@@ -202,12 +223,10 @@ public class Timestamp extends java.util.Date
    */
   public boolean after(Timestamp ts)
   {
-    if (ts.getTime() < getTime())
+    long time1 = getTime();
+    long time2 = ts.getTime();
+    if (time1 > time2 || (time1 == time2 && getNanos() > ts.getNanos()))
       return true;
-
-    if (ts.getNanos() < getNanos())
-      return true;
-
     return false;
   }