Skip to content

Commit

Permalink
Update getKustoDateTime with more lenient formatter (#246)
Browse files Browse the repository at this point in the history
* Update getKustoDateTime with more lenient formatter

* Convert DateTimeFormatter to static

* Add additional tests for getKustoDateTime

Co-authored-by: AsafMah <[email protected]>
  • Loading branch information
breriksenms and AsafMah authored May 24, 2022
1 parent 15eaf2a commit 2209318
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package com.microsoft.azure.kusto.data;

import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import com.microsoft.azure.kusto.data.format.CslDateTimeFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.json.JSONArray;
Expand Down Expand Up @@ -37,6 +36,8 @@ public class KustoResultSetTable {
private static final String ROWS_PROPERTY_NAME = "Rows";
private static final String EXCEPTIONS_PROPERTY_NAME = "Exceptions";
private static final String EXCEPTIONS_MESSAGE = "Query execution failed with multiple inner exceptions";
private static DateTimeFormatter kustoDateTimeFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME).appendLiteral('Z').toFormatter();

private final List<List<Object>> rows;
private String tableName;
Expand Down Expand Up @@ -465,15 +466,7 @@ public LocalDateTime getKustoDateTime(int columnIndex) {
return null;
}
String dateString = getString(columnIndex);
DateTimeFormatter dateTimeFormatter;
if (dateString.length() < 21) {
dateTimeFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern(CslDateTimeFormat.KUSTO_DATETIME_PATTERN_NO_FRACTIONS)).toFormatter();
} else {
dateTimeFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern(CslDateTimeFormat.KUSTO_DATETIME_PATTERN)).toFormatter();
}
return LocalDateTime.parse(getString(columnIndex), dateTimeFormatter);
return LocalDateTime.parse(dateString, kustoDateTimeFormatter);
}

public LocalDateTime getKustoDateTime(String columnName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.microsoft.azure.kusto.data;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class KustoDateTimeTest {
@Test
void KustoResultSet() throws Exception {
JSONArray rows = new JSONArray();
JSONArray row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.1Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.12Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.123Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.1234Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.12345Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.123456Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.1234567Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.12345678Z"));
rows.put(row);
row = new JSONArray();
row.put(Instant.parse("2022-05-17T00:00:00.123456789Z"));
rows.put(row);

String columns = "[ { \"ColumnName\": \"a\", \"ColumnType\": \"datetime\" } ]";
KustoResultSetTable res = new KustoResultSetTable(new JSONObject("{\"TableName\":\"Table_0\"," +
"\"Columns\":" + columns + ",\"Rows\":" +
rows.toString() + "}"));

Integer rowNum = 0;
while (res.next())
{
Assertions.assertEquals(
LocalDateTime.ofInstant((Instant)((JSONArray)rows.get(rowNum)).get(0), ZoneOffset.UTC),
res.getKustoDateTime(0));
rowNum++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.TimeZone;
import java.util.UUID;

Expand Down Expand Up @@ -75,6 +77,7 @@ void KustoResultSet() throws Exception {
assert res.getBooleanObject(0) == null;
assert res.getString(1).equals("");
assert res.getTimestamp(2) == null;
assert res.getKustoDateTime(2) == null;
assert res.getBigDecimal(3) == null;
assert res.getJSONObject(4) == null;
assert res.getUUID(5) == null;
Expand All @@ -88,6 +91,7 @@ void KustoResultSet() throws Exception {
Assertions.assertTrue(res.getBooleanObject(0));
Assertions.assertEquals(res.getString(1), str);
Assertions.assertEquals(res.getTimestamp(2), Timestamp.valueOf(now.atZone(ZoneId.of("UTC")).toLocalDateTime()));
Assertions.assertEquals(LocalDateTime.ofInstant(now, ZoneOffset.UTC), res.getKustoDateTime(2));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Assertions.assertEquals(sdf.format(new Date(now.getEpochSecond() * 1000)), res.getDate(2).toString());
Expand Down

0 comments on commit 2209318

Please sign in to comment.