From 84083f51be86a2fdcca225bdb18e4de9d64f35a1 Mon Sep 17 00:00:00 2001 From: Gary Benson Date: Tue, 11 Nov 2003 00:51:44 +0000 Subject: [PATCH] Timestamp.java (valueOf): Correctly handle nanoseconds. 2003-11-10 Gary Benson * java/sql/Timestamp.java (valueOf): Correctly handle nanoseconds. From-SVN: r73437 --- libjava/ChangeLog | 5 +++++ libjava/java/sql/Timestamp.java | 35 ++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 2524000..0d62f8f 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,8 @@ +2003-11-10 Gary Benson + + * java/sql/Timestamp.java (valueOf): Correctly handle + nanoseconds. + 2003-11-09 Tom Tromey * java/net/Inet4Address.java (serialVersionUID): Updated. 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(); } /** -- 2.7.4