@Test(dataProvider = "nextLeapDay") public void test_nextOrSameLeapDay_LocalDate(int year, int month, int day, int expectedYear) { LocalDate date = LocalDate.of(year, month, day); LocalDate test = DateAdjusters.nextOrSameLeapDay().adjust(date); if (month == 2 && day == 29) { assertEquals(test, date); } else { assertEquals(test.getYear(), expectedYear); assertEquals(test.getMonthValue(), 2); assertEquals(test.getDayOfMonth(), 29); } }
@Test(dataProvider = "nextLeapDay") public void test_nextOrSameLeapDay_Temporal(int year, int month, int day, int expectedYear) { LocalDate date = LocalDate.of(year, month, day); LocalDate test = (LocalDate) DateAdjusters.nextOrSameLeapDay().adjustInto(date); if (month == 2 && day == 29) { assertEquals(test, date); } else { assertEquals(test.getYear(), expectedYear); assertEquals(test.getMonthValue(), 2); assertEquals(test.getDayOfMonth(), 29); } }
@Override public double calculateYearFraction(LocalDate firstDate, LocalDate secondDate, ScheduleInfo scheduleInfo) { // tests show that there is no need to perform an initial check of period less than // or equal one year when using the OpenGamma interpretation of end-of-February rule // calculate the number of whole years back from the end // OpenGamma interpretation: reject ISDA end-of-Feb if 28th Feb, apply simple subtraction from secondDate LocalDate end = secondDate; LocalDate start = secondDate.minusYears(1); int years = 0; while (!start.isBefore(firstDate)) { years++; end = start; start = secondDate.minusYears(years + 1); } // calculate the remaining fraction, including start, excluding end long actualDays = daysBetween(firstDate, end); LocalDate nextLeap = DateAdjusters.nextOrSameLeapDay(firstDate); return years + (actualDays / (nextLeap.isBefore(end) ? 366d : 365d)); }