001 /**
002 * =========================================
003 * LibFormula : a free Java formula library
004 * =========================================
005 *
006 * Project Info: http://reporting.pentaho.org/libformula/
007 *
008 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
009 *
010 * This library is free software; you can redistribute it and/or modify it under the terms
011 * of the GNU Lesser General Public License as published by the Free Software Foundation;
012 * either version 2.1 of the License, or (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public License along with this
019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA 02111-1307, USA.
021 *
022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023 * in the United States and other countries.]
024 *
025 *
026 * ------------
027 * $Id: DateDifFunction.java 2716 2007-04-01 13:51:58Z taqua $
028 * ------------
029 * (C) Copyright 2006-2007, by Pentaho Corporation.
030 */
031 package org.jfree.formula.function.datetime;
032
033 import java.util.Calendar;
034 import java.util.Date;
035 import java.util.GregorianCalendar;
036 import java.util.Locale;
037 import java.util.TimeZone;
038
039 import org.jfree.formula.EvaluationException;
040 import org.jfree.formula.FormulaContext;
041 import org.jfree.formula.LibFormulaErrorValue;
042 import org.jfree.formula.LocalizationContext;
043 import org.jfree.formula.function.Function;
044 import org.jfree.formula.function.ParameterCallback;
045 import org.jfree.formula.lvalues.TypeValuePair;
046 import org.jfree.formula.typing.TypeRegistry;
047 import org.jfree.formula.typing.coretypes.NumberType;
048
049 /**
050 * This function returns the number of years, months, or days between two date
051 * numbers.<br/>
052 * <p/>
053 * The Format is a code from the following table, entered as text, that
054 * specifies the format you want: <TABLE> <TR> <TH>format</TH> <TH>Returns the
055 * number of</TH> </TR> <TR> <TD>y</TD> <TD>Years</TD> </TR> <TR> <TD>m</TD>
056 * <TD>Months. If there is not a complete month between the dates, 0 will be
057 * returned.</TD> </TR> <TR> <TD>d</TD> <TD>Days</TD> </TR> <TR> <TD>md</TD>
058 * <TD>Days, ignoring months and years</TD> </TR> <TR> <TD>ym</TD> <TD>Months,
059 * ignoring years</TD> </TR> <TR> <TD>yd</TD> <TD>Days, ignoring years</TD>
060 * </TR> <TR> <TD></TD> <TD></TD> </TR> </TABLE>
061 *
062 * @author Cedric Pronzato
063 */
064 public class DateDifFunction implements Function
065 {
066 public static final String YEARS_CODE = "y";
067 public static final String MONTHS_CODE = "m";
068 public static final String DAYS_CODE = "d";
069 public static final String DAYS_IGNORING_YEARS = "yd";
070 public static final String MONTHS_IGNORING_YEARS = "ym";
071 public static final String DAYS_IGNORING_MONTHS_YEARS = "md";
072
073 // private static final String[] CODES = {
074 // YEARS_CODE, MONTHS_CODE, DAYS_CODE, DAYS_IGNORING_YEARS,
075 // MONTHS_IGNORING_YEARS, DAYS_IGNORING_MONTHS_YEARS
076 // };
077 //
078
079 public DateDifFunction()
080 {
081 }
082
083 public String getCanonicalName()
084 {
085 return "DATEDIF";
086 }
087
088 public TypeValuePair evaluate(final FormulaContext context,
089 final ParameterCallback parameters)
090 throws EvaluationException
091 {
092 if (parameters.getParameterCount() != 3)
093 {
094 throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
095 }
096
097 final TypeRegistry typeRegistry = context.getTypeRegistry();
098 final Date date1 = typeRegistry.convertToDate
099 (parameters.getType(0), parameters.getValue(0));
100 final Date date2 = typeRegistry.convertToDate
101 (parameters.getType(1), parameters.getValue(1));
102 final String formatCode = typeRegistry.convertToText
103 (parameters.getType(2), parameters.getValue(2));
104
105 if (date1 == null || date2 == null || formatCode == null || "".equals(
106 formatCode))
107 {
108 throw new EvaluationException(
109 LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
110 }
111
112 final LocalizationContext localizationContext = context.getLocalizationContext();
113 final TimeZone timeZone = localizationContext.getTimeZone();
114 final Locale locale = localizationContext.getLocale();
115 final GregorianCalendar calandar1 =
116 new GregorianCalendar(timeZone, locale);
117 calandar1.setTime(date1);
118
119 final GregorianCalendar calandar2 =
120 new GregorianCalendar(timeZone, locale);
121 calandar2.setTime(date2);
122
123 int res;
124
125 if (DateDifFunction.YEARS_CODE.equals(formatCode))
126 {
127 res = calandar2.get(Calendar.YEAR) - calandar1.get(Calendar.YEAR);
128 }
129 else if (DateDifFunction.MONTHS_CODE.equals(formatCode))
130 {
131 final int month1 = calandar1.get(Calendar.MONTH);
132 final int month2 = calandar2.get(Calendar.MONTH);
133 final int year1 = calandar1.get(Calendar.YEAR);
134 final int year2 = calandar2.get(Calendar.YEAR);
135
136 res = (year2 - year1) * 12 +
137 month2 - month1;
138 }
139 else if (DateDifFunction.DAYS_IGNORING_MONTHS_YEARS.equals(formatCode))
140 {
141 // The number of days between Date1 and Date2, as if Date1 and
142 // Date2 were in the same month and the same year.
143
144 // Not sure what happens to leap years, so this solution may be invalid.
145 calandar1.set(Calendar.YEAR, calandar2.get(Calendar.YEAR));
146 calandar1.set(Calendar.MONTH, calandar2.get(Calendar.MONTH));
147
148 res = calandar2.get(Calendar.DAY_OF_MONTH) -
149 calandar1.get(Calendar.DAY_OF_MONTH);
150 }
151 else if (DateDifFunction.DAYS_CODE.equals(formatCode))
152 {
153 final int dayOfYear1 = calandar1.get(Calendar.DAY_OF_YEAR);
154 final int dayOfYear2 = calandar2.get(Calendar.DAY_OF_YEAR);
155 final int year1 = calandar1.get(Calendar.YEAR);
156 final int year2 = calandar2.get(Calendar.YEAR);
157
158 final GregorianCalendar workingCalandar =
159 new GregorianCalendar(timeZone, locale);
160
161 res = dayOfYear2 - dayOfYear1;
162
163 // run through the inner years, without counting the border years
164 // Always run from the lower to the higher, so that we prevent infinite
165 // loops ..
166 final int targetYear = Math.max(year1, year2);
167 for (int i = Math.min(year1, year2); i < targetYear; i++)
168 {
169 workingCalandar.set(Calendar.YEAR, i);
170 res += workingCalandar.getActualMaximum(Calendar.DAY_OF_YEAR);
171 }
172 }
173 else if (DateDifFunction.MONTHS_IGNORING_YEARS.equals(formatCode))
174 {
175 final int month1 = calandar1.get(Calendar.MONTH);
176 final int month2 = calandar2.get(Calendar.MONTH);
177
178 res = month2 - month1;
179 }
180 else if (DateDifFunction.DAYS_IGNORING_YEARS.equals(formatCode))
181 {
182 //Isn't that a stupid case? How could we count the days while ignoring
183 //how much days there are in each months without using the year?
184
185 // The number of days between Date1 and Date2, as if Date1 and Date2
186 // were in the same year.
187
188 // Not sure what happens to leap years, so this solution may be invalid.
189 calandar1.set(Calendar.YEAR, calandar2.get(Calendar.YEAR));
190 final int dayOne = calandar1.get(Calendar.DAY_OF_YEAR);
191 final int dayTwo = calandar2.get(Calendar.DAY_OF_YEAR);
192 res = Math.abs(dayOne - dayTwo);
193 }
194 else
195 {
196 throw new EvaluationException(
197 LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
198 }
199
200 if (res < 0)
201 {
202 throw new EvaluationException(
203 LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
204 }
205
206 return new TypeValuePair(NumberType.GENERIC_NUMBER, new Integer(res));
207 }
208 }