From fe1c10d1cb072c5cf477ebbfb5e7d1ab06f35bcc Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Sun, 22 Sep 2013 09:58:36 +0200 Subject: improve calendar time detection * now works for (wrong) time format "xx.00 o'clock" (where there is a dot instead of a colon) --- main/src/cgeo/geocaching/Geocache.java | 40 ++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'main') 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 patterns = new ArrayList(); + + // 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 -- cgit v1.1