You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/09/08 22:28:05 UTC
svn commit: r693250 - in /poi/trunk/src:
java/org/apache/poi/hssf/record/formula/eval/
java/org/apache/poi/hssf/record/formula/functions/
testcases/org/apache/poi/hssf/data/
Author: josh
Date: Mon Sep 8 13:28:05 2008
New Revision: 693250
URL: http://svn.apache.org/viewvc?rev=693250&view=rev
Log:
Fixes for DAY/MONTH/YEAR functions (junit cases added)
Added:
poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java
Removed:
poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Day.java
poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Month.java
poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Year.java
Modified:
poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java
poi/trunk/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java?rev=693250&r1=693249&r2=693250&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java Mon Sep 8 13:28:05 2008
@@ -143,9 +143,9 @@
retval[64] = new Match(); // MATCH
retval[65] = new Date(); // DATE
retval[66] = new Time(); // TIME
- retval[67] = new Day(); // DAY
- retval[68] = new Month(); // MONTH
- retval[69] = new Year(); // YEAR
+ retval[67] = CalendarFieldFunction.DAY; // DAY
+ retval[68] = CalendarFieldFunction.MONTH; // MONTH
+ retval[69] = CalendarFieldFunction.YEAR; // YEAR
retval[70] = new Weekday(); // WEEKDAY
retval[71] = new Hour(); // HOUR
retval[72] = new Minute(); // MINUTE
Added: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java?rev=693250&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java (added)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java Mon Sep 8 13:28:05 2008
@@ -0,0 +1,98 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.record.formula.functions;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+import org.apache.poi.hssf.record.formula.eval.BlankEval;
+import org.apache.poi.hssf.record.formula.eval.ErrorEval;
+import org.apache.poi.hssf.record.formula.eval.Eval;
+import org.apache.poi.hssf.record.formula.eval.EvaluationException;
+import org.apache.poi.hssf.record.formula.eval.NumberEval;
+import org.apache.poi.hssf.record.formula.eval.OperandResolver;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.usermodel.HSSFDateUtil;
+
+/**
+ * Implementation of Excel functions DAY, MONTH and YEAR
+ *
+ *
+ * @author Guenter Kickinger g.kickinger@gmx.net
+ */
+public final class CalendarFieldFunction implements Function {
+
+ public static final Function YEAR = new CalendarFieldFunction(Calendar.YEAR, false);
+ public static final Function MONTH = new CalendarFieldFunction(Calendar.MONTH, true);
+ public static final Function DAY = new CalendarFieldFunction(Calendar.DAY_OF_MONTH, false);
+
+ private final int _dateFieldId;
+ private final boolean _needsOneBaseAdjustment;
+
+ private CalendarFieldFunction(int dateFieldId, boolean needsOneBaseAdjustment) {
+ _dateFieldId = dateFieldId;
+ _needsOneBaseAdjustment = needsOneBaseAdjustment;
+ }
+
+ public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) {
+ if (operands.length != 1) {
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ int val;
+ try {
+ ValueEval ve = OperandResolver.getSingleValue(operands[0], srcCellRow, srcCellCol);
+
+ if (ve == BlankEval.INSTANCE) {
+ val = 0;
+ } else {
+ val = OperandResolver.coerceValueToInt(ve);
+ }
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ if (val < 0) {
+ return ErrorEval.NUM_ERROR;
+ }
+ return new NumberEval(getCalField(val));
+ }
+
+ private int getCalField(int serialDay) {
+ if (serialDay == 0) {
+ // Special weird case
+ // day zero should be 31-Dec-1899, but Excel seems to think it is 0-Jan-1900
+ switch (_dateFieldId) {
+ case Calendar.YEAR: return 1900;
+ case Calendar.MONTH: return 1;
+ case Calendar.DAY_OF_MONTH: return 0;
+ }
+ throw new IllegalStateException("bad date field " + _dateFieldId);
+ }
+ Date d = HSSFDateUtil.getJavaDate(serialDay, false); // TODO fix 1900/1904 problem
+
+ Calendar c = new GregorianCalendar();
+ c.setTime(d);
+
+ int result = c.get(_dateFieldId);
+ if (_needsOneBaseAdjustment) {
+ result++;
+ }
+ return result;
+ }
+}
\ No newline at end of file
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls?rev=693250&r1=693249&r2=693250&view=diff
==============================================================================
Binary files - no diff available.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org