View | Details | Raw Unified | Return to bug 60452
Collapse All | Expand All

(-)src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java (-7 / +7 lines)
Lines 38-44 Link Here
38
38
39
    /**
39
    /**
40
     * Calculate how many workdays are there between a start and an end date, as excel representations, considering a range of holidays.
40
     * Calculate how many workdays are there between a start and an end date, as excel representations, considering a range of holidays.
41
     * 
41
     *
42
     * @param start start date.
42
     * @param start start date.
43
     * @param end end date.
43
     * @param end end date.
44
     * @param holidays an array of holidays.
44
     * @param holidays an array of holidays.
Lines 53-59 Link Here
53
53
54
    /**
54
    /**
55
     * Calculate the workday past x workdays from a starting date, considering a range of holidays.
55
     * Calculate the workday past x workdays from a starting date, considering a range of holidays.
56
     * 
56
     *
57
     * @param start start date.
57
     * @param start start date.
58
     * @param workdays number of workdays to be past from starting date.
58
     * @param workdays number of workdays to be past from starting date.
59
     * @param holidays an array of holidays.
59
     * @param holidays an array of holidays.
Lines 76-85 Link Here
76
		}
76
		}
77
		return endDate.getTime();
77
		return endDate.getTime();
78
	}
78
	}
79
    
79
80
    /**
80
    /**
81
     * Calculates how many days of week past between a start and an end date.
81
     * Calculates how many days of week past between a start and an end date.
82
     * 
82
     *
83
     * @param start start date.
83
     * @param start start date.
84
     * @param end end date.
84
     * @param end end date.
85
     * @param dayOfWeek a day of week as represented by {@link Calendar} constants.
85
     * @param dayOfWeek a day of week as represented by {@link Calendar} constants.
Lines 96-107 Link Here
96
                pastDaysOfWeek++;
96
                pastDaysOfWeek++;
97
            }
97
            }
98
        }
98
        }
99
        return start < end ? pastDaysOfWeek : -pastDaysOfWeek;
99
        return start <= end ? pastDaysOfWeek : -pastDaysOfWeek;
100
    }
100
    }
101
101
102
    /**
102
    /**
103
     * Calculates how many holidays in a list are workdays, considering an interval of dates.
103
     * Calculates how many holidays in a list are workdays, considering an interval of dates.
104
     * 
104
     *
105
     * @param start start date.
105
     * @param start start date.
106
     * @param end end date.
106
     * @param end end date.
107
     * @param holidays an array of holidays.
107
     * @param holidays an array of holidays.
Lines 118-124 Link Here
118
                }
118
                }
119
            }
119
            }
120
        }
120
        }
121
        return start < end ? nonWeekendHolidays : -nonWeekendHolidays;
121
        return start <= end ? nonWeekendHolidays : -nonWeekendHolidays;
122
    }
122
    }
123
123
124
    /**
124
    /**
(-)src/testcases/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java (-1 / +19 lines)
Lines 67-72 Link Here
67
        assertEquals(4, WorkdayCalculator.instance.calculateWorkdays(A_FRIDAY, A_WEDNESDAY, new double[]{ A_SATURDAY, A_SUNDAY }));
67
        assertEquals(4, WorkdayCalculator.instance.calculateWorkdays(A_FRIDAY, A_WEDNESDAY, new double[]{ A_SATURDAY, A_SUNDAY }));
68
    }
68
    }
69
69
70
	@Test
71
	public void testCalculateWorkdaysOnSameDayShouldReturn1ForWeekdays() {
72
		final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
73
		assertEquals(1, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[0]));
74
	}
75
76
	@Test
77
	public void testCalculateWorkdaysOnSameDayShouldReturn0ForHolidays() {
78
		final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
79
		assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[]{ A_MONDAY }));
80
	}
81
82
	@Test
83
	public void testCalculateWorkdaysOnSameDayShouldReturn0ForWeekends() {
84
		final double A_SUNDAY = DateUtil.getExcelDate(d(2017, 1, 1));
85
		assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_SUNDAY, A_SUNDAY, new double[0]));
86
	}
87
70
    @Test
88
    @Test
71
    public void testCalculateWorkdaysNumberOfDays() {
89
    public void testCalculateWorkdaysNumberOfDays() {
72
    	double start = 41553.0;
90
    	double start = 41553.0;
Lines 108-114 Link Here
108
        final double A_SATURDAY = DateUtil.getExcelDate(d(2011, 12, 10));
126
        final double A_SATURDAY = DateUtil.getExcelDate(d(2011, 12, 10));
109
        assertEquals(1, WorkdayCalculator.instance.pastDaysOfWeek(A_THURSDAY, A_SATURDAY, SATURDAY));
127
        assertEquals(1, WorkdayCalculator.instance.pastDaysOfWeek(A_THURSDAY, A_SATURDAY, SATURDAY));
110
    }
128
    }
111
    
129
112
    private static Date d(int year, int month, int day) {
130
    private static Date d(int year, int month, int day) {
113
        Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day, 0, 0, 0);
131
        Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day, 0, 0, 0);
114
        return cal.getTime();
132
        return cal.getTime();

Return to bug 60452