public Time getInternalTime()

in src/main/java/org/mariadb/jdbc/internal/com/read/resultset/rowprotocol/TextRowProtocol.java [638:761]


  public Time getInternalTime(ColumnDefinition columnInfo, Calendar cal, TimeZone timeZone)
      throws SQLException {
    if (lastValueWasNull()) {
      return null;
    }

    if (columnInfo.getColumnType() == ColumnType.TIMESTAMP
        || columnInfo.getColumnType() == ColumnType.DATETIME) {
      int nanoBegin = -1;
      int[] timestampsPart = new int[] {0, 0, 0, 0, 0, 0, 0};
      int partIdx = 0;
      for (int begin = pos; begin < pos + length; begin++) {
        byte b = buf[begin];
        if (b == '-' || b == ' ' || b == ':') {
          partIdx++;
          continue;
        }
        if (b == '.') {
          partIdx++;
          nanoBegin = begin;
          continue;
        }
        if (b < '0' || b > '9') {
          throw new SQLException(
              "cannot parse data in timestamp string '"
                  + new String(buf, pos, length, StandardCharsets.UTF_8)
                  + "'");
        }

        timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48;
      }
      if (timestampsPart[0] == 0
          && timestampsPart[1] == 0
          && timestampsPart[2] == 0
          && timestampsPart[3] == 0
          && timestampsPart[4] == 0
          && timestampsPart[5] == 0
          && timestampsPart[6] == 0) {
        lastValueNull |= BIT_LAST_ZERO_DATE;
        return null;
      }

      // fix non leading tray for nanoseconds
      if (nanoBegin > 0) {
        for (int begin = 0; begin < 6 - (pos + length - nanoBegin - 1); begin++) {
          timestampsPart[6] = timestampsPart[6] * 10;
        }
      }

      if (timeZone == null) {
        Calendar calendar = cal != null ? cal : Calendar.getInstance();
        synchronized (calendar) {
          calendar.clear();
          calendar.set(Calendar.YEAR, 1970);
          calendar.set(Calendar.MONTH, 0);
          calendar.set(Calendar.DAY_OF_MONTH, 1);
          calendar.set(Calendar.HOUR_OF_DAY, timestampsPart[3]);
          calendar.set(Calendar.MINUTE, timestampsPart[4]);
          calendar.set(Calendar.SECOND, timestampsPart[5]);
          calendar.set(Calendar.MILLISECOND, timestampsPart[6] / 1_000);
          return new Time(calendar.getTimeInMillis());
        }
      }

      LocalDateTime ldt =
          LocalDateTime.of(
              timestampsPart[0],
              timestampsPart[1],
              timestampsPart[2],
              timestampsPart[3],
              timestampsPart[4],
              timestampsPart[5],
              timestampsPart[6] * 1000);
      ZonedDateTime zdt =
          ldt.atZone(timeZone.toZoneId()).withZoneSameInstant(TimeZone.getDefault().toZoneId());

      Calendar calendar = cal != null ? cal : Calendar.getInstance();
      synchronized (calendar) {
        calendar.clear();
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, zdt.getHour());
        calendar.set(Calendar.MINUTE, zdt.getMinute());
        calendar.set(Calendar.SECOND, zdt.getSecond());
        calendar.set(Calendar.MILLISECOND, zdt.getNano() / 1_000_000);
        return new Time(calendar.getTimeInMillis());
      }

    } else if (columnInfo.getColumnType() == ColumnType.DATE) {

      throw new SQLException("Cannot read Time using a Types.DATE field");

    } else {
      String raw = new String(buf, pos, length, StandardCharsets.UTF_8);
      if (!options.useLegacyDatetimeCode
          && (raw.startsWith("-") || raw.split(":").length != 3 || raw.indexOf(":") > 3)) {
        throw new SQLException("Time format \"" + raw + "\" incorrect, must be HH:mm:ss");
      }
      boolean negate = raw.startsWith("-");
      if (negate) {
        raw = raw.substring(1);
      }
      String[] rawPart = raw.split(":");
      if (rawPart.length == 3) {
        int hour = Integer.parseInt(rawPart[0]);
        int minutes = Integer.parseInt(rawPart[1]);
        int seconds = Integer.parseInt(rawPart[2].substring(0, 2));
        Calendar calendar = cal != null ? cal : Calendar.getInstance();
        if (options.useLegacyDatetimeCode) {
          calendar.setLenient(true);
        }
        calendar.clear();
        calendar.set(1970, Calendar.JANUARY, 1, (negate ? -1 : 1) * hour, minutes, seconds);
        int nanoseconds = extractNanos(raw);
        calendar.set(Calendar.MILLISECOND, nanoseconds / 1000000);

        return new Time(calendar.getTimeInMillis());
      } else {
        throw new SQLException(
            raw + " cannot be parse as time. time must have \"99:99:99\" format");
      }
    }
  }