aboutsummaryrefslogtreecommitdiffstats
path: root/poi/src
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-07-28 16:56:36 +0000
committerPJ Fanning <fanningpj@apache.org>2021-07-28 16:56:36 +0000
commit01b94f181719814e1e1a1e84005cd21b61686e2e (patch)
tree11adc4c02ae04ed8b7231d87c560092162cf3681 /poi/src
parentcec2d2b4cc345434f6bb8043c5f8a130bab30f41 (diff)
downloadpoi-01b94f181719814e1e1a1e84005cd21b61686e2e.tar.gz
poi-01b94f181719814e1e1a1e84005cd21b61686e2e.zip
[bug-65165] invalid date values not handled correctly in DateValue function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891858 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java15
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java16
2 files changed, 21 insertions, 10 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java
index 57a6733f6e..79c90cc23f 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java
@@ -17,14 +17,14 @@
package org.apache.poi.ss.formula.functions;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.util.DateParser;
-import org.apache.poi.ss.formula.eval.BlankEval;
-import org.apache.poi.ss.formula.eval.EvaluationException;
-import org.apache.poi.ss.formula.eval.NumberEval;
-import org.apache.poi.ss.formula.eval.OperandResolver;
-import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.DateUtil;
+import java.time.DateTimeException;
+
/**
* Implementation for the DATEVALUE() Excel function.<p>
*
@@ -44,6 +44,8 @@ import org.apache.poi.ss.usermodel.DateUtil;
*/
public class DateValue extends Fixed1ArgFunction {
+ private static final Logger LOG = LogManager.getLogger(DateValue.class);
+
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval dateTextArg) {
try {
@@ -55,6 +57,9 @@ public class DateValue extends Fixed1ArgFunction {
}
return new NumberEval(DateUtil.getExcelDate(DateParser.parseLocalDate(dateText)));
+ } catch (DateTimeException dte) {
+ LOG.atInfo().log("Failed to parse date", dte);
+ return ErrorEval.VALUE_INVALID;
} catch (EvaluationException e) {
return e.getErrorEval();
}
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java
index 9738546998..ae0ae805c3 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java
@@ -25,11 +25,7 @@ import java.time.Year;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
-import org.apache.poi.ss.formula.eval.BlankEval;
-import org.apache.poi.ss.formula.eval.ErrorEval;
-import org.apache.poi.ss.formula.eval.NumberEval;
-import org.apache.poi.ss.formula.eval.StringEval;
-import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.util.LocaleUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
@@ -88,6 +84,16 @@ final class TestDateValue {
}
}
+ @Test
+ void testInvalidDateValue() {
+ assertEquals(ErrorEval.VALUE_INVALID, invokeDateValue(new StringEval("not-date")),
+ "not-date evals to invalid");
+ assertEquals(ErrorEval.VALUE_INVALID, invokeDateValue(BoolEval.FALSE),
+ "false evals to invalid");
+ assertEquals(ErrorEval.VALUE_INVALID, invokeDateValue(new NumberEval(Math.E)),
+ "Math.E evals to invalid");
+ }
+
private ValueEval invokeDateValue(ValueEval text) {
return new DateValue().evaluate(0, 0, text);
}