Search in sources :

Example 1 with StringToTimestampParser

use of io.confluent.ksql.util.timestamp.StringToTimestampParser in project ksql by confluentinc.

the class ParseTimestamp method parseTimestamp.

@Udf(description = "Converts a string representation of a date at the given time zone" + " in the given format into the TIMESTAMP value." + " Single quotes in the timestamp format can be escaped with ''," + " for example: 'yyyy-MM-dd''T''HH:mm:ssX'.")
public Timestamp parseTimestamp(@UdfParameter(description = "The string representation of a date.") final String formattedTimestamp, @UdfParameter(description = "The format pattern should be in the format expected by" + " java.time.format.DateTimeFormatter.") final String formatPattern, @UdfParameter(description = " timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," + " \"America/Los_Angeles\", \"PST\", \"Europe/London\"") final String timeZone) {
    if (formattedTimestamp == null || formatPattern == null || timeZone == null) {
        return null;
    }
    try {
        final StringToTimestampParser timestampParser = parsers.get(formatPattern);
        final ZoneId zoneId = ZoneId.of(timeZone);
        return timestampParser.parseToTimestamp(formattedTimestamp, zoneId);
    } catch (final ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to parse timestamp '" + formattedTimestamp + "' at timezone '" + timeZone + "' with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : ZoneId(java.time.ZoneId) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) StringToTimestampParser(io.confluent.ksql.util.timestamp.StringToTimestampParser) ExecutionException(java.util.concurrent.ExecutionException) Udf(io.confluent.ksql.function.udf.Udf)

Example 2 with StringToTimestampParser

use of io.confluent.ksql.util.timestamp.StringToTimestampParser in project ksql by confluentinc.

the class StringToTimestamp method stringToTimestamp.

@Udf(description = "Converts a string representation of a date at the given time zone" + " into the number of milliseconds since 1970-01-01 00:00:00 UTC/GMT." + " Single quotes in the timestamp format can be escaped with ''," + " for example: 'yyyy-MM-dd''T''HH:mm:ssX'.")
public long stringToTimestamp(@UdfParameter(description = "The string representation of a date.") final String formattedTimestamp, @UdfParameter(description = "The format pattern should be in the format expected by" + " java.time.format.DateTimeFormatter.") final String formatPattern, @UdfParameter(description = " timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," + " \"America/Los_Angeles\", \"PST\", \"Europe/London\"") final String timeZone) {
    // there is no sentinel value for a "null" Date.
    try {
        final StringToTimestampParser timestampParser = parsers.get(formatPattern);
        final ZoneId zoneId = ZoneId.of(timeZone);
        return timestampParser.parse(formattedTimestamp, zoneId);
    } catch (final ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to parse timestamp '" + formattedTimestamp + "' at timezone '" + timeZone + "' with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : ZoneId(java.time.ZoneId) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) StringToTimestampParser(io.confluent.ksql.util.timestamp.StringToTimestampParser) ExecutionException(java.util.concurrent.ExecutionException) Udf(io.confluent.ksql.function.udf.Udf)

Aggregations

KsqlFunctionException (io.confluent.ksql.function.KsqlFunctionException)2 Udf (io.confluent.ksql.function.udf.Udf)2 StringToTimestampParser (io.confluent.ksql.util.timestamp.StringToTimestampParser)2 ZoneId (java.time.ZoneId)2 ExecutionException (java.util.concurrent.ExecutionException)2