Skip to content

Commit c8bac67

Browse files
authored
Fix error parsing timestamp containing offset (#575)
The new pattern will parse timestamp containing zone ids or offsets. It's also compatible with the subtle difference between ISO8601 and RFC3339 (offsets with +00:00 and -00:00).
1 parent 1dafcc1 commit c8bac67

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/main/java/org/influxdb/impl/InfluxDBResultMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public class InfluxDBResultMapper {
5757

5858
/**
5959
* When a query is executed without {@link TimeUnit}, InfluxDB returns the <tt>time</tt>
60-
* column as an ISO8601 date.
60+
* column as a RFC3339 date.
6161
*/
62-
private static final DateTimeFormatter ISO8601_FORMATTER = new DateTimeFormatterBuilder()
62+
private static final DateTimeFormatter RFC3339_FORMATTER = new DateTimeFormatterBuilder()
6363
.appendPattern("yyyy-MM-dd'T'HH:mm:ss")
6464
.appendFraction(ChronoField.NANO_OF_SECOND, FRACTION_MIN_WIDTH, FRACTION_MAX_WIDTH, ADD_DECIMAL_POINT)
65-
.appendPattern("X")
65+
.appendZoneOrOffsetId()
6666
.toFormatter();
6767

6868
/**
@@ -338,7 +338,7 @@ <T> boolean fieldValueModified(final Class<?> fieldType, final Field field, fina
338338
if (Instant.class.isAssignableFrom(fieldType)) {
339339
Instant instant;
340340
if (value instanceof String) {
341-
instant = Instant.from(ISO8601_FORMATTER.parse(String.valueOf(value)));
341+
instant = Instant.from(RFC3339_FORMATTER.parse(String.valueOf(value)));
342342
} else if (value instanceof Long) {
343343
instant = Instant.ofEpochMilli(toMillis((long) value, precision));
344344
} else if (value instanceof Double) {

src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,37 @@ void testToPOJOInheritance() {
441441
Assertions.assertEquals(subValue, result.get(0).subValue);
442442
}
443443

444+
@Test
445+
public void testToPOJO_ticket573() {
446+
// Given...
447+
mapper.cacheMeasurementClass(MyCustomMeasurement.class);
448+
449+
List<String> columnList = Arrays.asList("time");
450+
List<List<Object>> valuesList = Arrays.asList(
451+
Arrays.asList("2015-08-17T19:00:00-05:00"), // Chicago (UTC-5)
452+
Arrays.asList("2015-08-17T19:00:00.000000001-05:00"), // Chicago (UTC-5)
453+
Arrays.asList("2000-01-01T00:00:00-00:00"),
454+
Arrays.asList("2000-01-02T00:00:00+00:00")
455+
);
456+
457+
QueryResult.Series series = new QueryResult.Series();
458+
series.setColumns(columnList);
459+
series.setValues(valuesList);
460+
461+
// When...
462+
List<MyCustomMeasurement> result = new LinkedList<>();
463+
mapper.parseSeriesAs(series, MyCustomMeasurement.class, result);
464+
465+
// Then...
466+
Assertions.assertEquals(4, result.size(), "incorrect number of elemets");
467+
// Note: RFC3339 timestamp with TZ from InfluxDB are parsed into an Instant (UTC)
468+
Assertions.assertTrue(result.get(0).time.equals(Instant.parse("2015-08-18T00:00:00Z")));
469+
Assertions.assertTrue(result.get(1).time.equals(Instant.parse("2015-08-18T00:00:00.000000001Z")));
470+
// RFC3339 section 4.3 https://tools.ietf.org/html/rfc3339#section-4.3
471+
Assertions.assertTrue(result.get(2).time.equals(Instant.parse("2000-01-01T00:00:00Z")));
472+
Assertions.assertTrue(result.get(3).time.equals(Instant.parse("2000-01-02T00:00:00Z")));
473+
}
474+
444475
@Measurement(name = "CustomMeasurement")
445476
static class MyCustomMeasurement {
446477

0 commit comments

Comments
 (0)