Appendium / objectlabkit

Date Calculators for Business /FX, FX Rate Calculator and Utils
http://objectlabkit.sf.net
Apache License 2.0
67 stars 25 forks source link

PeriodCountCalculator's yearDiff inconsistent with periods ending in February #34

Open azafty468 opened 2 years ago

azafty468 commented 2 years ago

I've found an interesting issue. When aggregating the fractional year of two consecutive time periods, the results are inconsistent when the joining day is the last day of February. Here is a JUnit test that replicates the issue. This was run using the current version on maven (1.4.4) of datecalc-common and datecalc-jdk8.

` @Test public void testYearFracAroundFebruary() { PeriodCountCalculator calc = LocalDateKitCalculatorsFactory.getDefaultInstance().getPeriodCountCalculator();

    LocalDate startDate = LocalDate.of(2019, 2, 15);
    LocalDate endDate = LocalDate.of(2019, 3, 15);
    BigDecimal yearFractionA;
    BigDecimal yearFractionB;
    LocalDate middleDate = startDate.plusDays(1);

    while (middleDate.isBefore(endDate)) {
        yearFractionA = new BigDecimal(calc.yearDiff(startDate, middleDate, PeriodCountBasis.CONV_360E_ISDA)).setScale(20, RoundingMode.HALF_UP);
        yearFractionB = new BigDecimal(calc.yearDiff(middleDate, endDate, PeriodCountBasis.CONV_360E_ISDA)).setScale(20, RoundingMode.HALF_UP);
        System.out.println("Year fraction for " + middleDate.toString() + ": " + yearFractionA.add(yearFractionB));
        middleDate = middleDate.plusDays(1);
    }
}

`

The year fraction should be ~0.0833333. For 2019-02-28, the calculated value is off. Also, thank you guys for this library!

Year fraction for 2019-02-16: 0.08333333333333333868 Year fraction for 2019-02-17: 0.08333333333333333478 Year fraction for 2019-02-18: 0.08333333333333333044 Year fraction for 2019-02-19: 0.08333333333333332698 Year fraction for 2019-02-20: 0.08333333333333333565 Year fraction for 2019-02-21: 0.08333333333333333218 Year fraction for 2019-02-22: 0.08333333333333332870 Year fraction for 2019-02-23: 0.08333333333333333218 Year fraction for 2019-02-24: 0.08333333333333333565 Year fraction for 2019-02-25: 0.08333333333333332871 Year fraction for 2019-02-26: 0.08333333333333333217 Year fraction for 2019-02-27: 0.08333333333333333565 Year fraction for 2019-02-28: 0.07777777777777777207 Year fraction for 2019-03-01: 0.08333333333333333565 Year fraction for 2019-03-02: 0.08333333333333332871 Year fraction for 2019-03-03: 0.08333333333333333565 Year fraction for 2019-03-04: 0.08333333333333333217 Year fraction for 2019-03-05: 0.08333333333333332871 Year fraction for 2019-03-06: 0.08333333333333333565 Year fraction for 2019-03-07: 0.08333333333333333218 Year fraction for 2019-03-08: 0.08333333333333332870 Year fraction for 2019-03-09: 0.08333333333333333218 Year fraction for 2019-03-10: 0.08333333333333333565 Year fraction for 2019-03-11: 0.08333333333333332698 Year fraction for 2019-03-12: 0.08333333333333333044 Year fraction for 2019-03-13: 0.08333333333333333478 Year fraction for 2019-03-14: 0.08333333333333333868

azafty468 commented 2 years ago

I should also follow this up with the fact that I'm not in the finance industry, I'm just a software engineer. This appears to be a logical bug to me, but it could be functioning as designed by the accounting standard.