[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
This commit is contained in:
PJ Fanning 2021-07-28 16:56:36 +00:00
parent cec2d2b4cc
commit 01b94f1817
2 changed files with 21 additions and 10 deletions

View File

@ -17,14 +17,14 @@
package org.apache.poi.ss.formula.functions; 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.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 org.apache.poi.ss.usermodel.DateUtil;
import java.time.DateTimeException;
/** /**
* Implementation for the DATEVALUE() Excel function.<p> * Implementation for the DATEVALUE() Excel function.<p>
* *
@ -44,6 +44,8 @@ import org.apache.poi.ss.usermodel.DateUtil;
*/ */
public class DateValue extends Fixed1ArgFunction { public class DateValue extends Fixed1ArgFunction {
private static final Logger LOG = LogManager.getLogger(DateValue.class);
@Override @Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval dateTextArg) { public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval dateTextArg) {
try { try {
@ -55,6 +57,9 @@ public class DateValue extends Fixed1ArgFunction {
} }
return new NumberEval(DateUtil.getExcelDate(DateParser.parseLocalDate(dateText))); 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) { } catch (EvaluationException e) {
return e.getErrorEval(); return e.getErrorEval();
} }

View File

@ -25,11 +25,7 @@ import java.time.Year;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Locale; import java.util.Locale;
import org.apache.poi.ss.formula.eval.BlankEval; import org.apache.poi.ss.formula.eval.*;
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.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; 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) { private ValueEval invokeDateValue(ValueEval text) {
return new DateValue().evaluate(0, 0, text); return new DateValue().evaluate(0, 0, text);
} }