Skip to content

Commit e3eeb3d

Browse files
committed
Tests - fixed hilariously code DateUtils test
1 parent 6d604af commit e3eeb3d

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

Ghidra/Framework/Generic/src/main/java/ghidra/util/DateUtils.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.format.DateTimeParseException;
2323
import java.time.temporal.TemporalAccessor;
2424
import java.util.*;
25+
import java.util.function.Predicate;
2526

2627
import ghidra.util.exception.AssertException;
2728

@@ -265,21 +266,69 @@ private static Date toDate(LocalDate ld) {
265266
//@formatter:on
266267
}
267268

269+
/**
270+
* Returns a date for the given numeric values
271+
*
272+
* @param year the year
273+
* @param month the month; 0-based
274+
* @param day the day of month; 1-based
275+
* @return the date
276+
*/
268277
public static Date getDate(int year, int month, int day) {
269278
Calendar cal = new GregorianCalendar(year, month, day);
270279
return cal.getTime();
271280
}
272281

282+
/**
283+
* Returns all days between the two dates. Returns 0 if the same date is passed for both
284+
* parameters. The order of the dates does not matter.
285+
*
286+
* @param date1 the first date
287+
* @param date2 the second date
288+
* @return the number of days
289+
*/
273290
public static int getDaysBetween(Date date1, Date date2) {
274-
date1 = normalizeDate(date1);
275-
date2 = normalizeDate(date2);
291+
return doGetDaysBetween(date1, date2, DateUtils::anyDay);
292+
}
293+
294+
/**
295+
* Returns the <b>business days</b> between the two dates. Returns 0 if the same date is
296+
* passed for both parameters. The order of the dates does not matter.
297+
*
298+
* @param date1 the first date
299+
* @param date2 the second date
300+
* @return the number of days
301+
*/
302+
public static int getBusinessDaysBetween(Date date1, Date date2) {
303+
return doGetDaysBetween(date1, date2, DateUtils::isBusinessDay);
304+
}
305+
306+
private static boolean anyDay(Calendar c) {
307+
return true;
308+
}
309+
310+
private static boolean isBusinessDay(Calendar c) {
311+
return !(isWeekend(c) || isHoliday(c));
312+
}
313+
314+
private static int doGetDaysBetween(Date date1, Date date2, Predicate<Calendar> dayFilter) {
315+
316+
Date d1 = date1;
317+
Date d2 = date2;
318+
if (date1.compareTo(date2) > 0) {
319+
d1 = date2;
320+
d2 = date1;
321+
}
322+
323+
d1 = normalizeDate(d1);
324+
d2 = normalizeDate(d2);
276325

277326
Calendar cal = new GregorianCalendar();
278-
cal.setTime(date1);
327+
cal.setTime(d1);
279328
int days = 0;
280-
while (cal.getTime().compareTo(date2) < 0) {
329+
while (cal.getTime().compareTo(d2) < 0) {
281330
cal.add(Calendar.DAY_OF_MONTH, 1);
282-
if (!isWeekend(cal) && !isHoliday(cal)) {
331+
if (dayFilter.test(cal)) {
283332
days++;
284333
}
285334
}

Ghidra/Framework/Generic/src/test/java/ghidra/util/DateUtilsTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,37 @@ public void testGetDaysBetween() {
8080
int daysBetween = DateUtils.getDaysBetween(nowDate, futureDate);
8181
assertEquals(days, daysBetween);
8282
}
83+
84+
@Test
85+
public void testGetDaysBetween_SameDay() {
86+
87+
long now = System.currentTimeMillis();
88+
Date nowDate = new Date(now);
89+
int daysBetween = DateUtils.getDaysBetween(nowDate, nowDate);
90+
assertEquals(0, daysBetween);
91+
}
92+
93+
@Test
94+
public void testGetDaysBetween_MostRecentDateFirst() {
95+
96+
long now = System.currentTimeMillis();
97+
int days = 3;
98+
long threeDaysOffset = days * (24 * 60 * 60 * 1000);
99+
long future = now + threeDaysOffset;
100+
101+
Date nowDate = new Date(now);
102+
Date futureDate = new Date(future);
103+
int daysBetween = DateUtils.getDaysBetween(futureDate, nowDate);
104+
assertEquals(days, daysBetween);
105+
}
106+
107+
@Test
108+
public void testGetBusinessDaysBetween() {
109+
110+
int november = 10;
111+
Date friday = DateUtils.getDate(2019, november, 22);
112+
Date monday = DateUtils.getDate(2019, november, 25);
113+
int daysBetween = DateUtils.getBusinessDaysBetween(friday, monday);
114+
assertEquals(1, daysBetween);
115+
}
83116
}

0 commit comments

Comments
 (0)