Author: gwoolsey
Date: Sat Mar 30 20:13:24 2019
New Revision: 1856648

URL: http://svn.apache.org/viewvc?rev=1856648&view=rev
Log:
#63292 1904 date windowing flag not used for some format cases

Modified:
    poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java?rev=1856648&r1=1856647&r2=1856648&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java Sat Mar 
30 20:13:24 2019
@@ -49,6 +49,7 @@ import org.apache.poi.ss.util.NumberToTe
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 
 /**
@@ -306,10 +307,17 @@ public class DataFormatter implements Ob
         if(formatStr == null || formatStr.trim().length() == 0) {
             return null;
         }
-        return getFormat(cell.getNumericCellValue(), formatIndex, formatStr);
+        return getFormat(cell.getNumericCellValue(), formatIndex, formatStr, 
isDate1904(cell));
     }
 
-    private Format getFormat(double cellValue, int formatIndex, String 
formatStrIn) {
+    private boolean isDate1904(Cell cell) {
+        if (cell == null || ! (cell.getSheet().getWorkbook() instanceof 
XSSFWorkbook) ) {
+            return false;
+        }
+        return ((XSSFWorkbook) cell.getSheet().getWorkbook()).isDate1904();
+    }
+    
+    private Format getFormat(double cellValue, int formatIndex, String 
formatStrIn, boolean use1904Windowing) {
         localeChangedObservable.checkForLocaleChange();
 
         // Might be better to separate out the n p and z formats, falling back 
to p when n and z are not set.
@@ -338,7 +346,7 @@ public class DataFormatter implements Ob
                 if (DateUtil.isADateFormat(formatIndex, formatStr) && 
                         // don't try to handle Date value 0, let a 3 or 4-part 
format take care of it 
                         ((Double)cellValueO).doubleValue() != 0.0) {
-                    cellValueO = DateUtil.getJavaDate(cellValue);
+                    cellValueO = DateUtil.getJavaDate(cellValue, 
use1904Windowing);
                 }
                 // Wrap and return (non-cachable - CellFormat does that)
                 return new CellFormatResultWrapper( cfmt.apply(cellValueO) );
@@ -788,7 +796,7 @@ public class DataFormatter implements Ob
         }
         return generalNumberFormat;
     }
-    
+
     /**
      * Performs Excel-style date formatting, using the
      *  supplied Date and format
@@ -875,7 +883,7 @@ public class DataFormatter implements Ob
         // Is it a date?
         if(DateUtil.isADateFormat(formatIndex,formatString)) {
             if(DateUtil.isValidExcelDate(value)) {
-                Format dateFormat = getFormat(value, formatIndex, 
formatString);
+                Format dateFormat = getFormat(value, formatIndex, 
formatString, use1904Windowing);
                 if(dateFormat instanceof ExcelStyleDateFormatter) {
                     // Hint about the raw excel value
                     
((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(value);
@@ -890,7 +898,7 @@ public class DataFormatter implements Ob
         }
         
         // else Number
-        Format numberFormat = getFormat(value, formatIndex, formatString);
+        Format numberFormat = getFormat(value, formatIndex, formatString, 
use1904Windowing);
         if (numberFormat == null) {
             return String.valueOf(value);
         }

Modified: 
poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java?rev=1856648&r1=1856647&r2=1856648&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java 
(original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java 
Sat Mar 30 20:13:24 2019
@@ -936,6 +936,37 @@ public class TestDataFormatter {
         String value = df.formatCellValue(cell, 
wb.getCreationHelper().createFormulaEvaluator());
         assertEquals("-130", value);
     }
+    
+    /**
+     * Bug #63292
+     */
+    @Test
+    public void test1904With4PartFormat() {
+        Date date = new Date();
+        int formatIndex = 105;
+        String formatString1 = "[$-F400]m/d/yy h:mm:ss\\ AM/PM";
+        String formatString4 = "[$-F400]m/d/yy h:mm:ss\\ AM/PM;[$-F400]m/d/yy 
h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-";
+
+        String s1900, s1904;
+
+        // These two format calls return the same thing, as expected:
+        // The assertEquals() passes with 1-part format
+        s1900 = format(date, formatIndex, formatString1, false);
+        s1904 = format(date, formatIndex, formatString1, true);
+        assertEquals(s1900, s1904);  // WORKS
+
+        // These two format calls should return the same thing but don't:
+        // It fails with 4-part format because the call to CellFormat ignores 
'use1904Windowing'
+        s1900 = format(date, formatIndex, formatString4, false);
+        s1904 = format(date, formatIndex, formatString4, true);
+        assertEquals(s1900, s1904);  // FAILS before fix for #63292
+    }
+
+    private String format(Date date, int formatIndex, String formatString, 
boolean use1904Windowing) {
+        DataFormatter formatter = new DataFormatter();
+        double n = DateUtil.getExcelDate(date, use1904Windowing);
+        return formatter.formatRawCellContents(n, formatIndex, formatString, 
use1904Windowing);
+    }
 
     @Test
     public void testConcurrentCellFormat() throws Exception {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to