View | Details | Raw Unified | Return to bug 58468
Collapse All | Expand All

(-)src/java/org/apache/poi/ss/formula/functions/Days.java (+81 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.ss.formula.functions;
18
19
import java.util.Calendar;
20
21
import org.apache.poi.ss.formula.eval.EvaluationException;
22
import org.apache.poi.ss.formula.eval.NumberEval;
23
import org.apache.poi.ss.formula.eval.OperandResolver;
24
import org.apache.poi.ss.formula.eval.ValueEval;
25
import org.apache.poi.ss.usermodel.DateUtil;
26
import org.apache.poi.util.LocaleUtil;
27
28
/**
29
 * <p>Calculates the number of days between two dates based on a real year
30
 * , which is used in some accounting calculations. Use
31
 * this function to help compute payments if your accounting system is based on
32
 * twelve 30-day months.</p>
33
 *
34
 * <p> 
35
 * {@code DAYS(end_date,start_date)}
36
 * 
37
 * <ul>
38
 * <li>Start_date, end_date (required):<br/>
39
 * The two dates between which you want to know the number of days.<br/>
40
 * If start_date occurs after end_date, the DAYS function returns a negative number.</li>
41
 * 
42
 * </ul>
43
 * </p>
44
 * 
45
 * @see <a href="https://support.office.com/en-us/article/DAYS-function-57740535-D549-4395-8728-0F07BFF0B9DF">DAYS function - Microsoft Office</a>
46
 */
47
public class Days extends Fixed2ArgFunction {
48
49
    private final static long MILLIS_PER_DAY = 1000*60*60*24;
50
51
    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
52
        double result;
53
        try {
54
            double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
55
            double d1 = NumericFunction.singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
56
            result = evaluate(d0, d1);
57
        } catch (EvaluationException e) {
58
            return e.getErrorEval();
59
        }
60
        return new NumberEval(result);
61
    }
62
63
    private static double evaluate(double d0, double d1) {
64
        Calendar endDate = getDate(d0);
65
        Calendar startDate = getDate(d1);
66
67
        return daysBetween(startDate.getTimeInMillis(), endDate.getTimeInMillis());
68
    }
69
70
    private static Calendar getDate(double date) {
71
        Calendar processedDate = LocaleUtil.getLocaleCalendar();
72
        processedDate.setTime(DateUtil.getJavaDate(date, false));
73
        return processedDate;
74
    }
75
76
    /** Replace me with org.joda.time.Days.daysBetween */
77
    private static double daysBetween(long start, long end) {
78
        return (end - start) / MILLIS_PER_DAY;
79
    }
80
}
81
native

Return to bug 58468