From c2edd459c77750d868f6874a4b3977e6b9b8aa4e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 23 May 2014 09:18:16 +0000 Subject: [PATCH] Handle date format strings in an iso8601 style format, with a T in them. Fixes bug #54034 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1597038 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/usermodel/DataFormatter.java | 3 +- .../org/apache/poi/ss/usermodel/DateUtil.java | 4 +- .../poi/xssf/usermodel/TestXSSFBugs.java | 45 +++++++++++++++++- test-data/spreadsheet/54034.xlsx | Bin 0 -> 8840 bytes 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 test-data/spreadsheet/54034.xlsx diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index b86b090447..28dbfbe3fb 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -20,8 +20,6 @@ ==================================================================== */ package org.apache.poi.ss.usermodel; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.math.RoundingMode; import java.text.DateFormatSymbols; import java.text.DecimalFormat; @@ -424,6 +422,7 @@ public class DataFormatter { formatStr = formatStr.replaceAll(";@", ""); formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy formatStr = formatStr.replace("\"\"", "'"); // replace Excel quoting with Java style quoting + formatStr = formatStr.replaceAll("\\\\T","'T'"); // Quote the T is iso8601 style dates boolean hasAmPm = false; diff --git a/src/java/org/apache/poi/ss/usermodel/DateUtil.java b/src/java/org/apache/poi/ss/usermodel/DateUtil.java index e4d870de7f..2bc9f01713 100644 --- a/src/java/org/apache/poi/ss/usermodel/DateUtil.java +++ b/src/java/org/apache/poi/ss/usermodel/DateUtil.java @@ -56,7 +56,7 @@ 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_ptrn3a = Pattern.compile("[yYmMdDhHsS]"); - private static final Pattern date_ptrn3b = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-/,. :\"\\\\]+0*[ampAMP/]*$"); + private static final Pattern date_ptrn3b = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-T/,. :\"\\\\]+0*[ampAMP/]*$"); // elapsed time patterns: [h],[m] and [s] private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]"); @@ -445,7 +445,7 @@ public class DateUtil { } // If we get here, check it's only made up, in any case, of: - // y m d h s - \ / , . : [ ] + // y m d h s - \ / , . : [ ] T // optionally followed by AM/PM boolean result = date_ptrn3b.matcher(fs).matches(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index aa8395e02d..71f462361c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -18,7 +18,13 @@ package org.apache.poi.xssf.usermodel; import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -42,7 +48,25 @@ import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.functions.Function; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellValue; +import org.apache.poi.ss.usermodel.ClientAnchor; +import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.Drawing; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.FormulaError; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; @@ -1501,6 +1525,23 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertThat(firstSave, equalTo(secondSave)); } + + /** + * ISO-8601 style cell formats with a T in them, eg + * cell format of "yyyy-MM-ddTHH:mm:ss" + */ + @Test + public void bug54034() throws IOException { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54034.xlsx"); + Sheet sheet = wb.getSheet("Sheet1"); + Row row = sheet.getRow(1); + Cell cell = row.getCell(2); + assertTrue(DateUtil.isCellDateFormatted(cell)); + + DataFormatter fmt = new DataFormatter(); + assertEquals("yyyy\\-mm\\-dd\\Thh:mm", cell.getCellStyle().getDataFormatString()); + assertEquals("2012-08-08T22:59", fmt.formatCellValue(cell)); + } @Test diff --git a/test-data/spreadsheet/54034.xlsx b/test-data/spreadsheet/54034.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..2ae2bebefcdbc1d7a23cd23a6ad4fe590f712dd6 GIT binary patch literal 8840 zcmeHMg6ZAx;xBQKRw{f5bZt8aP3O z@xIM&rPt73O=8q4f$m;W$_cJ342d{@>1xij9wP`tN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