Skip to content

Commit 8d4a853

Browse files
committed
pojo to timestamp field
1 parent 88aab67 commit 8d4a853

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.influxdb.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
import java.util.concurrent.TimeUnit;
8+
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.FIELD)
11+
public @interface TimeColumn {
12+
TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
13+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.math.BigDecimal;
66
import java.math.BigInteger;
77
import java.text.NumberFormat;
8+
import java.time.Instant;
89
import java.util.Locale;
910
import java.util.Map;
1011
import java.util.Map.Entry;
@@ -14,6 +15,7 @@
1415
import org.influxdb.BuilderException;
1516
import org.influxdb.annotation.Column;
1617
import org.influxdb.annotation.Measurement;
18+
import org.influxdb.annotation.TimeColumn;
1719
import org.influxdb.impl.Preconditions;
1820

1921
/**
@@ -259,6 +261,15 @@ private void addFieldByAttribute(final Object pojo, final Field field, final Col
259261
try {
260262
Object fieldValue = field.get(pojo);
261263

264+
TimeColumn tc = field.getAnnotation(TimeColumn.class);
265+
if (tc != null && Instant.class.isAssignableFrom(field.getType())) {
266+
Instant instant = (Instant) fieldValue;
267+
TimeUnit timeUint = tc.timeUnit();
268+
this.time = TimeUnit.MILLISECONDS.convert(instant.toEpochMilli(), timeUint);
269+
this.precision = timeUint;
270+
return;
271+
}
272+
262273
if (column.tag()) {
263274
this.tags.put(fieldName, (String) fieldValue);
264275
} else {

0 commit comments

Comments
 (0)