@Udf(description = "Converts a number of milliseconds since 1970-01-01 00:00:00 UTC/GMT into the"
      + " string representation of the timestamp in the given format. Single quotes in the"
      + " timestamp format can be escaped with '', for example: 'yyyy-MM-dd''T''HH:mm:ssX'."
      + " The system default time zone is used when no time zone is explicitly provided."
      + " The format pattern should be in the format expected"
      + " by java.time.format.DateTimeFormatter")
  public String timestampToString(
      @UdfParameter(
          description = "Milliseconds since"
              + " January 1, 1970, 00:00:00 UTC/GMT.") final long epochMilli,
      @UdfParameter(
          description = "The format pattern should be in the format expected by"
              + " java.time.format.DateTimeFormatter.") final String formatPattern) {
    if (formatPattern == null) {
      return null;
    }
    try {
      final Timestamp timestamp = new Timestamp(epochMilli);
      final DateTimeFormatter formatter = formatters.get(formatPattern);
      return timestamp.toInstant()
          .atZone(ZoneId.systemDefault())
          .format(formatter);
    } catch (final ExecutionException | RuntimeException e) {
      throw new KsqlFunctionException("Failed to format timestamp " + epochMilli
          + " with formatter '" + formatPattern
          + "': " + e.getMessage(), e);
    }

  }
@Test
  public void shouldRoundTripWithStringToTimestamp() {
    final String pattern = "yyyy-MM-dd HH:mm:ss.SSS'Freya'";
    final StringToTimestamp stringToTimestamp = new StringToTimestamp();
    IntStream.range(-10_000, 20_000)
        .parallel()
        .forEach(idx -> {
          final long millis = 1538361611123L + idx;
          final String result = udf.timestampToString(millis, pattern);
          final String expectedResult = new SimpleDateFormat(pattern).format(new Date(millis));
          assertThat(result, is(expectedResult));

          final long roundtripMillis = stringToTimestamp.stringToTimestamp(result, pattern);
          assertThat(roundtripMillis, is(millis));
        });
  }