Skip to content

Commit

Permalink
Moved formatter for date time over.
Browse files Browse the repository at this point in the history
Signed-off-by: MitchellGale-BitQuill <[email protected]>
  • Loading branch information
MitchellGale committed Sep 28, 2022
1 parent 9b9250e commit eb0c894
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_FORMATTER_SHORT_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_LONG_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_SHORT_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_STRICT_WITH_TZ;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -669,11 +670,11 @@ private ExprValue exprDate(ExprValue exprValue) {
*/
private ExprValue exprDateTime(ExprValue dateTime, ExprValue timeZone) {
String defaultTimeZone = TimeZone.getDefault().getID();
DateTimeFormatter formatDT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss[xxx]")
.withResolverStyle(ResolverStyle.STRICT);


try {
LocalDateTime ldtFormatted = LocalDateTime.parse(dateTime.stringValue(), formatDT);
LocalDateTime ldtFormatted =
LocalDateTime.parse(dateTime.stringValue(), DATE_TIME_FORMATTER_STRICT_WITH_TZ);
if (timeZone.isNull()) {
return new ExprDatetimeValue(ldtFormatted);
}
Expand All @@ -688,7 +689,8 @@ private ExprValue exprDateTime(ExprValue dateTime, ExprValue timeZone) {
String toTz;

try {
ZonedDateTime zdtWithZoneOffset = ZonedDateTime.parse(dateTime.stringValue(), formatDT);
ZonedDateTime zdtWithZoneOffset =
ZonedDateTime.parse(dateTime.stringValue(), DATE_TIME_FORMATTER_STRICT_WITH_TZ);
ZoneId fromTZ = zdtWithZoneOffset.getZone();

ldt = new ExprDatetimeValue(zdtWithZoneOffset.toLocalDateTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,10 @@ public class DateTimeFormatters {
.appendPattern("MMddHHmmss")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);

public static final DateTimeFormatter DATE_TIME_FORMATTER_STRICT_WITH_TZ =
new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH:mm:ss[xxx]")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);
}

0 comments on commit eb0c894

Please sign in to comment.