re PR libgcj/16574 (java.sql.Timestamp#toString() does not deal with fractional seconds correctly)

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.

From-SVN: r84784
This commit is contained in:
Bryce McKinlay 2004-07-15 22:14:45 +00:00 committed by Bryce McKinlay
parent e4f78bd4af
commit cbd7a79bc3
2 changed files with 46 additions and 15 deletions

View 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...

View 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;
}