aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/sql
diff options
context:
space:
mode:
authorGary Benson <gbenson@redhat.com>2003-11-11 00:51:44 +0000
committerTom Tromey <tromey@gcc.gnu.org>2003-11-11 00:51:44 +0000
commit84083f51be86a2fdcca225bdb18e4de9d64f35a1 (patch)
treeb8c48c81ef87d72df44557d96975d8ddb7acbf0f /libjava/java/sql
parentb1e0ae3eaac2cf19674b37d1c18c022987f23dac (diff)
downloadgcc-84083f51be86a2fdcca225bdb18e4de9d64f35a1.zip
gcc-84083f51be86a2fdcca225bdb18e4de9d64f35a1.tar.gz
gcc-84083f51be86a2fdcca225bdb18e4de9d64f35a1.tar.bz2
Timestamp.java (valueOf): Correctly handle nanoseconds.
2003-11-10 Gary Benson <gbenson@redhat.com> * java/sql/Timestamp.java (valueOf): Correctly handle nanoseconds. From-SVN: r73437
Diffstat (limited to 'libjava/java/sql')
-rw-r--r--libjava/java/sql/Timestamp.java35
1 files changed, 26 insertions, 9 deletions
diff --git a/libjava/java/sql/Timestamp.java b/libjava/java/sql/Timestamp.java
index be0aa4d..5ec75da 100644
--- a/libjava/java/sql/Timestamp.java
+++ b/libjava/java/sql/Timestamp.java
@@ -58,11 +58,7 @@ public class Timestamp extends java.util.Date
/**
* 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 class Timestamp extends java.util.Date
*/
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 class Timestamp extends java.util.Date
*/
public String toString()
{
- return format_sdf.format(this) + "." + getNanos();
+ return sdf.format(this) + "." + getNanos();
}
/**