]> source.dussan.org Git - poi.git/commitdiff
Bug 60452: WorkdayCalculator calculateWorkdays does return wrong value for same day...
authorDominik Stadler <centic@apache.org>
Fri, 30 Dec 2016 15:05:14 +0000 (15:05 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 30 Dec 2016 15:05:14 +0000 (15:05 +0000)
deprecate unused method isNonWorkday

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776588 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java
src/testcases/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java

index 29b29feaa33c3b1bf0161377a180d58f660c7e16..326a7e02f743411c905dbc2f8de8d997d69fc6b1 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Date;
 
 import org.apache.poi.ss.usermodel.DateUtil;
 import org.apache.poi.util.LocaleUtil;
+import org.apache.poi.util.Removal;
 
 /**
  * A calculator for workdays, considering dates as excel representations.
@@ -38,7 +39,7 @@ public class WorkdayCalculator {
 
     /**
      * 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 +54,7 @@ public class WorkdayCalculator {
 
     /**
      * 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 +77,10 @@ public class WorkdayCalculator {
                }
                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 +97,12 @@ public class WorkdayCalculator {
                 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.
@@ -111,14 +112,14 @@ public class WorkdayCalculator {
         int nonWeekendHolidays = 0;
         double startDay = start < end ? start : end;
         double endDay = end > start ? end : start;
-        for (int i = 0; i < holidays.length; i++) {
-            if (isInARange(startDay, endDay, holidays[i])) {
-                if (!isWeekend(holidays[i])) {
+        for (double holiday : holidays) {
+            if (isInARange(startDay, endDay, holiday)) {
+                if (!isWeekend(holiday)) {
                     nonWeekendHolidays++;
                 }
             }
         }
-        return start < end ? nonWeekendHolidays : -nonWeekendHolidays;
+        return start <= end ? nonWeekendHolidays : -nonWeekendHolidays;
     }
 
     /**
@@ -137,8 +138,8 @@ public class WorkdayCalculator {
      * @return <code>true</code> if date is a holiday, <code>false</code> otherwise.
      */
     protected boolean isHoliday(double aDate, double[] holidays) {
-        for (int i = 0; i < holidays.length; i++) {
-            if (Math.round(holidays[i]) == Math.round(aDate)) {
+        for (double holiday : holidays) {
+            if (Math.round(holiday) == Math.round(aDate)) {
                 return true;
             }
         }
@@ -149,7 +150,10 @@ public class WorkdayCalculator {
      * @param aDate a given date.
      * @param holidays an array of holidays.
      * @return <code>1</code> is not a workday, <code>0</code> otherwise.
+     *
+     * @deprecated POI 3.16 - will be removed, not used in POI itself
      */
+    @Removal(version="3.18")
     protected int isNonWorkday(double aDate, double[] holidays) {
         return isWeekend(aDate) || isHoliday(aDate, holidays) ? 1 : 0;
     }
index 055149c6c6a207f356b441c134e7c14c4e3faf61..80adae743e831bf86137d829c48bb968efc5c992 100644 (file)
@@ -67,6 +67,24 @@ public class TestWorkdayCalculator {
         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,9 +126,42 @@ public class TestWorkdayCalculator {
         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();
     }
+
+    @Test
+    public void testCalculateNonWeekendHolidays() {
+        final double start = DateUtil.getExcelDate(d(2016, 12, 24));
+        final double end = DateUtil.getExcelDate(d(2016, 12, 31));
+        final double holiday1 = DateUtil.getExcelDate(d(2016, 12, 25));
+        final double holiday2 = DateUtil.getExcelDate(d(2016, 12, 26));
+        int count = WorkdayCalculator.instance.calculateNonWeekendHolidays(start, end, new double[]{holiday1, holiday2});
+        assertEquals("Expected 1 non-weekend-holiday for " + start + " to " + end + " and " + holiday1 + " and " + holiday2,
+                1, count);
+    }
+
+    @Test
+    public void testCalculateNonWeekendHolidaysOneDay() {
+        final double start = DateUtil.getExcelDate(d(2016, 12, 26));
+        final double end = DateUtil.getExcelDate(d(2016, 12, 26));
+        final double holiday1 = DateUtil.getExcelDate(d(2016, 12, 25));
+        final double holiday2 = DateUtil.getExcelDate(d(2016, 12, 26));
+        int count = WorkdayCalculator.instance.calculateNonWeekendHolidays(start, end, new double[]{holiday1, holiday2});
+        assertEquals("Expected 1 non-weekend-holiday for " + start + " to " + end + " and " + holiday1 + " and " + holiday2,
+                1, count);
+    }
+
+    @SuppressWarnings("deprecation")
+    @Test
+    public void testIsNonWorkday() throws Exception {
+        final double weekend = DateUtil.getExcelDate(d(2016, 12, 25));
+        final double holiday = DateUtil.getExcelDate(d(2016, 12, 26));
+        final double workday = DateUtil.getExcelDate(d(2016, 12, 27));
+        assertEquals(1, WorkdayCalculator.instance.isNonWorkday(weekend, new double[]{holiday}));
+        assertEquals(1, WorkdayCalculator.instance.isNonWorkday(holiday, new double[]{holiday}));
+        assertEquals(0, WorkdayCalculator.instance.isNonWorkday(workday, new double[]{holiday}));
+    }
 }