From 82f989991d7a6fed862226f8f640e0919d2aa95a Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 28 Jul 2021 19:46:05 +0000 Subject: [PATCH] [bug-65471] fix DataFormatter so it can handle Ts (ISO 8601) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891862 13f79535-47bb-0310-9956-ffa450edef68 --- .../ss/usermodel/ExcelStyleDateFormatter.java | 2 ++ .../poi/ss/formula/functions/TestText.java | 32 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java b/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java index fcaed994d5..4b7ecc957c 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java @@ -98,6 +98,8 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { t = t.replace("[mm]", String.valueOf(MM_BRACKET_SYMBOL)); t = t.replace("[s]", String.valueOf(S_BRACKET_SYMBOL)); t = t.replace("[ss]", String.valueOf(SS_BRACKET_SYMBOL)); + t = t.replace("T", "'T'"); + t = t.replace("''T''", "'T'"); t = t.replaceAll("s.000", "s.SSS"); t = t.replaceAll("s.00", "s." + LL_BRACKET_SYMBOL); t = t.replaceAll("s.0", "s." + L_BRACKET_SYMBOL); diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java index cbbfaad11f..7db63331b9 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java @@ -123,14 +123,14 @@ final class TestText { // Again with Java style formatArg = new StringEval("MMMM dd, yyyy"); args[1] = formatArg; - result = TextFunction.TEXT.evaluate(args, -1, (short)-1); + result = TextFunction.TEXT.evaluate(args, -1, -1); testResult = new StringEval(november + " 16, 1900"); assertEquals(testResult.toString(), result.toString()); // And Excel style formatArg = new StringEval("mmmm dd, yyyy"); args[1] = formatArg; - result = TextFunction.TEXT.evaluate(args, -1, (short)-1); + result = TextFunction.TEXT.evaluate(args, -1, -1); testResult = new StringEval(november + " 16, 1900"); assertEquals(testResult.toString(), result.toString()); } finally { @@ -138,22 +138,22 @@ final class TestText { } } - @Disabled("see https://bz.apache.org/bugzilla/show_bug.cgi?id=65471") @Test void testTextWithISODateTimeFormatSecondArg() { - TimeZone userTZ = LocaleUtil.getUserTimeZone(); - LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET")); - try { - // Test with Java style M=Month - ValueEval numArg = new NumberEval(321.321); - ValueEval formatArg = new StringEval("yyyy-mm-ddThh:MM:ss"); - ValueEval[] args = { numArg, formatArg }; - ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short)-1); - ValueEval testResult = new StringEval("1900-11-16T07:42:14"); - assertEquals(testResult.toString(), result.toString()); - } finally { - LocaleUtil.setUserTimeZone(userTZ); - } + ValueEval numArg = new NumberEval(321.321); + ValueEval formatArg = new StringEval("yyyy-mm-ddThh:MM:ss"); + ValueEval[] args = { numArg, formatArg }; + ValueEval result = TextFunction.TEXT.evaluate(args, -1, -1); + ValueEval testResult = new StringEval("1900-11-16T07:42:14"); + assertEquals(testResult.toString(), result.toString()); + + // test milliseconds + formatArg = new StringEval("yyyy-mm-ddThh:MM:ss.000"); + args[1] = formatArg; + result = TextFunction.TEXT.evaluate(args, -1, -1); + testResult = new StringEval("16:11:1900 07:42:14.400"); + assertEquals(testResult.toString(), result.toString()); + } } -- 2.39.5