Merge pull request #519 from ypid/fix-OpeningHoursParser

Cleanup opening hours parser.
This commit is contained in:
vshcherb 2013-08-07 10:28:26 -07:00
commit b0fd78c4b3
2 changed files with 67 additions and 51 deletions

1
.gitignore vendored Executable file → Normal file
View file

@ -6,3 +6,4 @@ h_*.png
g_*.png
mm_*.png
mx_*.png
*.class

View file

@ -1,4 +1,5 @@
package net.osmand.util;
/* Has to be commented out in order to run the main function and test the package? */
@ -337,7 +338,7 @@ public class OpeningHoursParser {
@Override
public String toRuleString() {
StringBuilder b = new StringBuilder(25);
{ // Month
// Month
boolean dash = false;
boolean first = true;
for (int i = 0; i < 12; i++) {
@ -360,12 +361,11 @@ public class OpeningHoursParser {
}
if (b.length() != 0) {
b.append(": ");
}
}
// Day
boolean dash = false;
boolean first = true;
boolean open24_7 = true;
dash = false;
first = true;
for (int i = 0; i < 7; i++) {
if (days[i]) {
if (i > 0 && days[i - 1] && i < 6 && days[i + 1]) {
@ -453,7 +453,7 @@ public class OpeningHoursParser {
int previousDay = -1;
int startMonth = -1;
int previousMonth = -1;
int k = 0;
int k = 0; // Position in opening_hours string
BasicOpeningHourRule basic = new BasicOpeningHourRule();
boolean[] days = basic.getDays();
@ -623,6 +623,7 @@ public class OpeningHoursParser {
public static OpeningHours parseOpenedHours(String format){
// split the OSM string in multiple rules
String[] rules = format.split(";"); //$NON-NLS-1$
// FIXME: What if the semicolon is inside a quoted string?
OpeningHours rs = new OpeningHours();
for(String r : rules){
r = r.trim();
@ -651,39 +652,53 @@ public class OpeningHoursParser {
}
/**
* test if the calculated opening hours are what you expect
* @param time the time to test in the format "dd.MM.yyyy HH:mm"
* @param hours the OpeningHours object
* @param expected the expected state
*/
private static void testOpened(String time, OpeningHours hours, boolean expected) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd.MM.yyyy HH:mm").parse(time));
System.out.println("Expected " + time+": " + expected +" = " + hours.isOpenedForTime(cal));
boolean calculated = hours.isOpenedForTime(cal);
System.out.printf(" %sok: Expected %s: %b = %b\n",
((calculated != expected) ? "NOT " : ""), time, expected, calculated);
}
public static void main(String[] args) throws ParseException {
// Test basic case
OpeningHours hours = parseOpenedHours("Mo-Fr 08:30-14:40" ); //$NON-NLS-1$
System.out.println(hours);
testOpened("09.08.2012 11:00", hours, true);
testOpened("09.08.2012 16:00", hours, false);
// two time and date ranges
hours = parseOpenedHours("Mo-We, Fr 08:30-14:40,15:00-19:00"); //$NON-NLS-1$
System.out.println(hours);
testOpened("08.08.2012 14:00", hours, true);
testOpened("10.08.2012 15:00", hours, true);
testOpened("08.08.2012 14:50", hours, false);
testOpened("10.08.2012 15:00", hours, true);
// test exception on general schema
hours = parseOpenedHours("Mo-Sa 08:30-14:40; Tu 08:00 - 14:00"); //$NON-NLS-1$
System.out.println(hours);
testOpened("07.08.2012 14:20", hours, false);
// test off value
hours = parseOpenedHours("Mo-Sa 09:00-18:25; Th off"); //$NON-NLS-1$
System.out.println(hours);
testOpened("08.08.2012 12:00", hours, true);
testOpened("09.08.2012 12:00", hours, false);
//test 24/7
hours = parseOpenedHours("24/7"); //$NON-NLS-1$
System.out.println(hours);
testOpened("08.08.2012 23:59", hours, true);
testOpened("08.08.2012 12:23", hours, true);
testOpened("08.08.2012 06:23", hours, true);
// some people seem to use the following syntax:
hours = parseOpenedHours("Sa-Su 24/7");
System.out.println(hours);