Skip to content

Commit

Permalink
Improved ISO datetime parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderlech authored and nineinchnick committed Sep 28, 2024
1 parent 7c93b14 commit bd78780
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/pl/net/was/JsonTrinoConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ public static Object convert(JsonNode jsonNode, Type type, Schema<?> schemaType)
}
if (type instanceof TimestampType timestampType) {
String format = schemaType.getFormat();
DateTimeFormatter dateFormatter;
if (format.equals("date-time")) {
format = "yyyy-MM-dd'T'HH:mm:ss[.SSSSSSSSS][.SSSSSS][.SSS]XXX";
dateFormatter = DateTimeFormatter.ISO_DATE_TIME;
}
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(format);
else {
dateFormatter = DateTimeFormatter.ofPattern(format);
}

Instant instant = dateFormatter.parse(jsonNode.asText(), Instant::from);
long epochMicros = instant.toEpochMilli() * MICROSECONDS_PER_MILLISECOND;
return Timestamps.round(epochMicros, 6 - timestampType.getPrecision());
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/pl/net/was/JsonTrinoConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.net.was;

import com.fasterxml.jackson.databind.node.TextNode;
import io.swagger.v3.oas.models.media.Schema;
import io.trino.spi.type.DateType;
import io.trino.spi.type.TimestampType;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class JsonTrinoConverterTest
{
@Test
void shouldConvertDifferentDateTimeFormatsIntoLong()
{
var type = TimestampType.createTimestampType(3);
var schema = new Schema<>();

schema.setFormat("date-time");

assertThat(JsonTrinoConverter.convert(TextNode.valueOf("2024-09-24T19:17:58.638+04:00"), type, schema))
.isEqualTo(1727191078638000L);

assertThat(JsonTrinoConverter.convert(TextNode.valueOf("2024-09-24T18:30:47.94+04:00"), type, schema))
.isEqualTo(1727188247940000L);
}

@Test
void shouldConvertDateToLong()
{
var type = DateType.DATE;
var schema = new Schema<>();

schema.setFormat("date");

assertThat(JsonTrinoConverter.convert(TextNode.valueOf("2024-09-24"), type, schema))
.isEqualTo(19990);
}
}

0 comments on commit bd78780

Please sign in to comment.