package seedu.address.logic.parser;

import java.time.LocalTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.commons.exceptions.IllegalValueException;

/**
 * A parser for 12-hour clock times.
 */
public class TimeParser {

    private static final Pattern PATTERN_TIME = Pattern.compile("(?<hour>\\d{1,2})"
            + "(?:[.:](?<minute>\\d{2}))?(?<ampm>am|pm)");

    private final LocalTime referenceTime;

    public TimeParser(LocalTime referenceTime) {
        this.referenceTime = referenceTime;
    }

    public LocalTime getReferenceTime() {
        return referenceTime;
    }

    public LocalTime parse(String str) throws IllegalValueException {
        final Matcher matcher = PATTERN_TIME.matcher(str.trim());
        if (!matcher.matches()) {
            throw new IllegalValueException("invalid time format");
        }

        int hour = Integer.parseInt(matcher.group("hour"));
        if (hour > 12) {
            throw new IllegalValueException("invalid hour: " + hour);
        } else if (hour == 12) {
            hour = 0;
        }

        final int minute = matcher.group("minute") != null ? Integer.parseInt(matcher.group("minute")) : 0;
        if (minute >= 60) {
            throw new IllegalValueException("invalid minute: " + minute);
        }

        final boolean isPM = matcher.group("ampm").equals("pm");
        return LocalTime.of(hour + (isPM ? 12 : 0), minute);
    }

}

@@ -156,40 +156,27 @@
 ons.
-exceptions.IllegalValueException
+util.SubstringRange
 ;%0A%0A/
@@ -242,16 +242,45 @@
 meParser
+ implements Parser%3CLocalTime%3E
  %7B%0A%0A    
@@ -555,32 +555,96 @@
 nceTime;%0A    %7D%0A%0A
+    public TimeParser() %7B%0A        this(LocalTime.now());%0A    %7D%0A%0A
     public Local
@@ -698,32 +698,46 @@
 nceTime;%0A    %7D%0A%0A
+    @Override%0A
     public Local
@@ -766,27 +766,20 @@
  throws 
-IllegalValu
+Pars
 eExcepti
@@ -901,35 +901,28 @@
   throw new 
-IllegalValu
+Pars
 eException(%22
@@ -941,16 +941,40 @@
  format%22
+, SubstringRange.of(str)
 );%0A     
@@ -1082,35 +1082,28 @@
   throw new 
-IllegalValu
+Pars
 eException(%22
@@ -1124,16 +1124,117 @@
 %22 + hour
+,%0A                                     new SubstringRange(matcher.start(%22hour%22), matcher.end(%22hour%22))
 );%0A     
@@ -1426,24 +1426,76 @@
 te %3E= 60) %7B%0A
+            assert matcher.group(%22minute%22) != null;%0A
             
@@ -1508,19 +1508,12 @@
 new 
-IllegalValu
+Pars
 eExc
@@ -1542,24 +1542,129 @@
 : %22 + minute
+,%0A                                     new SubstringRange(matcher.start(%22minute%22), matcher.end(%22minute%22))
 );%0A        %7D

implement Parser interface