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);
}
}
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);
}
}
Aggregations