From: Bryce McKinlay Date: Thu, 15 Jul 2004 22:14:45 +0000 (+0000) Subject: re PR libgcj/16574 (java.sql.Timestamp#toString() does not deal with fractional secon... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=cbd7a79bc312c93c7081209c2e65024d016f662e;p=gcc.git re PR libgcj/16574 (java.sql.Timestamp#toString() does not deal with fractional seconds correctly) 2004-07-15 Bryce McKinlay 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 --- diff --git a/libjava/ChangeLog b/libjava/ChangeLog index ae51a6d4136..d6d635804ec 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,15 @@ +2004-07-15 Bryce McKinlay + + 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 * acinclude.m4: "Inline" LIBGCJ_CONFIGURE macro into... diff --git a/libjava/java/sql/Timestamp.java b/libjava/java/sql/Timestamp.java index 22108f58f93..e480fff6be8 100644 --- a/libjava/java/sql/Timestamp.java +++ b/libjava/java/sql/Timestamp.java @@ -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 Time 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; }