--- src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java (revision 74ab8da0fda0cb2814dbc29a535a5b035469ba36) +++ src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java (revision ) @@ -38,7 +38,7 @@ /** * Calculate how many workdays are there between a start and an end date, as excel representations, considering a range of holidays. - * + * * @param start start date. * @param end end date. * @param holidays an array of holidays. @@ -53,7 +53,7 @@ /** * Calculate the workday past x workdays from a starting date, considering a range of holidays. - * + * * @param start start date. * @param workdays number of workdays to be past from starting date. * @param holidays an array of holidays. @@ -76,10 +76,10 @@ } return endDate.getTime(); } - + /** * Calculates how many days of week past between a start and an end date. - * + * * @param start start date. * @param end end date. * @param dayOfWeek a day of week as represented by {@link Calendar} constants. @@ -96,12 +96,12 @@ pastDaysOfWeek++; } } - return start < end ? pastDaysOfWeek : -pastDaysOfWeek; + return start <= end ? pastDaysOfWeek : -pastDaysOfWeek; } /** * Calculates how many holidays in a list are workdays, considering an interval of dates. - * + * * @param start start date. * @param end end date. * @param holidays an array of holidays. @@ -118,7 +118,7 @@ } } } - return start < end ? nonWeekendHolidays : -nonWeekendHolidays; + return start <= end ? nonWeekendHolidays : -nonWeekendHolidays; } /** --- src/testcases/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java (revision 74ab8da0fda0cb2814dbc29a535a5b035469ba36) +++ src/testcases/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java (revision ) @@ -67,6 +67,24 @@ assertEquals(4, WorkdayCalculator.instance.calculateWorkdays(A_FRIDAY, A_WEDNESDAY, new double[]{ A_SATURDAY, A_SUNDAY })); } + @Test + public void testCalculateWorkdaysOnSameDayShouldReturn1ForWeekdays() { + final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2)); + assertEquals(1, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[0])); + } + + @Test + public void testCalculateWorkdaysOnSameDayShouldReturn0ForHolidays() { + final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2)); + assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[]{ A_MONDAY })); + } + + @Test + public void testCalculateWorkdaysOnSameDayShouldReturn0ForWeekends() { + final double A_SUNDAY = DateUtil.getExcelDate(d(2017, 1, 1)); + assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_SUNDAY, A_SUNDAY, new double[0])); + } + @Test public void testCalculateWorkdaysNumberOfDays() { double start = 41553.0; @@ -108,7 +126,7 @@ final double A_SATURDAY = DateUtil.getExcelDate(d(2011, 12, 10)); assertEquals(1, WorkdayCalculator.instance.pastDaysOfWeek(A_THURSDAY, A_SATURDAY, SATURDAY)); } - + private static Date d(int year, int month, int day) { Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day, 0, 0, 0); return cal.getTime();