Skip to content

Commit

Permalink
DRILL-7150: Fix timezone conversion for timestamp from MaprDB after t…
Browse files Browse the repository at this point in the history
…he transition from PDT to PST

closes #1729
  • Loading branch information
vvysotskyi authored and sohami committed Apr 5, 2019
1 parent c9d3501 commit 3772757
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Expand Up @@ -35,7 +35,6 @@
import org.apache.drill.common.expression.ValueExpressions.TimeStampExpression;
import org.apache.drill.common.expression.ValueExpressions.VarDecimalExpression;
import org.apache.drill.common.expression.visitors.AbstractExprVisitor;
import org.apache.drill.exec.expr.fn.impl.DateUtility;
import org.joda.time.LocalTime;
import org.ojai.Value;
import org.ojai.types.ODate;
Expand All @@ -47,6 +46,10 @@

import org.ojai.types.OTimestamp;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

class CompareFunctionsProcessor extends AbstractExprVisitor<Boolean, LogicalExpression, RuntimeException> {

private String functionName;
Expand Down Expand Up @@ -93,7 +96,11 @@ public static CompareFunctionsProcessor processWithTimeZoneOffset(FunctionCall c
protected boolean visitTimestampExpr(SchemaPath path, TimeStampExpression valueArg) {
// converts timestamp value from local time zone to UTC since the record reader
// reads the timestamp in local timezone if the readTimestampWithZoneOffset flag is enabled
long timeStamp = valueArg.getTimeStamp() - DateUtility.TIMEZONE_OFFSET_MILLIS;
Instant localInstant = Instant.ofEpochMilli(valueArg.getTimeStamp());
ZonedDateTime utcZonedDateTime = localInstant.atZone(ZoneId.of("UTC"));
ZonedDateTime convertedZonedDateTime = utcZonedDateTime.withZoneSameLocal(ZoneId.systemDefault());
long timeStamp = convertedZonedDateTime.toInstant().toEpochMilli();

this.value = KeyValueBuilder.initFrom(new OTimestamp(timeStamp));
this.path = path;
return true;
Expand Down
Expand Up @@ -64,7 +64,8 @@
import org.apache.drill.shaded.guava.com.google.common.collect.Sets;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -307,7 +308,7 @@ private void setupWriter() {
@Override
protected void writeTimeStamp(MapOrListWriterImpl writer, String fieldName, DocumentReader reader) {
String formattedTimestamp = Instant.ofEpochMilli(reader.getTimestampLong())
.atOffset(OffsetDateTime.now().getOffset()).format(DateUtility.UTC_FORMATTER);
.atZone(ZoneId.systemDefault()).format(DateUtility.UTC_FORMATTER);
writeString(writer, fieldName, formattedTimestamp);
}
};
Expand Down Expand Up @@ -357,8 +358,11 @@ protected void writeTimeStamp(MapOrListWriterImpl writer, String fieldName, Docu
* @param reader document reader
*/
private void writeTimestampWithLocalZoneOffset(MapOrListWriterImpl writer, String fieldName, DocumentReader reader) {
long timestamp = reader.getTimestampLong() + DateUtility.TIMEZONE_OFFSET_MILLIS;
writer.timeStamp(fieldName).writeTimeStamp(timestamp);
Instant utcInstant = Instant.ofEpochMilli(reader.getTimestampLong());
ZonedDateTime localZonedDateTime = utcInstant.atZone(ZoneId.systemDefault());
ZonedDateTime convertedZonedDateTime = localZonedDateTime.withZoneSameLocal(ZoneId.of("UTC"));
long timeStamp = convertedZonedDateTime.toInstant().toEpochMilli();
writer.timeStamp(fieldName).writeTimeStamp(timeStamp);
}

@Override
Expand Down
Expand Up @@ -636,7 +636,6 @@ public class DateUtility {
public static final DateTimeFormatter isoFormatTime = buildFormatter("HH:mm:ss.SSSXX");

public static final DateTimeFormatter UTC_FORMATTER = buildFormatter("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
public static final long TIMEZONE_OFFSET_MILLIS = OffsetDateTime.now().getOffset().getTotalSeconds() * 1000;

public static DateTimeFormatter dateTimeTZFormat = null;
public static DateTimeFormatter timeFormat = null;
Expand Down

0 comments on commit 3772757

Please sign in to comment.