PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libformula-1.1.3/source/org/pentaho/reporting/libraries/formula/function/datetime/DateDifFunction.java

#
Java | 181 lines | 116 code | 24 blank | 41 comment | 19 complexity | 76bb9e7549cc13473dabab5a9ab28093 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it under the
  3. * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
  4. * Foundation.
  5. *
  6. * You should have received a copy of the GNU Lesser General Public License along with this
  7. * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
  8. * or from the Free Software Foundation, Inc.,
  9. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU Lesser General Public License for more details.
  14. *
  15. * Copyright (c) 2006 - 2009 Pentaho Corporation and Contributors. All rights reserved.
  16. */
  17. package org.pentaho.reporting.libraries.formula.function.datetime;
  18. import java.util.Calendar;
  19. import java.util.Date;
  20. import java.util.GregorianCalendar;
  21. import java.util.Locale;
  22. import java.util.TimeZone;
  23. import java.math.BigDecimal;
  24. import org.pentaho.reporting.libraries.formula.EvaluationException;
  25. import org.pentaho.reporting.libraries.formula.FormulaContext;
  26. import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
  27. import org.pentaho.reporting.libraries.formula.LocalizationContext;
  28. import org.pentaho.reporting.libraries.formula.util.NumberUtil;
  29. import org.pentaho.reporting.libraries.formula.function.Function;
  30. import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
  31. import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
  32. import org.pentaho.reporting.libraries.formula.typing.TypeRegistry;
  33. import org.pentaho.reporting.libraries.formula.typing.coretypes.NumberType;
  34. /**
  35. * This function returns the number of years, months, or days between two date
  36. * numbers.<br/>
  37. * <p/>
  38. * The Format is a code from the following table, entered as text, that
  39. * specifies the format you want: <TABLE> <TR> <TH>format</TH> <TH>Returns the
  40. * number of</TH> </TR> <TR> <TD>y</TD> <TD>Years</TD> </TR> <TR> <TD>m</TD>
  41. * <TD>Months. If there is not a complete month between the dates, 0 will be
  42. * returned.</TD> </TR> <TR> <TD>d</TD> <TD>Days</TD> </TR> <TR> <TD>md</TD>
  43. * <TD>Days, ignoring months and years</TD> </TR> <TR> <TD>ym</TD> <TD>Months,
  44. * ignoring years</TD> </TR> <TR> <TD>yd</TD> <TD>Days, ignoring years</TD>
  45. * </TR> <TR> <TD></TD> <TD></TD> </TR> </TABLE>
  46. *
  47. * @author Cedric Pronzato
  48. */
  49. public class DateDifFunction implements Function
  50. {
  51. public static final String YEARS_CODE = "y";
  52. public static final String MONTHS_CODE = "m";
  53. public static final String DAYS_CODE = "d";
  54. public static final String DAYS_IGNORING_YEARS = "yd";
  55. public static final String MONTHS_IGNORING_YEARS = "ym";
  56. public static final String DAYS_IGNORING_MONTHS_YEARS = "md";
  57. private static final long serialVersionUID = 81013707499607068L;
  58. public DateDifFunction()
  59. {
  60. }
  61. public String getCanonicalName()
  62. {
  63. return "DATEDIF";
  64. }
  65. public TypeValuePair evaluate(final FormulaContext context,
  66. final ParameterCallback parameters)
  67. throws EvaluationException
  68. {
  69. if (parameters.getParameterCount() != 3)
  70. {
  71. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  72. }
  73. final TypeRegistry typeRegistry = context.getTypeRegistry();
  74. final String formatCode = typeRegistry.convertToText
  75. (parameters.getType(2), parameters.getValue(2));
  76. if (formatCode == null || "".equals(formatCode))
  77. {
  78. throw EvaluationException.getInstance(
  79. LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  80. }
  81. if (DateDifFunction.DAYS_CODE.equals(formatCode))
  82. {
  83. final Number date1 = typeRegistry.convertToNumber
  84. (parameters.getType(0), parameters.getValue(0));
  85. final Number date2 = typeRegistry.convertToNumber
  86. (parameters.getType(1), parameters.getValue(1));
  87. final BigDecimal dn1 = NumberUtil.performIntRounding(NumberUtil.getAsBigDecimal(date1));
  88. final BigDecimal dn2 = NumberUtil.performIntRounding(NumberUtil.getAsBigDecimal(date2));
  89. //noinspection UnpredictableBigDecimalConstructorCall
  90. return new TypeValuePair(NumberType.GENERIC_NUMBER, new BigDecimal(dn2.longValue() - dn1.longValue()));
  91. }
  92. final Date date1 = typeRegistry.convertToDate
  93. (parameters.getType(0), parameters.getValue(0));
  94. final Date date2 = typeRegistry.convertToDate
  95. (parameters.getType(1), parameters.getValue(1));
  96. if (date1 == null || date2 == null)
  97. {
  98. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  99. }
  100. final LocalizationContext localizationContext = context.getLocalizationContext();
  101. final TimeZone timeZone = localizationContext.getTimeZone();
  102. final Locale locale = localizationContext.getLocale();
  103. final GregorianCalendar calandar1 =
  104. new GregorianCalendar(timeZone, locale);
  105. calandar1.setTime(date1);
  106. final GregorianCalendar calandar2 =
  107. new GregorianCalendar(timeZone, locale);
  108. calandar2.setTime(date2);
  109. final int res;
  110. if (DateDifFunction.YEARS_CODE.equals(formatCode))
  111. {
  112. res = Math.abs(calandar2.get(Calendar.YEAR) - calandar1.get(Calendar.YEAR));
  113. }
  114. else if (DateDifFunction.MONTHS_CODE.equals(formatCode))
  115. {
  116. final int month1 = calandar1.get(Calendar.MONTH);
  117. final int month2 = calandar2.get(Calendar.MONTH);
  118. final int year1 = calandar1.get(Calendar.YEAR);
  119. final int year2 = calandar2.get(Calendar.YEAR);
  120. res = Math.abs(year2 - year1) * 12 + Math.abs(month2 - month1);
  121. }
  122. else if (DateDifFunction.DAYS_IGNORING_MONTHS_YEARS.equals(formatCode))
  123. {
  124. // The number of days between Date1 and Date2, as if Date1 and
  125. // Date2 were in the same month and the same year.
  126. // Not sure what happens to leap years, so this solution may be invalid.
  127. calandar1.set(Calendar.YEAR, calandar2.get(Calendar.YEAR));
  128. calandar1.set(Calendar.MONTH, calandar2.get(Calendar.MONTH));
  129. res = Math.abs(calandar2.get(Calendar.DAY_OF_MONTH) -
  130. calandar1.get(Calendar.DAY_OF_MONTH));
  131. }
  132. else if (DateDifFunction.MONTHS_IGNORING_YEARS.equals(formatCode))
  133. {
  134. final int month1 = calandar1.get(Calendar.MONTH);
  135. final int month2 = calandar2.get(Calendar.MONTH);
  136. res = Math.abs(month2 - month1);
  137. }
  138. else if (DateDifFunction.DAYS_IGNORING_YEARS.equals(formatCode))
  139. {
  140. //Isn't that a stupid case? How could we count the days while ignoring
  141. //how much days there are in each months without using the year?
  142. // The number of days between Date1 and Date2, as if Date1 and Date2
  143. // were in the same year.
  144. // Not sure what happens to leap years, so this solution may be invalid.
  145. calandar1.set(Calendar.YEAR, calandar2.get(Calendar.YEAR));
  146. final int dayOne = calandar1.get(Calendar.DAY_OF_YEAR);
  147. final int dayTwo = calandar2.get(Calendar.DAY_OF_YEAR);
  148. res = Math.abs(dayOne - dayTwo);
  149. }
  150. else
  151. {
  152. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  153. }
  154. //noinspection UnpredictableBigDecimalConstructorCall
  155. return new TypeValuePair(NumberType.GENERIC_NUMBER, new BigDecimal((double) res));
  156. }
  157. }