mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
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
This commit is contained in:
parent
00f6289b2f
commit
d836ff9a39
@ -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);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user