Skip to content

Commit

Permalink
Merge pull request #514 from ratrun/master
Browse files Browse the repository at this point in the history
Fix parsing of duration tag for hh:mm:ss format.
  • Loading branch information
karussell committed Sep 14, 2015
2 parents 46f4828 + 8decef8 commit db6f9cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Expand Up @@ -462,13 +462,14 @@ protected double parseSpeed( String str )
}

/**
* This method parses a string ala "00:00" (hours and minutes) or "0:00:00" (days, hours and
* minutes).
* This method parses a string ala 'hh:mm', format for hours and minutes 'mm', 'hh:mm' or 'hh:mm:ss'
* FIXME: Add support for ISO_8601
* <p>
* @return duration value in minutes
*/
protected static int parseDuration( String str )
{
int minutes = 0;
if (str == null)
return 0;

Expand All @@ -478,22 +479,18 @@ protected static int parseDuration( String str )
// because P1M != PT1M but there are wrong edits in OSM! e.g. http://www.openstreetmap.org/way/24791405
// http://wiki.openstreetmap.org/wiki/Key:duration
if (str.startsWith("P"))
return 0;
return minutes;

int index = str.indexOf(":");
if (index > 0)
{
String hourStr = str.substring(0, index);
String minStr = str.substring(index + 1);
index = minStr.indexOf(":");
int minutes = 0;
if (index > 0)
{
// string contains hours too
String dayStr = hourStr;
hourStr = minStr.substring(0, index);
minStr = minStr.substring(index + 1);
minutes = Integer.parseInt(dayStr) * 60 * 24;
// string contains seconds too: we ignore them
minStr = minStr.substring(0, index);
}

minutes += Integer.parseInt(hourStr) * 60;
Expand All @@ -507,7 +504,7 @@ protected static int parseDuration( String str )
{
logger.warn("Cannot parse " + str + " using 0 minutes");
}
return 0;
return minutes;
}

/**
Expand Down
Expand Up @@ -55,7 +55,7 @@ public void testParseDuration()
assertEquals(0, AbstractFlagEncoder.parseDuration("oh"));
assertEquals(0, AbstractFlagEncoder.parseDuration(null));
assertEquals(60 * 20, AbstractFlagEncoder.parseDuration("20:00"));
assertEquals(60 * 20, AbstractFlagEncoder.parseDuration("0:20:00"));
assertEquals(60 * 24 * 2 + 60 * 20 + 2, AbstractFlagEncoder.parseDuration("02:20:02"));
assertEquals(20, AbstractFlagEncoder.parseDuration("0:20:00"));
assertEquals(60 * 2 + 20, AbstractFlagEncoder.parseDuration("02:20:02"));
}
}

0 comments on commit db6f9cf

Please sign in to comment.