Skip to content

Commit d97dabd

Browse files
authored
fix: make the underlying timestamp consistent (#105)
Switch to using a base storage of `ms` and consistent across all parsing methods
1 parent d81c298 commit d97dabd

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

Diff for: lib/src/main/java/io/cloudquery/scalar/Timestamp.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package io.cloudquery.scalar;
22

3-
import java.time.*;
3+
import java.time.Instant;
4+
import java.time.LocalDate;
5+
import java.time.LocalDateTime;
6+
import java.time.ZoneId;
7+
import java.time.ZoneOffset;
8+
import java.time.ZonedDateTime;
49
import org.apache.arrow.vector.types.TimeUnit;
510
import org.apache.arrow.vector.types.pojo.ArrowType;
611

712
public class Timestamp extends Scalar<Long> {
813
public static final ZoneId zoneID = ZoneOffset.UTC;
914

1015
// TODO: add more units support later
11-
private static final ArrowType dt = new ArrowType.Timestamp(TimeUnit.SECOND, zoneID.toString());
16+
private static final ArrowType dt =
17+
new ArrowType.Timestamp(TimeUnit.MILLISECOND, zoneID.toString());
1218

1319
public Timestamp() {
1420
super();
@@ -26,34 +32,36 @@ public ArrowType dataType() {
2632
@Override
2733
public void setValue(Object value) throws ValidationException {
2834
if (value instanceof ZonedDateTime timestamp) {
29-
this.value = timestamp.withZoneSameInstant(zoneID).toEpochSecond();
35+
this.value = timestamp.withZoneSameInstant(zoneID).toEpochSecond() * 1000;
3036
return;
3137
}
3238

3339
if (value instanceof LocalDate date) {
34-
this.value = date.atStartOfDay(zoneID).toEpochSecond();
40+
this.value = date.atStartOfDay(zoneID).toEpochSecond() * 1000;
3541
return;
3642
}
3743

3844
if (value instanceof LocalDateTime date) {
39-
this.value = date.atZone(zoneID).toEpochSecond();
45+
this.value = date.atZone(zoneID).toEpochSecond() * 1000;
4046
return;
4147
}
4248

4349
if (value instanceof Integer integer) {
4450
this.value =
45-
ZonedDateTime.ofInstant(Instant.ofEpochMilli(integer), ZoneOffset.UTC).toEpochSecond();
51+
ZonedDateTime.ofInstant(Instant.ofEpochMilli(integer), ZoneOffset.UTC).toEpochSecond()
52+
* 1000;
4653
return;
4754
}
4855

4956
if (value instanceof Long longValue) {
5057
this.value =
51-
ZonedDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneOffset.UTC).toEpochSecond();
58+
ZonedDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneOffset.UTC).toEpochSecond()
59+
* 1000;
5260
return;
5361
}
5462

5563
if (value instanceof CharSequence sequence) {
56-
this.value = ZonedDateTime.parse(sequence).toEpochSecond();
64+
this.value = ZonedDateTime.parse(sequence).toInstant().toEpochMilli();
5765
return;
5866
}
5967

@@ -64,7 +72,7 @@ public void setValue(Object value) throws ValidationException {
6472
@Override
6573
public java.lang.String toString() {
6674
if (this.value != null) {
67-
return ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) this.value), zoneID).toString();
75+
return ZonedDateTime.ofInstant(Instant.ofEpochMilli((Long) this.value), zoneID).toString();
6876
}
6977

7078
return NULL_VALUE_STRING;

Diff for: lib/src/test/java/io/cloudquery/scalar/TimestampTest.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.cloudquery.scalar;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNull;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
410

511
import java.time.Instant;
612
import java.time.ZoneOffset;
@@ -66,7 +72,7 @@ public void testToString() {
6672
@Test
6773
public void testDataType() {
6874
Timestamp timestamp = new Timestamp();
69-
assertEquals(new ArrowType.Timestamp(TimeUnit.SECOND, "Z"), timestamp.dataType());
75+
assertEquals(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Z"), timestamp.dataType());
7076
}
7177

7278
@Test
@@ -118,7 +124,7 @@ public void testGet() {
118124
timestamp.set(ts);
119125
});
120126
assertTrue(timestamp.isValid());
121-
assertEquals(ts.toEpochSecond(), timestamp.get());
127+
assertEquals(ts.toEpochSecond() * 1000, timestamp.get());
122128

123129
assertDoesNotThrow(
124130
() -> {

0 commit comments

Comments
 (0)