Title: [1985] trunk: SqlTimestampConverter does not ignore timezone (XSTR-708).
Revision
1985
Author
joehni
Date
2012-10-13 11:49:17 -0500 (Sat, 13 Oct 2012)

Log Message

SqlTimestampConverter does not ignore timezone (XSTR-708).

Modified Paths


Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/SqlTimestampConverter.java (1984 => 1985)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/SqlTimestampConverter.java	2012-10-05 21:41:03 UTC (rev 1984)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/SqlTimestampConverter.java	2012-10-13 16:49:17 UTC (rev 1985)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2003, 2004 Joe Walnes.
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2012 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -12,22 +12,62 @@
 package com.thoughtworks.xstream.converters.extended;
 
 import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
+import com.thoughtworks.xstream.core.util.ThreadSafeSimpleDateFormat;
 
 import java.sql.Timestamp;
+import java.text.ParseException;
+import java.util.TimeZone;
 
+
 /**
  * Converts a java.sql.Timestamp to text.
- *
+ * 
  * @author Joe Walnes
+ * @author Jörg Schaible
  */
 public class SqlTimestampConverter extends AbstractSingleValueConverter {
 
+    private final ThreadSafeSimpleDateFormat format = new ThreadSafeSimpleDateFormat(
+        "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("UTC"), 0, 5, false);
+
     public boolean canConvert(Class type) {
         return type.equals(Timestamp.class);
     }
 
+    @Override
+    public String toString(Object obj) {
+        Timestamp timestamp = (Timestamp)obj;
+        StringBuffer buffer = new StringBuffer(format.format(timestamp)).append('.');
+        if (timestamp.getNanos() == 0) {
+            buffer.append('0');
+        } else {
+            String nanos = String.valueOf(timestamp.getNanos() + 1000000000);
+            int last = 10;
+            while (last > 2 && nanos.charAt(last-1) == '0')
+                --last;
+            buffer.append(nanos.subSequence(1, last));
+        }
+        return buffer.toString();
+    }
+
     public Object fromString(String str) {
-        return Timestamp.valueOf(str);
+        int idx = str.lastIndexOf('.');
+        if (idx < 0 || str.length() - idx < 2 || str.length() - idx > 10) {
+            throw new IllegalArgumentException(
+                "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]");
+        }
+        try {
+            Timestamp timestamp = new Timestamp(format.parse(str.substring(0, idx)).getTime());
+            StringBuffer buffer = new StringBuffer(str.substring(idx + 1));
+            while(buffer.length() != 9) {
+                buffer.append('0');
+            }
+            timestamp.setNanos(Integer.parseInt(buffer.toString()));
+            return timestamp;
+        } catch (ParseException e) {
+            throw new IllegalArgumentException(
+                "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]");
+        }
     }
 
 }

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/ExtendedTypesTest.java (1984 => 1985)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/ExtendedTypesTest.java	2012-10-05 21:41:03 UTC (rev 1984)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/ExtendedTypesTest.java	2012-10-13 16:49:17 UTC (rev 1985)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2003, 2004, 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2012 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -58,8 +58,10 @@
     }
 
     public void testSqlTimestamp() {
-        assertBothWays(new Timestamp(1234),
-                "<sql-timestamp>1969-12-31 19:00:01.234</sql-timestamp>");
+        Timestamp timestamp = new Timestamp(1234);
+        timestamp.setNanos(78900);
+        assertBothWays(timestamp,
+                "<sql-timestamp>1970-01-01 00:00:01.0000789</sql-timestamp>");
     }
 
     public void testSqlTime() {

Modified: trunk/xstream-distribution/src/content/changes.html (1984 => 1985)


--- trunk/xstream-distribution/src/content/changes.html	2012-10-05 21:41:03 UTC (rev 1984)
+++ trunk/xstream-distribution/src/content/changes.html	2012-10-13 16:49:17 UTC (rev 1985)
@@ -35,6 +35,7 @@
     <h2>Minor changes</h2>
     
     <ul>
+    	<li>JIRA:XSTR-708: SqlTimestampConverter does not ignore timezone.</li>
     	<li>JIRA:XSTR-707: Creation of XmllPullParser with the XmlPullParserFactory may fail in OSGi environment.</li>
     	<li>JIRA:XSTR-705: Unnecessary synchronization accessing the field cache decreases performance.</li>
     </ul>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to