Title: [2340] branches/v-1.4.x: Merge support for W3C datetime from trunk.

Diff

Property changes: branches/v-1.4.x


Modified: svn:mergeinfo

+ /trunk:2151-2152,2154-2156,2158-2163,2165,2172,2175,2177,2188-2189,2197,2199-2201,2204,2206,2210,2212,2214-2217,2226,2229,2231,2233-2234,2236-2238,2247-2249,2279-2280,2285,2291-2292,2294,2298-2299,2301,2303,2306-2310,2312,2314,2316-2318,2320,2322,2324,2326,2329,2333,2335,2339

Modified: branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/basic/DateConverter.java (2339 => 2340)


--- branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/basic/DateConverter.java	2015-02-16 22:58:19 UTC (rev 2339)
+++ branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/basic/DateConverter.java	2015-02-16 23:12:56 UTC (rev 2340)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2003, 2004 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2009, 2011, 2012, 2013, 2014 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2009, 2011, 2012, 2013, 2014, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -11,10 +11,6 @@
  */
 package com.thoughtworks.xstream.converters.basic;
 
-import com.thoughtworks.xstream.converters.ConversionException;
-import com.thoughtworks.xstream.converters.ErrorReporter;
-import com.thoughtworks.xstream.converters.ErrorWriter;
-
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -24,22 +20,30 @@
 import java.util.Locale;
 import java.util.TimeZone;
 
+import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.converters.ErrorReporter;
+import com.thoughtworks.xstream.converters.ErrorWriter;
 import com.thoughtworks.xstream.core.JVM;
 import com.thoughtworks.xstream.core.util.ThreadSafeSimpleDateFormat;
 
 
 /**
- * Converts a java.util.Date to a String as a date format, retaining precision down to
- * milliseconds.
+ * Converts a {@link Date} to a string as a date format, retaining precision down to milliseconds.
+ * <p>
+ * The formatted string is by default in UTC and English locale. You can provide a different {@link Locale} and
+ * {@link TimeZone} that are used for serialization or <code>null</code> to use always the current TimeZone. Note, that
+ * the default format uses 3-letter time zones that can be ambiguous and may cause wrong results at deserialization and
+ * is localized since Java 6.
+ * </p>
+ * <p>
+ * Using a Java 7 runtime or higher, the converter supports the <a href=""
+ * format defined by W3C</a> (a subset of ISO 8601) at deserialization. Only the formats that also contain the time
+ * information.
+ * </p>
+ * <p>
+ * Dates in a different era are using a special default pattern that contains the era itself.
+ * </p>
  * 
- * <p>The formatted string is by default in UTC and English locale. You can provide
- * a different {@link Locale} and {@link TimeZone} that are used for serialization or
- * <code>null</code> to use always the current TimeZone. Note, that the default format uses
- * 3-letter time zones that can be ambiguous and may cause wrong results at deserialization and
- * is localized since Java 6.</p>
- * 
- * <p>Dates in a different era are using a special default pattern that contains the era itself.</p> 
- * 
  * @author Joe Walnes
  * @author J&ouml;rg Schaible
  */
@@ -70,6 +74,11 @@
         if (!utcSupported) {
             acceptablePatterns.add("yyyy-MM-dd HH:mm:ss 'UTC'");
         }
+        if (JVM.canParseISO8601TimeZoneInDateFormat()) {
+            acceptablePatterns.add("yyyy-MM-dd'T'HH:mm:ss.SX");
+            acceptablePatterns.add("yyyy-MM-dd'T'HH:mm:ssX");
+            acceptablePatterns.add("yyyy-MM-dd'T'HH:mmX");
+        }
         // backwards compatibility
         acceptablePatterns.add("yyyy-MM-dd HH:mm:ssa");
         DEFAULT_ACCEPTABLE_FORMATS = (String[]) acceptablePatterns.toArray(new String[acceptablePatterns.size()]);
@@ -83,7 +92,6 @@
     private final ThreadSafeSimpleDateFormat defaultFormat;
     private final ThreadSafeSimpleDateFormat defaultEraFormat;
     private final ThreadSafeSimpleDateFormat[] acceptableFormats;
-    private final Locale locale;
 
     /**
      * Construct a DateConverter with standard formats and lenient set off.
@@ -99,7 +107,7 @@
      * @param timeZone the TimeZone used to serialize the Date
      * @since 1.4
      */
-    public DateConverter(TimeZone timeZone) {
+    public DateConverter(final TimeZone timeZone) {
         this(DEFAULT_PATTERN, DEFAULT_ACCEPTABLE_FORMATS, timeZone);
     }
 
@@ -109,7 +117,7 @@
      * @param lenient the lenient setting of {@link SimpleDateFormat#setLenient(boolean)}
      * @since 1.3
      */
-    public DateConverter(boolean lenient) {
+    public DateConverter(final boolean lenient) {
         this(DEFAULT_PATTERN, DEFAULT_ACCEPTABLE_FORMATS, lenient);
     }
 
@@ -119,7 +127,7 @@
      * @param defaultFormat the default format
      * @param acceptableFormats fallback formats
      */
-    public DateConverter(String defaultFormat, String[] acceptableFormats) {
+    public DateConverter(final String defaultFormat, final String[] acceptableFormats) {
         this(defaultFormat, acceptableFormats, false);
     }
 
@@ -130,7 +138,7 @@
      * @param acceptableFormats fallback formats
      * @since 1.4
      */
-    public DateConverter(String defaultFormat, String[] acceptableFormats, TimeZone timeZone) {
+    public DateConverter(final String defaultFormat, final String[] acceptableFormats, final TimeZone timeZone) {
         this(defaultFormat, acceptableFormats, timeZone, false);
     }
 
@@ -142,7 +150,7 @@
      * @param lenient the lenient setting of {@link SimpleDateFormat#setLenient(boolean)}
      * @since 1.3
      */
-    public DateConverter(String defaultFormat, String[] acceptableFormats, boolean lenient) {
+    public DateConverter(final String defaultFormat, final String[] acceptableFormats, final boolean lenient) {
         this(defaultFormat, acceptableFormats, UTC, lenient);
     }
 
@@ -156,7 +164,7 @@
      * @since 1.4
      */
     public DateConverter(
-        String defaultFormat, String[] acceptableFormats, TimeZone timeZone, boolean lenient) {
+            final String defaultFormat, final String[] acceptableFormats, final TimeZone timeZone, final boolean lenient) {
         this(DEFAULT_ERA_PATTERN, defaultFormat, acceptableFormats, Locale.ENGLISH, timeZone, lenient);
     }
 
@@ -173,9 +181,8 @@
      * @since 1.4.4
      */
     public DateConverter(
-        String defaultEraFormat, String defaultFormat, String[] acceptableFormats,
-        Locale locale, TimeZone timeZone, boolean lenient) {
-        this.locale = locale;
+            final String defaultEraFormat, final String defaultFormat, final String[] acceptableFormats,
+            final Locale locale, final TimeZone timeZone, final boolean lenient) {
         if (defaultEraFormat != null) {
             this.defaultEraFormat = new ThreadSafeSimpleDateFormat(
                 defaultEraFormat, timeZone, locale, 4, 20, lenient);

Modified: branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/core/JVM.java (2339 => 2340)


--- branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/core/JVM.java	2015-02-16 22:58:19 UTC (rev 2339)
+++ branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/core/JVM.java	2015-02-16 23:12:56 UTC (rev 2340)
@@ -44,6 +44,7 @@
     private static final boolean optimizedTreeSetAddAll;
     private static final boolean optimizedTreeMapPutAll;
     private static final boolean canParseUTCDateFormat;
+    private static final boolean canParseISO8601TimeZoneInDateFormat;
     private static final boolean canCreateDerivedObjectOutputStream;
 
     private static final String vendor = System.getProperty("java.vm.vendor");
@@ -150,6 +151,15 @@
         }
         canParseUTCDateFormat = test;
         try {
+            new SimpleDateFormat("X").parse("Z");
+            test = true;
+        } catch (final ParseException e) {
+            test = false;
+        } catch (final IllegalArgumentException e) {
+            test = false;
+        }
+        canParseISO8601TimeZoneInDateFormat = test;
+        try {
             test = new CustomObjectOutputStream(null) != null;
         } catch (RuntimeException e) {
             test = false;
@@ -450,6 +460,13 @@
     }
 
     /**
+     * @since upcoming
+     */
+    public static boolean canParseISO8601TimeZoneInDateFormat() {
+        return canParseISO8601TimeZoneInDateFormat;
+    }
+
+    /**
      * @since 1.4.6
      */
     public static boolean canCreateDerivedObjectOutputStream() {

Modified: branches/v-1.4.x/xstream/src/test/com/thoughtworks/xstream/converters/basic/DateConverterTest.java (2339 => 2340)


--- branches/v-1.4.x/xstream/src/test/com/thoughtworks/xstream/converters/basic/DateConverterTest.java	2015-02-16 22:58:19 UTC (rev 2339)
+++ branches/v-1.4.x/xstream/src/test/com/thoughtworks/xstream/converters/basic/DateConverterTest.java	2015-02-16 23:12:56 UTC (rev 2340)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004, 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2009, 2012, 2014 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2009, 2012, 2014, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -12,6 +12,7 @@
 package com.thoughtworks.xstream.converters.basic;
 
 import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.core.JVM;
 import com.thoughtworks.xstream.testutil.TimeZoneChanger;
 
 import junit.framework.TestCase;
@@ -84,6 +85,15 @@
         assertEquals(expected, converter.fromString("2004-02-22 20:16:04.0 UTC"));
         assertEquals(expected, converter.fromString("2004-02-23 01:46:04.0 IST"));
         assertEquals(expected, converter.fromString("2004-02-23 01:46:04.0 GMT+05:30"));
+        
+        if (JVM.canParseISO8601TimeZoneInDateFormat()) {
+            // W3C subset of ISO 8601 date time representations
+            assertEquals(expected, converter.fromString("2004-02-22T15:16:04-05:00"));
+            assertEquals(expected, converter.fromString("2004-02-22T20:16:04Z"));
+            assertEquals(expected, converter.fromString("2004-02-22T20:16:04.0Z"));
+            expected.setTime(expected.getTime()-4000);
+            assertEquals(expected, converter.fromString("2004-02-22T15:16-05:00"));
+        }
     }
 
     public void testUnmarshalsDateWithDifferentDefaultTimeZones() throws ParseException {

Modified: branches/v-1.4.x/xstream-distribution/src/content/changes.html (2339 => 2340)


--- branches/v-1.4.x/xstream-distribution/src/content/changes.html	2015-02-16 22:58:19 UTC (rev 2339)
+++ branches/v-1.4.x/xstream-distribution/src/content/changes.html	2015-02-16 23:12:56 UTC (rev 2340)
@@ -45,6 +45,8 @@
     	<li>XSTR-761: Support ignored serialPersistentField at deserialization time.</li>
     	<li>XSTR-755: ExternalizableConverter does not respect writeReplace and readResolve.</li>
     	<li>XSTR-757: Deserialized TreeSet does not honor remove(Object) return value contract.</li>
+    	<li>XSTR-759: Support deserialization of <a href="" datetime format</a>
+    	in DateConverter with Java 7 runtime.</li>
     	<li>Fix: DateConverter ignores provided locale.</li>
     	<li>Fix: WeakCache.entrySet().iterator().next.setValue(value) returns the reference instead of the old value.</li>
     	<li>Fix: SqlTimestampConverter throws IllegalArgumentException instead of ConversionException on fromString().</li>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to