This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: FYI:
- From: Tom Tromey <tromey at redhat dot com>
- To: GCC libjava patches <java-patches at gcc dot gnu dot org>
- Date: 10 Nov 2003 17:41:57 -0700
- Subject: Patch: FYI:
- Reply-to: tromey at redhat dot com
I'm checking this in on the trunk and in Classpath.
This changes java.sql.Timestamp.valueOf to correctly handle
nanoseconds.
Tom
Index: ChangeLog
from Gary Benson <gbenson@redhat.com>
* java/sql/Timestamp.java (valueOf): Correctly handle
nanoseconds.
Index: java/sql/Timestamp.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/sql/Timestamp.java,v
retrieving revision 1.7
diff -u -r1.7 Timestamp.java
--- java/sql/Timestamp.java 18 Sep 2003 06:11:41 -0000 1.7
+++ java/sql/Timestamp.java 11 Nov 2003 00:49:18 -0000
@@ -58,11 +58,7 @@
/**
* Used for parsing and formatting this date.
*/
- // Millisecond will have to be close enough for now.
- private static SimpleDateFormat parse_sdf =
- new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
-
- private static SimpleDateFormat format_sdf =
+ private static SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
@@ -79,14 +75,35 @@
*/
public static Timestamp valueOf(String str)
{
+ int nanos = 0;
+ int dot = str.indexOf('.');
+ if (dot != -1)
+ {
+ if (str.lastIndexOf('.') != dot)
+ throw new IllegalArgumentException(str);
+
+ int len = str.length() - dot - 1;
+ if (len < 1 || len > 9)
+ throw new IllegalArgumentException(str);
+
+ nanos = Integer.parseInt(str.substring(dot + 1));
+ for (int i = len; i < 9; i++)
+ nanos *= 10;
+
+ str = str.substring(0, dot);
+
+ }
+
try
{
- java.util.Date d = (java.util.Date)parse_sdf.parseObject(str);
+ java.util.Date d = (java.util.Date)sdf.parseObject(str);
if (d == null)
throw new IllegalArgumentException(str);
- else
- return new Timestamp(d.getTime());
+
+ Timestamp ts = new Timestamp(d.getTime() + nanos / 1000000);
+ ts.nanos = nanos;
+ return ts;
}
catch (ParseException e)
{
@@ -133,7 +150,7 @@
*/
public String toString()
{
- return format_sdf.format(this) + "." + getNanos();
+ return sdf.format(this) + "." + getNanos();
}
/**