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
This commit is contained in:
Gary Benson 2003-11-11 00:51:44 +00:00 committed by Tom Tromey
parent b1e0ae3eaa
commit 84083f51be
2 changed files with 31 additions and 9 deletions

View File

@ -1,3 +1,8 @@
2003-11-10 Gary Benson <gbenson@redhat.com>
* java/sql/Timestamp.java (valueOf): Correctly handle
nanoseconds.
2003-11-09 Tom Tromey <tromey@redhat.com> 2003-11-09 Tom Tromey <tromey@redhat.com>
* java/net/Inet4Address.java (serialVersionUID): Updated. * java/net/Inet4Address.java (serialVersionUID): Updated.

View File

@ -58,11 +58,7 @@ public class Timestamp extends java.util.Date
/** /**
* Used for parsing and formatting this date. * Used for parsing and formatting this date.
*/ */
// Millisecond will have to be close enough for now. private static SimpleDateFormat sdf =
private static SimpleDateFormat parse_sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
private static SimpleDateFormat format_sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 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) 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 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) if (d == null)
throw new IllegalArgumentException(str); 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) catch (ParseException e)
{ {
@ -133,7 +150,7 @@ public class Timestamp extends java.util.Date
*/ */
public String toString() public String toString()
{ {
return format_sdf.format(this) + "." + getNanos(); return sdf.format(this) + "." + getNanos();
} }
/** /**