Repository: influxdata/influxdb-java
Pull request #973 (2023-12-02 19:27:40)

Fix time column with precision > ms
When `@TimeColumn` is used with `timeUnit` larger than a millisecond, saving the pojo would result in a wrong timestamp.
This change fixes that.


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/influxdata/influxdb-java/pull/973?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`ba9bfeb`)](https://app.codecov.io/gh/influxdata/influxdb-java/commit/ba9bfebf2a24672b48827660ece79b45fbefe8bd?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) 61.28% compared to head [(`b95938e`)](https://app.codecov.io/gh/influxdata/influxdb-java/pull/973?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) 55.13%.

> :exclamation: Current head b95938e differs from pull request most recent head 3a2cf97. Consider uploading reports for the commit 3a2cf97 to get more accurate results


:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master     #973      +/-   ##
============================================
- Coverage     61.28%   55.13%   -6.16%     
+ Complexity      450      400      -50     
============================================
  Files            70       70              
  Lines          2591     2590       -1     
  Branches        277      277              
============================================
- Hits           1588     1428     -160     
- Misses          932     1094     +162     
+ Partials         71       68       -3     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/influxdata/influxdb-java/pull/973?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata).


Patch:
diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java
index b8cc9e2..d7eb039 100755
--- a/src/main/java/org/influxdb/dto/Point.java
+++ b/src/main/java/org/influxdb/dto/Point.java
@@ -331,8 +331,7 @@ public class Point {
                                     .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;
           });


Test patch:
diff --git a/src/test/java/org/influxdb/dto/PointTest.java b/src/test/java/org/influxdb/dto/PointTest.java
index c315aad..b59440c 100755
--- a/src/test/java/org/influxdb/dto/PointTest.java
+++ b/src/test/java/org/influxdb/dto/PointTest.java
@@ -695,6 +695,23 @@ public class PointTest {
         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();
@@ -914,6 +931,14 @@ public class PointTest {
         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 {