/**
   * Our rule is that least significant date unit determines the resolution.
   *
   * @param durationStr the duration string to check
   * @return the data unit
   */
  Precision reverseParseAtlassianDuration(String durationStr) {
    Precision rank = null;

    Matcher matcher = DURATION_PATTERN.matcher(durationStr);
    while (matcher.find()) {
      Precision current;
      String util = StringUtils.stripToNull(matcher.group(1));
      if (util == null || util.equals("m")) {
        // If we find minutes then just return immediately.
        return Precision.MINUTES;
      } else if (util.equals("h")) {
        current = Precision.HOURS;
      } else if (util.equals("d") || util.equals("w")) {
        current = Precision.DAYS;
      } else {
        // Assume minutes in the worst possible case.
        return Precision.MINUTES;
      }

      if (rank == null || rank.compareTo(current) > 0) {
        rank = current;
      }
    }
    return rank == null ? Precision.MINUTES : rank;
  }
  /**
   * This will round out a date to a precision based on the input string. We need to do this to be
   * more meaningful to the user when the type in x = '2011-10-13'.
   *
   * @param returnDate the parsed data in its specific form
   * @param acceptedFormat the format we used to
   * @param dateString
   * @return
   */
  private DateRange toPrecision(Date returnDate, String acceptedFormat, String dateString) {
    Precision precision = null;

    if (YYYY_MM_DD_HH_MM1.equals(acceptedFormat) || YYYY_MM_DD_HH_MM2.equals(acceptedFormat)) {
      precision = Precision.MINUTES;
    } else if (YYYY_MM_DD1.equals(acceptedFormat) || YYYY_MM_DD2.equals(acceptedFormat)) {
      precision = Precision.DAYS;
    } else if (ATLASSIAN_DURATION.equals(acceptedFormat)) {
      // ok its an atlassian duration
      precision = reverseParseAtlassianDuration(dateString.toLowerCase(Locale.ENGLISH));
    }

    if (precision != null) {
      return precision.createRange(returnDate, timeZoneManager.getLoggedInUserTimeZone());
    } else {
      // Is this even possible? probably not but here we are so something sensible
      return new DateRange(returnDate, returnDate);
    }
  }