]> source.dussan.org Git - poi.git/commitdiff
Fix bug #54557 - Don't mis-detect format patterns like .000 as dates
authorNick Burch <nick@apache.org>
Wed, 13 Feb 2013 16:45:03 +0000 (16:45 +0000)
committerNick Burch <nick@apache.org>
Wed, 13 Feb 2013 16:45:03 +0000 (16:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1445725 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/usermodel/DateUtil.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java

index 12e862d4d1b501532775a37eb52cb6dd610a554f..2a32e47210fe3d4ef6ab350538a66a4df75dc0ab 100644 (file)
@@ -55,7 +55,8 @@ public class DateUtil {
      */
     private static final Pattern date_ptrn1 = Pattern.compile("^\\[\\$\\-.*?\\]");
     private static final Pattern date_ptrn2 = Pattern.compile("^\\[[a-zA-Z]+\\]");
-    private static final Pattern date_ptrn3 = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-/,. :\"\\\\]+0*[ampAMP/]*$");
+    private static final Pattern date_ptrn3a = Pattern.compile("[yYmMdDhHsS]");
+    private static final Pattern date_ptrn3b = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-/,. :\"\\\\]+0*[ampAMP/]*$");
     //  elapsed time patterns: [h],[m] and [s]
     private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]");
 
@@ -364,10 +365,16 @@ public class DateUtil {
            fs = fs.substring(0, fs.indexOf(';'));
         }
 
-        // Otherwise, check it's only made up, in any case, of:
-        //  y m d h s - \ / , . :
+        // Ensure it has some date letters in it
+        // (Avoids false positives on the rest of pattern 3)
+        if (! date_ptrn3a.matcher(fs).find()) {
+           return false;
+        }
+        
+        // If we get here, check it's only made up, in any case, of:
+        //  y m d h s - \ / , . : [ ]
         // optionally followed by AM/PM
-        return date_ptrn3.matcher(fs).matches();
+        return date_ptrn3b.matcher(fs).matches();
     }
 
     /**
index 32f3fe65f929180489197fd419d4c5b4a54b7860..6733f73fa264159d22954d66a00839233b5fb1c6 100644 (file)
@@ -499,5 +499,14 @@ public final class TestHSSFDateUtil extends TestCase {
         assertEquals(valueToTest.getTime(), returnedValue.getTime());
     }
 
-
+    /**
+     * DateUtil.isCellFormatted(Cell) should not true for a numeric cell 
+     * that's formatted as ".0000"
+     */
+    public void testBug54557() throws Exception {
+       final String format = ".0000";
+       boolean isDateFormat = HSSFDateUtil.isADateFormat(165, format);
+       
+       assertEquals(false, isDateFormat);
+    }
 }