Skip to content

Commit

Permalink
Fix time column with precision > ms (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
eranl authored Dec 3, 2023
1 parent ba9bfeb commit e5518be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ private void addFieldByAttribute(final Object pojo, final Field field, final boo
.add(BigInteger.valueOf(instant.getNano()))
.divide(BigInteger.valueOf(TimeUnit.NANOSECONDS.convert(1, timeUnit)));
} else {
this.time = TimeUnit.MILLISECONDS.convert(instant.toEpochMilli(), timeUnit);
this.precision = timeUnit;
this.time = timeUnit.convert(instant.toEpochMilli(), TimeUnit.MILLISECONDS);
}
this.precision = timeUnit;
});
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/influxdb/dto/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,23 @@ public void testAddFieldsFromPOJOWithTimeColumnNanoseconds() throws NoSuchFieldE
pojo.time = null;
}

@Test
public void testAddFieldsFromPOJOWithTimeColumnSeconds() throws NoSuchFieldException, IllegalAccessException {
TimeColumnPojoSec pojo = new TimeColumnPojoSec();
pojo.time = Instant.now().plusSeconds(132L).plus(365L * 12000, ChronoUnit.DAYS);
pojo.booleanPrimitive = true;

Point p = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();
Field timeField = p.getClass().getDeclaredField("time");
Field precisionField = p.getClass().getDeclaredField("precision");
timeField.setAccessible(true);
precisionField.setAccessible(true);

Assertions.assertEquals(pojo.booleanPrimitive, p.getFields().get("booleanPrimitive"));
Assertions.assertEquals(TimeUnit.SECONDS, precisionField.get(p));
Assertions.assertEquals(pojo.time.getEpochSecond(), timeField.get(p));
}

@Test
public void testAddFieldsFromPOJOWithTimeColumnNull() throws NoSuchFieldException, IllegalAccessException {
TimeColumnPojo pojo = new TimeColumnPojo();
Expand Down Expand Up @@ -914,6 +931,14 @@ static class TimeColumnPojoNano {
private Instant time;
}

@Measurement(name = "tcmeasurement", allFields = true)
static class TimeColumnPojoSec {
boolean booleanPrimitive;

@TimeColumn(timeUnit = TimeUnit.SECONDS)
Instant time;
}

@Measurement(name = "mymeasurement")
static class Pojo {

Expand Down

0 comments on commit e5518be

Please sign in to comment.