diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-09-22 09:58:36 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-09-22 09:58:36 +0200 |
| commit | fe1c10d1cb072c5cf477ebbfb5e7d1ab06f35bcc (patch) | |
| tree | 42cf24a47179092d3f136a24fd5456023e8d38a8 | |
| parent | 8a539e82bac733031257e053b5994c8d662c5334 (diff) | |
| download | cgeo-fe1c10d1cb072c5cf477ebbfb5e7d1ab06f35bcc.zip cgeo-fe1c10d1cb072c5cf477ebbfb5e7d1ab06f35bcc.tar.gz cgeo-fe1c10d1cb072c5cf477ebbfb5e7d1ab06f35bcc.tar.bz2 | |
improve calendar time detection
* now works for (wrong) time format "xx.00 o'clock" (where there is a
dot instead of a colon)
| -rw-r--r-- | main/src/cgeo/geocaching/Geocache.java | 40 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/GeocacheTest.java | 38 |
2 files changed, 53 insertions, 25 deletions
diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index 912d1be..76b55bc 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -1695,30 +1695,28 @@ public class Geocache implements ICache, IWaypoint { if (!isEventCache()) { return null; } - // 12:34 - final Pattern time = Pattern.compile("\\b(\\d{1,2})\\:(\\d\\d)\\b"); - final MatcherWrapper matcher = new MatcherWrapper(time, getDescription()); - while (matcher.find()) { - try { - final int hours = Integer.valueOf(matcher.group(1)); - final int minutes = Integer.valueOf(matcher.group(2)); - if (hours >= 0 && hours < 24 && minutes >= 0 && minutes < 60) { - return String.valueOf(hours * 60 + minutes); - } - } catch (final NumberFormatException e) { - // cannot happen, but static code analysis doesn't know - } - } - // 12 o'clock + final String hourLocalized = CgeoApplication.getInstance().getString(R.string.cache_time_full_hours); + ArrayList<Pattern> patterns = new ArrayList<Pattern>(); + + // 12:34 + patterns.add(Pattern.compile("\\b(\\d{1,2})\\:(\\d\\d)\\b")); if (StringUtils.isNotBlank(hourLocalized)) { - final Pattern fullHours = Pattern.compile("\\b(\\d{1,2})\\s+" + Pattern.quote(hourLocalized), Pattern.CASE_INSENSITIVE); - final MatcherWrapper matcherHours = new MatcherWrapper(fullHours, getDescription()); - if (matcherHours.find()) { + // 12 o'clock, 12.00 o'clock + patterns.add(Pattern.compile("\\b(\\d{1,2})(?:\\.00)?\\s+" + Pattern.quote(hourLocalized), Pattern.CASE_INSENSITIVE)); + } + + for (Pattern pattern : patterns) { + final MatcherWrapper matcher = new MatcherWrapper(pattern, getDescription()); + while (matcher.find()) { try { - final int hours = Integer.valueOf(matcherHours.group(1)); - if (hours >= 0 && hours < 24) { - return String.valueOf(hours * 60); + final int hours = Integer.valueOf(matcher.group(1)); + int minutes = 0; + if (matcher.groupCount() >= 2) { + minutes = Integer.valueOf(matcher.group(2)); + } + if (hours >= 0 && hours < 24 && minutes >= 0 && minutes < 60) { + return String.valueOf(hours * 60 + minutes); } } catch (final NumberFormatException e) { // cannot happen, but static code analysis doesn't know diff --git a/tests/src/cgeo/geocaching/GeocacheTest.java b/tests/src/cgeo/geocaching/GeocacheTest.java index 2f5281c..7d26370 100644 --- a/tests/src/cgeo/geocaching/GeocacheTest.java +++ b/tests/src/cgeo/geocaching/GeocacheTest.java @@ -5,13 +5,15 @@ import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.list.StoredList; +import org.apache.commons.lang3.StringUtils; + import java.util.ArrayList; import java.util.Date; import java.util.List; public class GeocacheTest extends CGeoTestCase { - final static private class MockedEventCache extends Geocache { + private static final class MockedEventCache extends Geocache { public MockedEventCache(final Date date) { setHidden(date); setType(CacheType.EVENT); @@ -51,15 +53,15 @@ public class GeocacheTest extends CGeoTestCase { assertEquals("GC1234", cache.getGeocode()); } - public void testUpdateWaypointFromNote() { + public final void testUpdateWaypointFromNote() { assertWaypointsParsed("Test N51 13.888 E007 03.444", 1); } - public void testUpdateWaypointsFromNote() { + public final void testUpdateWaypointsFromNote() { assertWaypointsParsed("Test N51 13.888 E007 03.444 Test N51 13.233 E007 03.444 Test N51 09.123 E007 03.444", 3); } - private void assertWaypointsParsed(String note, int expectedWaypoints) { + private void assertWaypointsParsed(final String note, final int expectedWaypoints) { recordMapStoreFlags(); @@ -221,4 +223,32 @@ public class GeocacheTest extends CGeoTestCase { cache.setName("GR8 01-01"); assertEquals("GR000008 000001-000001", cache.getNameForSorting()); } + + public static void testGuessEventTime() { + assertTime("text 14:20 text", 14, 20); + assertNoTime("text 30:40 text"); + assertNoTime("text 14:90 text"); + final String time_hours = CgeoApplication.getInstance().getString(R.string.cache_time_full_hours); + assertTime("text 16 " + time_hours, 16, 0); + assertTime("text 16 " + StringUtils.lowerCase(time_hours), 16, 0); + assertTime("text 16:00 " + time_hours, 16, 0); + assertTime("text 16.00 " + time_hours, 16, 0); + assertTime("text 14:20.", 14, 20); + assertTime("<b>14:20</b>", 14, 20); + } + + private static void assertTime(final String description, final int hours, final int minutes) { + final Geocache cache = new Geocache(); + cache.setDescription(description); + cache.setType(CacheType.EVENT); + final int minutesAfterMidnight = hours * 60 + minutes; + assertEquals(String.valueOf(minutesAfterMidnight), cache.guessEventTimeMinutes()); + } + + private static void assertNoTime(final String description) { + final Geocache cache = new Geocache(); + cache.setDescription(description); + cache.setType(CacheType.EVENT); + assertNull(cache.guessEventTimeMinutes()); + } } |
