Skip to content

fix: make the underlying timestamp consistent #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/src/main/java/io/cloudquery/scalar/Timestamp.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package io.cloudquery.scalar;

import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.apache.arrow.vector.types.TimeUnit;
import org.apache.arrow.vector.types.pojo.ArrowType;

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

// TODO: add more units support later
private static final ArrowType dt = new ArrowType.Timestamp(TimeUnit.SECOND, zoneID.toString());
private static final ArrowType dt =
new ArrowType.Timestamp(TimeUnit.MILLISECOND, zoneID.toString());

public Timestamp() {
super();
Expand All @@ -26,34 +32,36 @@ public ArrowType dataType() {
@Override
public void setValue(Object value) throws ValidationException {
if (value instanceof ZonedDateTime timestamp) {
this.value = timestamp.withZoneSameInstant(zoneID).toEpochSecond();
this.value = timestamp.withZoneSameInstant(zoneID).toEpochSecond() * 1000;
return;
}

if (value instanceof LocalDate date) {
this.value = date.atStartOfDay(zoneID).toEpochSecond();
this.value = date.atStartOfDay(zoneID).toEpochSecond() * 1000;
return;
}

if (value instanceof LocalDateTime date) {
this.value = date.atZone(zoneID).toEpochSecond();
this.value = date.atZone(zoneID).toEpochSecond() * 1000;
return;
}

if (value instanceof Integer integer) {
this.value =
ZonedDateTime.ofInstant(Instant.ofEpochMilli(integer), ZoneOffset.UTC).toEpochSecond();
ZonedDateTime.ofInstant(Instant.ofEpochMilli(integer), ZoneOffset.UTC).toEpochSecond()
* 1000;
return;
}

if (value instanceof Long longValue) {
this.value =
ZonedDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneOffset.UTC).toEpochSecond();
ZonedDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneOffset.UTC).toEpochSecond()
* 1000;
return;
}

if (value instanceof CharSequence sequence) {
this.value = ZonedDateTime.parse(sequence).toEpochSecond();
this.value = ZonedDateTime.parse(sequence).toInstant().toEpochMilli();
return;
}

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

return NULL_VALUE_STRING;
Expand Down
12 changes: 9 additions & 3 deletions lib/src/test/java/io/cloudquery/scalar/TimestampTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package io.cloudquery.scalar;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Instant;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -66,7 +72,7 @@ public void testToString() {
@Test
public void testDataType() {
Timestamp timestamp = new Timestamp();
assertEquals(new ArrowType.Timestamp(TimeUnit.SECOND, "Z"), timestamp.dataType());
assertEquals(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Z"), timestamp.dataType());
}

@Test
Expand Down Expand Up @@ -118,7 +124,7 @@ public void testGet() {
timestamp.set(ts);
});
assertTrue(timestamp.isValid());
assertEquals(ts.toEpochSecond(), timestamp.get());
assertEquals(ts.toEpochSecond() * 1000, timestamp.get());

assertDoesNotThrow(
() -> {
Expand Down