Skip to content

Commit

Permalink
Fix parsing of hour:minute intervals
Browse files Browse the repository at this point in the history
THis fixes the parsing of intervals that are expressed in hout:minutes
format and a, hidden, regression in the same code wrt minute value
calculation.
  • Loading branch information
simonpoole committed Jun 19, 2021
1 parent 1bd977f commit 78203e4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: "jacoco"

version = '0.23.3'
version = '0.23.4'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/ch/poole/openinghoursparser/OpeningHoursParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -533,19 +533,20 @@ int interval() :
{
< SLASH >
(
LOOKAHEAD(hours() minutes())
(
(
number = number()
)
hours = hours() number = minutes()
{
interval = Integer.parseInt(number.image);
interval = Integer.parseInt(hours.image) * 60 + Integer.parseInt(number.image);
}
)
|
|
(
hours = hours() number = minutes()
(
number = number()
)
{
interval = Integer.parseInt(hours.image) * 60 + Integer.parseInt(number.image.substring(1));
interval = Integer.parseInt(number.image);
}
)
)
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/ch/poole/openinghoursparser/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,49 @@ public void dateRangeWithOccurance() {
fail(pex.getMessage());
}
}

@Test
public void intervalMinutesOnly() {
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("07:00-20:00/99".getBytes()));
try {
List<Rule> rules = parser.rules(true);
assertEquals(1, rules.size());
Rule r = rules.get(0);
assertEquals(1,r.times.size());
TimeSpan ts = r.times.get(0);
assertEquals(99, ts.getInterval());
} catch (ParseException pex) {
fail(pex.getMessage());
}
}

@Test
public void intervalHourMinutes() {
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("07:00-20:00/01:39".getBytes()));
try {
List<Rule> rules = parser.rules(true);
assertEquals(1, rules.size());
Rule r = rules.get(0);
assertEquals(1,r.times.size());
TimeSpan ts = r.times.get(0);
assertEquals(99, ts.getInterval());
} catch (ParseException pex) {
fail(pex.getMessage());
}
}

@Test
public void intervalHourMinutesNoLeading0() {
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("07:00-20:00/1:39".getBytes()));
try {
List<Rule> rules = parser.rules(true);
assertEquals(1, rules.size());
Rule r = rules.get(0);
assertEquals(1,r.times.size());
TimeSpan ts = r.times.get(0);
assertEquals(99, ts.getInterval());
} catch (ParseException pex) {
fail(pex.getMessage());
}
}
}
4 changes: 2 additions & 2 deletions test-data/oh.txt-result
Original file line number Diff line number Diff line change
Expand Up @@ -7211,7 +7211,7 @@
0 13:00-23:00
0 13:00-23:00
0 13:00-23:00
1
0 13:00-23:00/01:00
0 13:00-23:00; Sa 11:00-23:00; Tu off
0 13:00-23:00; Sa-Su 12:00-23:00
0 13:00-23:00; Tu closed
Expand Down Expand Up @@ -102794,7 +102794,7 @@
0 Mo-Su,PH 19:00-20:00
0 Mo-Su,PH 20:00+
0 Mo-Su,PH 20:00-05:00; May-Aug Mo-Fr,Su,PH 14:30-05:00 "bei gutem Wetter"; May-Aug Sa 12:30-05:00 "bei gutem Wetter"
1
0 Mo-Su,PH 05:45-00:45/01:00
0 Mo-Su,PH 08:00-17:30; "8:00-20:00 Hoogseizoen"
0 Mo-Su,PH 09:00-01:00; Dec 25,Jan 1 off
1
Expand Down
2 changes: 1 addition & 1 deletion test-data/oh.txt-result-strict
Original file line number Diff line number Diff line change
Expand Up @@ -7211,7 +7211,7 @@
0 13:00-23:00
1
1
1
0 13:00-23:00/01:00
0 13:00-23:00; Sa 11:00-23:00; Tu off
0 13:00-23:00; Sa-Su 12:00-23:00
0 13:00-23:00; Tu closed
Expand Down

0 comments on commit 78203e4

Please sign in to comment.