summaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-05-28 18:26:21 +0000
committerPJ Fanning <fanningpj@apache.org>2022-05-28 18:26:21 +0000
commitd836ff9a39b36da9454f6925f3933c0be5eaaf30 (patch)
tree3e293574a0439b51e07ce009085e43f8f2660138 /poi
parent00f6289b2f14f3c69e8893b4df62ad30c005ea20 (diff)
downloadpoi-d836ff9a39b36da9454f6925f3933c0be5eaaf30.tar.gz
poi-d836ff9a39b36da9454f6925f3933c0be5eaaf30.zip
DAYS360 function needs to support dates provided as strings as well as a nums
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901364 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java2
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/Days360.java17
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestDays360.java19
3 files changed, 29 insertions, 9 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java
index a5a55943b2..cce8180890 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java
@@ -73,7 +73,7 @@ public class Days implements FreeRefFunction {
return ChronoUnit.DAYS.between(startDate, endDate);
}
- private static LocalDate getDate(ValueEval eval, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
+ static LocalDate getDate(ValueEval eval, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
ValueEval ve = OperandResolver.getSingleValue(eval, srcRowIndex, srcColumnIndex);
try {
double d0 = NumericFunction.singleOperandEvaluate(ve, srcRowIndex, srcColumnIndex);
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Days360.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Days360.java
index e86b23be89..cfb4fe8194 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Days360.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Days360.java
@@ -16,6 +16,7 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
+import java.time.LocalDate;
import java.util.Calendar;
import org.apache.poi.ss.formula.eval.EvaluationException;
@@ -69,23 +70,23 @@ public class Days360 extends Var2or3ArgFunction {
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
try {
- double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
- double d1 = NumericFunction.singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
- return new NumberEval(evaluate(d0, d1, false));
+ LocalDate d0 = Days.getDate(arg0, srcRowIndex, srcColumnIndex);
+ LocalDate d1 = Days.getDate(arg1, srcRowIndex, srcColumnIndex);
+ return new NumberEval(evaluate(DateUtil.getExcelDate(d0), DateUtil.getExcelDate(d1), false));
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
@Override
- public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
- ValueEval arg2) {
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
try {
- double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
- double d1 = NumericFunction.singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
+ LocalDate d0 = Days.getDate(arg0, srcRowIndex, srcColumnIndex);
+ LocalDate d1 = Days.getDate(arg1, srcRowIndex, srcColumnIndex);
ValueEval ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex);
Boolean method = OperandResolver.coerceValueToBoolean(ve, false);
- return new NumberEval(evaluate(d0, d1, method != null && method.booleanValue()));
+ return new NumberEval(evaluate(DateUtil.getExcelDate(d0), DateUtil.getExcelDate(d1),
+ method != null && method.booleanValue()));
} catch (EvaluationException e) {
return e.getErrorEval();
}
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDays360.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDays360.java
index 161b7b98b0..824bc37afe 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDays360.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDays360.java
@@ -17,13 +17,20 @@
package org.apache.poi.ss.formula.functions;
+import static org.apache.poi.ss.util.Utils.assertDouble;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
@@ -74,6 +81,18 @@ final class TestDays360 {
confirm(30, makeDate(2011, 1, 1), makeDate(2011, 2, 1), false);
}
+ @Test
+ void testDatesAsStrings() throws Exception {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ HSSFSheet sheet = wb.createSheet();
+ HSSFRow row = sheet.createRow(0);
+ HSSFCell cell = row.createCell(0);
+ assertDouble(fe, cell, "DAYS360(\"1-FEB-2021\", \"15-MAR-2021\")", 44, 0.00000000001);
+ assertDouble(fe, cell, "DAYS360(\"1-FEB-2021\", \"15-MAR-2021\", FALSE)", 44, 0.00000000001);
+ }
+ }
+
private static void confirm(int expResult, int y1, int m1, int d1, int y2, int m2, int d2) {
confirm(expResult, makeDate(y1, m1, d1), makeDate(y2, m2, d2), false);
confirm(-expResult, makeDate(y2, m2, d2), makeDate(y1, m1, d1), false);