Bug 60452: WorkdayCalculator calculateWorkdays does return wrong value for same day input
deprecate unused method isNonWorkday git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b573615cd0
commit
8def7307d9
|
@ -22,6 +22,7 @@ import java.util.Date;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.DateUtil;
|
import org.apache.poi.ss.usermodel.DateUtil;
|
||||||
import org.apache.poi.util.LocaleUtil;
|
import org.apache.poi.util.LocaleUtil;
|
||||||
|
import org.apache.poi.util.Removal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A calculator for workdays, considering dates as excel representations.
|
* A calculator for workdays, considering dates as excel representations.
|
||||||
|
@ -96,7 +97,7 @@ public class WorkdayCalculator {
|
||||||
pastDaysOfWeek++;
|
pastDaysOfWeek++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return start < end ? pastDaysOfWeek : -pastDaysOfWeek;
|
return start <= end ? pastDaysOfWeek : -pastDaysOfWeek;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,14 +112,14 @@ public class WorkdayCalculator {
|
||||||
int nonWeekendHolidays = 0;
|
int nonWeekendHolidays = 0;
|
||||||
double startDay = start < end ? start : end;
|
double startDay = start < end ? start : end;
|
||||||
double endDay = end > start ? end : start;
|
double endDay = end > start ? end : start;
|
||||||
for (int i = 0; i < holidays.length; i++) {
|
for (double holiday : holidays) {
|
||||||
if (isInARange(startDay, endDay, holidays[i])) {
|
if (isInARange(startDay, endDay, holiday)) {
|
||||||
if (!isWeekend(holidays[i])) {
|
if (!isWeekend(holiday)) {
|
||||||
nonWeekendHolidays++;
|
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.
|
* @return <code>true</code> if date is a holiday, <code>false</code> otherwise.
|
||||||
*/
|
*/
|
||||||
protected boolean isHoliday(double aDate, double[] holidays) {
|
protected boolean isHoliday(double aDate, double[] holidays) {
|
||||||
for (int i = 0; i < holidays.length; i++) {
|
for (double holiday : holidays) {
|
||||||
if (Math.round(holidays[i]) == Math.round(aDate)) {
|
if (Math.round(holiday) == Math.round(aDate)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,7 +150,10 @@ public class WorkdayCalculator {
|
||||||
* @param aDate a given date.
|
* @param aDate a given date.
|
||||||
* @param holidays an array of holidays.
|
* @param holidays an array of holidays.
|
||||||
* @return <code>1</code> is not a workday, <code>0</code> otherwise.
|
* @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) {
|
protected int isNonWorkday(double aDate, double[] holidays) {
|
||||||
return isWeekend(aDate) || isHoliday(aDate, holidays) ? 1 : 0;
|
return isWeekend(aDate) || isHoliday(aDate, holidays) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,24 @@ public class TestWorkdayCalculator {
|
||||||
assertEquals(4, WorkdayCalculator.instance.calculateWorkdays(A_FRIDAY, A_WEDNESDAY, new double[]{ A_SATURDAY, A_SUNDAY }));
|
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
|
@Test
|
||||||
public void testCalculateWorkdaysNumberOfDays() {
|
public void testCalculateWorkdaysNumberOfDays() {
|
||||||
double start = 41553.0;
|
double start = 41553.0;
|
||||||
|
@ -113,4 +131,37 @@ public class TestWorkdayCalculator {
|
||||||
Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day, 0, 0, 0);
|
Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day, 0, 0, 0);
|
||||||
return cal.getTime();
|
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}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue