+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...
package java.sql;
import java.text.ParseException;
+import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
/**
/**
* 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
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);
/**
* 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);
}
/**
*/
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);
+ }
}
/**
*/
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;
}
*/
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;
}