You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/12/17 08:21:25 UTC

svn commit: r891572 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/time/DateUtils.java test/org/apache/commons/lang3/time/DateUtilsTest.java

Author: bayard
Date: Thu Dec 17 07:21:25 2009
New Revision: 891572

URL: http://svn.apache.org/viewvc?rev=891572&view=rev
Log:
Applying 'fix' for LANG-530. DateUtils.parseDate now protects the common use case of FastDateFormat ZZ output, namely ZZ on the end of the pattern, from being passed to SimpleDateFormat as is. Use of ZZ elsewhere in the pattern isn't protected and will want to consider emulating the String changes made in this patch. 

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java?rev=891572&r1=891571&r2=891572&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java Thu Dec 17 07:21:25 2009
@@ -290,14 +290,29 @@
         SimpleDateFormat parser = null;
         ParsePosition pos = new ParsePosition(0);
         for (int i = 0; i < parsePatterns.length; i++) {
+
+            String pattern = parsePatterns[i];
+
+            // LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
+            if (parsePatterns[i].endsWith("ZZ")) {
+                pattern = pattern.substring(0, pattern.length() - 1);
+            }
+            
             if (i == 0) {
-                parser = new SimpleDateFormat(parsePatterns[0]);
+                parser = new SimpleDateFormat(pattern);
             } else {
-                parser.applyPattern(parsePatterns[i]); // cannot be null if i != 0
+                parser.applyPattern(pattern); // cannot be null if i != 0
             }
             pos.setIndex(0);
-            Date date = parser.parse(str, pos);
-            if (date != null && pos.getIndex() == str.length()) {
+
+            String str2 = str;
+            // LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
+            if (parsePatterns[i].endsWith("ZZ")) {
+                str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2"); 
+            }
+
+            Date date = parser.parse(str2, pos);
+            if (date != null && pos.getIndex() == str2.length()) {
                 return date;
             }
         }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java?rev=891572&r1=891571&r2=891572&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java Thu Dec 17 07:21:25 2009
@@ -1156,6 +1156,15 @@
         // restore default time zone
         TimeZone.setDefault(defaultZone);
     }
+
+    // http://issues.apache.org/jira/browse/LANG-520
+    public void testLang520() throws ParseException {
+        Date d = new Date();
+        String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
+        Date d2 = DateUtils.parseDate(isoDateStr, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() });
+        // the format loses milliseconds so have to reintroduce them
+        assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000); 
+    }
     
     /**
      * Tests various values with the ceiling method



Re: svn commit: r891572 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/time/DateUtils.java test/org/apache/commons/lang3/time/DateUtilsTest.java

Posted by Henri Yandell <fl...@gmail.com>.
Unless we implement FastDateFormat.parseObject fully, I couldn't see
any good paths here and chose the least bad path. I think this change
helps the common use case without incurring any pain on the abnormal
use case (apart from the two endsWith checks). Opinions welcome as
this didn't feel beautiful.

On Wed, Dec 16, 2009 at 11:21 PM,  <ba...@apache.org> wrote:
> Author: bayard
> Date: Thu Dec 17 07:21:25 2009
> New Revision: 891572
>
> URL: http://svn.apache.org/viewvc?rev=891572&view=rev
> Log:
> Applying 'fix' for LANG-530. DateUtils.parseDate now protects the common use case of FastDateFormat ZZ output, namely ZZ on the end of the pattern, from being passed to SimpleDateFormat as is. Use of ZZ elsewhere in the pattern isn't protected and will want to consider emulating the String changes made in this patch.
>
> Modified:
>    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java
>    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java
>
> Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java
> URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java?rev=891572&r1=891571&r2=891572&view=diff
> ==============================================================================
> --- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java (original)
> +++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java Thu Dec 17 07:21:25 2009
> @@ -290,14 +290,29 @@
>         SimpleDateFormat parser = null;
>         ParsePosition pos = new ParsePosition(0);
>         for (int i = 0; i < parsePatterns.length; i++) {
> +
> +            String pattern = parsePatterns[i];
> +
> +            // LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
> +            if (parsePatterns[i].endsWith("ZZ")) {
> +                pattern = pattern.substring(0, pattern.length() - 1);
> +            }
> +
>             if (i == 0) {
> -                parser = new SimpleDateFormat(parsePatterns[0]);
> +                parser = new SimpleDateFormat(pattern);
>             } else {
> -                parser.applyPattern(parsePatterns[i]); // cannot be null if i != 0
> +                parser.applyPattern(pattern); // cannot be null if i != 0
>             }
>             pos.setIndex(0);
> -            Date date = parser.parse(str, pos);
> -            if (date != null && pos.getIndex() == str.length()) {
> +
> +            String str2 = str;
> +            // LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
> +            if (parsePatterns[i].endsWith("ZZ")) {
> +                str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
> +            }
> +
> +            Date date = parser.parse(str2, pos);
> +            if (date != null && pos.getIndex() == str2.length()) {
>                 return date;
>             }
>         }
>
> Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java
> URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java?rev=891572&r1=891571&r2=891572&view=diff
> ==============================================================================
> --- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java (original)
> +++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java Thu Dec 17 07:21:25 2009
> @@ -1156,6 +1156,15 @@
>         // restore default time zone
>         TimeZone.setDefault(defaultZone);
>     }
> +
> +    // http://issues.apache.org/jira/browse/LANG-520
> +    public void testLang520() throws ParseException {
> +        Date d = new Date();
> +        String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
> +        Date d2 = DateUtils.parseDate(isoDateStr, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() });
> +        // the format loses milliseconds so have to reintroduce them
> +        assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000);
> +    }
>
>     /**
>      * Tests various values with the ceiling method
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org