Skip to content

Commit e5518be

Browse files
authored
Fix time column with precision > ms (#973)
1 parent ba9bfeb commit e5518be

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/main/java/org/influxdb/dto/Point.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ private void addFieldByAttribute(final Object pojo, final Field field, final boo
331331
.add(BigInteger.valueOf(instant.getNano()))
332332
.divide(BigInteger.valueOf(TimeUnit.NANOSECONDS.convert(1, timeUnit)));
333333
} else {
334-
this.time = TimeUnit.MILLISECONDS.convert(instant.toEpochMilli(), timeUnit);
335-
this.precision = timeUnit;
334+
this.time = timeUnit.convert(instant.toEpochMilli(), TimeUnit.MILLISECONDS);
336335
}
337336
this.precision = timeUnit;
338337
});

src/test/java/org/influxdb/dto/PointTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,23 @@ public void testAddFieldsFromPOJOWithTimeColumnNanoseconds() throws NoSuchFieldE
695695
pojo.time = null;
696696
}
697697

698+
@Test
699+
public void testAddFieldsFromPOJOWithTimeColumnSeconds() throws NoSuchFieldException, IllegalAccessException {
700+
TimeColumnPojoSec pojo = new TimeColumnPojoSec();
701+
pojo.time = Instant.now().plusSeconds(132L).plus(365L * 12000, ChronoUnit.DAYS);
702+
pojo.booleanPrimitive = true;
703+
704+
Point p = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();
705+
Field timeField = p.getClass().getDeclaredField("time");
706+
Field precisionField = p.getClass().getDeclaredField("precision");
707+
timeField.setAccessible(true);
708+
precisionField.setAccessible(true);
709+
710+
Assertions.assertEquals(pojo.booleanPrimitive, p.getFields().get("booleanPrimitive"));
711+
Assertions.assertEquals(TimeUnit.SECONDS, precisionField.get(p));
712+
Assertions.assertEquals(pojo.time.getEpochSecond(), timeField.get(p));
713+
}
714+
698715
@Test
699716
public void testAddFieldsFromPOJOWithTimeColumnNull() throws NoSuchFieldException, IllegalAccessException {
700717
TimeColumnPojo pojo = new TimeColumnPojo();
@@ -914,6 +931,14 @@ static class TimeColumnPojoNano {
914931
private Instant time;
915932
}
916933

934+
@Measurement(name = "tcmeasurement", allFields = true)
935+
static class TimeColumnPojoSec {
936+
boolean booleanPrimitive;
937+
938+
@TimeColumn(timeUnit = TimeUnit.SECONDS)
939+
Instant time;
940+
}
941+
917942
@Measurement(name = "mymeasurement")
918943
static class Pojo {
919944

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy