Skip to content

Commit

Permalink
fix: Incorrect date-time reporting in the UI due to time zone handling (
Browse files Browse the repository at this point in the history
#9)

This commit fixes the handling of UTC to local time zone conversion,
which was causing incorrect date-time display in the UI.
  • Loading branch information
KaviarasuSakthivadivel authored Jan 15, 2025
1 parent 2e2520f commit fcb4c6c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -132,9 +134,10 @@ private LocalDateTime getLocalDateTime(Calendar calendar) {

if (calendar != null) {
TimeZone timeZone = calendar.getTimeZone();
long millis = this.timeUnit.toMillis(value);
localDateTime = localDateTime.minus(
(long) timeZone.getOffset(millis) - (long) this.timeZone.getOffset(millis), ChronoUnit.MILLIS);
ZonedDateTime utcZonedDateTime =
ZonedDateTime.ofInstant(Instant.ofEpochMilli(this.timeUnit.toMillis(value)), ZoneOffset.UTC);
localDateTime =
utcZonedDateTime.withZoneSameInstant(timeZone.toZoneId()).toLocalDateTime();
}
return localDateTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.salesforce.datacloud.jdbc.core.accessor.SoftAssertions;
import com.salesforce.datacloud.jdbc.util.RootAllocatorTestExtension;
import com.salesforce.datacloud.jdbc.util.TestWasNullConsumer;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -486,6 +488,40 @@ void testNulledTimestampVector() {
consumer.assertThat().hasNotNullSeen(0).hasNullSeen(NUM_OF_METHODS * values.size());
}

@SneakyThrows
@Test
void testGetTimestampWithDifferentTimeZone() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
List<Integer> monthNumber = getRandomMonthNumber();
val values = getMilliSecondValues(calendar, monthNumber);
val consumer = new TestWasNullConsumer(collector);

try (val vector = extension.createTimeStampNanoVector(values)) {
val i = new AtomicInteger(0);
val sut = new TimeStampVectorAccessor(vector, i::get, consumer);

Calendar pstCalendar = Calendar.getInstance(TimeZone.getTimeZone("PST"));

for (; i.get() < vector.getValueCount(); i.incrementAndGet()) {
val timestampValue = sut.getTimestamp(pstCalendar);
val stringValue = sut.getString();
val currentMillis = values.get(i.get());

pstCalendar.setTimeInMillis(currentMillis);
long pstMillis = pstCalendar.getTimeInMillis();

LocalDateTime expectedPST = LocalDateTime.ofInstant(
Instant.ofEpochMilli(pstMillis),
TimeZone.getTimeZone("PST").toZoneId());
Timestamp expectedTimestamp = Timestamp.valueOf(expectedPST);

collector.assertThat(timestampValue).isEqualTo(expectedTimestamp);
assertISOStringLike(stringValue, currentMillis);
}
}
}

private List<Long> getMilliSecondValues(Calendar calendar, List<Integer> monthNumber) {
List<Long> result = new ArrayList<>();
for (int currentNumber : monthNumber) {
Expand Down

0 comments on commit fcb4c6c

Please sign in to comment.