commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject 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
Date Thu, 17 Dec 2009 07:21:25 GMT
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



Mime
View raw message