private DateTimeResolutionResult match2Time()

in libraries/bot-dialogs/src/main/java/com/microsoft/recognizers/text/datetime/parsers/BaseTimeParser.java [172:341]


    private DateTimeResolutionResult match2Time(Match match, LocalDateTime referenceTime) {

        DateTimeResolutionResult result = new DateTimeResolutionResult();
        boolean hasMin = false;
        boolean hasSec = false;
        boolean hasAm = false;
        boolean hasPm = false;
        boolean hasMid = false;
        int hour = 0;
        int minute = 0;
        int second = 0;
        int day = referenceTime.getDayOfMonth();
        int month = referenceTime.getMonthValue();
        int year = referenceTime.getYear();

        String writtenTimeStr = match.getGroup("writtentime").value;

        if (!StringUtility.isNullOrEmpty(writtenTimeStr)) {
            // get hour
            String hourStr = match.getGroup("hournum").value.toLowerCase();
            hour = config.getNumbers().get(hourStr);

            // get minute
            String minStr = match.getGroup("minnum").value.toLowerCase();
            String tensStr = match.getGroup("tens").value.toLowerCase();

            if (!StringUtility.isNullOrEmpty(minStr)) {
                minute = config.getNumbers().get(minStr);
                if (!StringUtility.isNullOrEmpty(tensStr)) {
                    minute += config.getNumbers().get(tensStr);
                }
                hasMin = true;
            }
        } else if (!StringUtility.isNullOrEmpty(match.getGroup("mid").value)) {
            hasMid = true;
            if (!StringUtility.isNullOrEmpty(match.getGroup("midnight").value)) {
                hour = 0;
                minute = 0;
                second = 0;
            } else if (!StringUtility.isNullOrEmpty(match.getGroup("midmorning").value)) {
                hour = 10;
                minute = 0;
                second = 0;
            } else if (!StringUtility.isNullOrEmpty(match.getGroup("midafternoon").value)) {
                hour = 14;
                minute = 0;
                second = 0;
            } else if (!StringUtility.isNullOrEmpty(match.getGroup("midday").value)) {
                hour = Constants.HalfDayHourCount;
                minute = 0;
                second = 0;
            }
        } else {
            // get hour
            String hourStr = match.getGroup(Constants.HourGroupName).value;
            if (StringUtility.isNullOrEmpty(hourStr)) {
                hourStr = match.getGroup("hournum").value.toLowerCase();
                if (!config.getNumbers().containsKey(hourStr)) {
                    return result;
                }

                hour = config.getNumbers().get(hourStr);
            } else {
                if (!IntegerUtility.canParse(hourStr)) {
                    if (!config.getNumbers().containsKey(hourStr.toLowerCase())) {
                        return result;
                    }

                    hour = config.getNumbers().get(hourStr.toLowerCase());
                } else {
                    hour = Integer.parseInt(hourStr);
                }
            }

            // get minute
            String minStr = match.getGroup(Constants.MinuteGroupName).value.toLowerCase();
            if (StringUtility.isNullOrEmpty(minStr)) {
                minStr = match.getGroup("minnum").value;
                if (!StringUtility.isNullOrEmpty(minStr)) {
                    minute = config.getNumbers().get(minStr);
                    hasMin = true;
                }

                String tensStr = match.getGroup("tens").value;
                if (!StringUtility.isNullOrEmpty(tensStr)) {
                    minute += config.getNumbers().get(tensStr);
                    hasMin = true;
                }
            } else {
                minute = Integer.parseInt(minStr);
                hasMin = true;
            }

            // get second
            String secStr = match.getGroup(Constants.SecondGroupName).value.toLowerCase();
            if (!StringUtility.isNullOrEmpty(secStr)) {
                second = Integer.parseInt(secStr);
                hasSec = true;
            }
        }

        // Adjust by desc string
        String descStr = match.getGroup(Constants.DescGroupName).value.toLowerCase();

        // ampm is a special case in which at 6ampm = at 6
        if (isAmDesc(descStr, match)) {
            if (hour >= Constants.HalfDayHourCount) {
                hour -= Constants.HalfDayHourCount;
            }

            if (!checkRegex(config.getUtilityConfiguration().getAmPmDescRegex(), descStr)) {
                hasAm = true;
            }

        } else if (isPmDesc(descStr, match)) {
            if (hour < Constants.HalfDayHourCount) {
                hour += Constants.HalfDayHourCount;
            }

            hasPm = true;
        }

        // adjust min by prefix
        String timePrefix = match.getGroup(Constants.PrefixGroupName).value.toLowerCase();
        if (!StringUtility.isNullOrEmpty(timePrefix)) {
            PrefixAdjustResult prefixResult = config.adjustByPrefix(timePrefix, hour, minute, hasMin);
            hour = prefixResult.hour;
            minute = prefixResult.minute;
            hasMin = prefixResult.hasMin;
        }

        // adjust hour by suffix
        String timeSuffix = match.getGroup(Constants.SuffixGroupName).value.toLowerCase();
        if (!StringUtility.isNullOrEmpty(timeSuffix)) {
            SuffixAdjustResult suffixResult = config.adjustBySuffix(timeSuffix, hour, minute, hasMin, hasAm, hasPm);
            hour = suffixResult.hour;
            minute = suffixResult.minute;
            hasMin = suffixResult.hasMin;
            hasAm = suffixResult.hasAm;
            hasPm = suffixResult.hasPm;
        }

        if (hour == 24) {
            hour = 0;
        }

        StringBuilder timex = new StringBuilder(String.format("T%02d", hour));

        if (hasMin) {
            timex.append(String.format(":%02d", minute));
        }

        if (hasSec) {
            timex.append(String.format(":%02d", second));
        }

        result.setTimex(timex.toString());

        if (hour <= Constants.HalfDayHourCount && !hasPm && !hasAm && !hasMid) {
            result.setComment(Constants.Comment_AmPm);
        }

        LocalDateTime resultTime = DateUtil.safeCreateFromMinValue(year, month, day, hour, minute, second);
        result.setFutureValue(resultTime);
        result.setPastValue(resultTime);

        result.setSuccess(true);

        return result;
    }