Skip to content

Commit

Permalink
fix: fix date range check
Browse files Browse the repository at this point in the history
  • Loading branch information
hbruch committed Jul 31, 2024
1 parent 3b98937 commit 057ce78
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/main/java/io/leonard/OpeningHoursEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ public void testRepeatingTimePeriod() throws OpeningHoursParseException {
@ParameterizedTest
@ValueSource(
strings = {
"mo-su 06:00-23:00", "fri", "none @ sat", "2024 Jul 26", "2024 Jul 25 - 2024 Jul 27",
"mo-su 06:00-23:00",
"fri",
"none @ sat",
"2024 Jul 26",
"2024 Jul 25 - 2024 Jul 27",
"2024 Jul 25 - 2024 Jul 26",
"2024 Jul 26 - 2024 Jul 26",
"2024 Jul 25 - 2025 May 27",
"2024 Jul 25 - 2024 Jul 27",
}
) // six numbers
// Note: hours only restrictions (e.g. "06:00-23:00") are not yet supported
Expand All @@ -50,4 +58,19 @@ public void testOHCalendarRestrictionOpen(String condition) throws OpeningHoursP
long time = dateTime.getEpochSecond();
assertTrue(tr.active(time));
}

@ParameterizedTest
@ValueSource(strings = { "2024 Jul 1 - 2099 Dec 31" }) // six numbers
// Note: hours only restrictions (e.g. "06:00-23:00") are not yet supported
public void testOHCalendarRestrictionActive(String condition) throws OpeningHoursParseException {
TimeRestriction tr = OHRulesRestriction.parseFromCondition(
condition,
() -> {
return zoneId;
}
);
Instant dateTime = Instant.parse("2025-04-01T10:30:00Z");
long time = dateTime.getEpochSecond();
assertTrue(tr.active(time));
}
}

0 comments on commit 057ce78

Please sign in to comment.