Skip to content

Commit

Permalink
fix: fix date range calculation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hbruch committed Aug 2, 2024
1 parent d02f953 commit 4d72dcf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/main/java/io/leonard/OpeningHoursEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,33 @@ private static boolean dateMatchesDateRanges(LocalDateTime time, List<DateRange>
private static boolean dateMatchesDateRange(LocalDateTime time, DateRange range) {
// if the end date is null it means that it's just a single date like in "2020 Aug 11"
DateWithOffset startDate = range.getStartDate();
boolean afterStartDate = time.getYear() >= startDate.getYear() && time.getMonth().ordinal() >= startDate.getMonth().ordinal() && time.getDayOfMonth() >= startDate.getDay();
boolean afterStartDate = isSameDateOrAfter(time, startDate);

if (range.getEndDate() == null) {
return afterStartDate;
}
DateWithOffset endDate = range.getEndDate();
boolean beforeEndDate = time.getYear() <= endDate.getYear() && time.getMonth().ordinal() <= endDate.getMonth().ordinal() && time.getDayOfMonth() <= endDate.getDay();
boolean beforeEndDate = !isSameDateOrAfter(time.minusDays(1), endDate);
return afterStartDate && beforeEndDate;
}

private static boolean isSameDateOrAfter(LocalDateTime time, DateWithOffset startDate) {
return (
time.getYear() > startDate.getYear() ||
(
time.getYear() == startDate.getYear() &&
startDate.getMonth() != null &&
(
time.getMonth().ordinal() > startDate.getMonth().ordinal() ||
(
time.getMonth().ordinal() == startDate.getMonth().ordinal() &&
time.getDayOfMonth() >= startDate.getDay()
)
)
)
);
}

private static boolean timeMatchesHours(LocalDateTime time, TimeSpan timeSpan) {
var minutesAfterMidnight = minutesAfterMidnight(time.toLocalTime());
return timeSpan.getStart() <= minutesAfterMidnight && timeSpan.getEnd() >= minutesAfterMidnight;
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/closed.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"2020-08-06T14:28:04","2025 Jan 1 - 2099 Dec 31"
"2020-08-06T14:28:04","Mo-Fr 10:00-12:00"
"2020-08-06T15:28:04","Mo-Fr 10:00-15:00"
"2020-08-06T15:28:04","Mo-We 10:00-15:00"
Expand All @@ -15,3 +14,4 @@
"2020-08-06T14:28:04","2020 Aug 04 - 2020 Aug 05;2020 Aug 07"
"2020-08-06T14:28:04","2020 Nov"
"2020-08-06T14:28:04","2020 Aug 06 10:00-12:00"
"2020-08-06T14:28:04","2025 Jan 1 - 2099 Dec 31"
1 change: 1 addition & 0 deletions src/test/resources/open.csv
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
"2020-08-06T14:28:04","2020 Aug 04 - 2020 Aug 06"
"2020-08-06T14:28:04","2020 Aug"
"2020-08-06T14:28:04","2020 Aug 06 14:00-16:00"
"2020-08-06T14:28:04","2019 Nov 01 - 2020 Aug 06"

0 comments on commit 4d72dcf

Please sign in to comment.