Skip to content

Commit 2f50854

Browse files
authored
Fixing DST bug in data_nanos support (elastic#1943) (elastic#1947)
In elastic#1803 we added support for the data_nanos Elasticsearch field type. However there is a bug in that sets the zone offset to the offset right now, as opposed to what the zone offset was at the time of the date. So for example if the date was 2015-01-01T06:10:30.123456789-06:00, then if you read that date after daylight saving had begun you would see it as 2015-01-01T06:10:30.123456789-05:00.
1 parent fafe9e4 commit 2f50854

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

mr/src/main/java/org/elasticsearch/hadoop/serialization/builder/JdkValueWriter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
import javax.xml.bind.DatatypeConverter;
2525
import java.sql.Timestamp;
26-
import java.time.LocalDateTime;
26+
import java.time.Instant;
2727
import java.time.OffsetDateTime;
28+
import java.time.ZoneId;
2829
import java.time.format.DateTimeFormatter;
2930
import java.util.Calendar;
3031
import java.util.Date;
@@ -131,8 +132,9 @@ else if (value instanceof Iterable) {
131132
}
132133
else if (value instanceof Timestamp) {
133134
Timestamp timestamp = (Timestamp) value;
134-
LocalDateTime localDateTime = timestamp.toLocalDateTime();
135-
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, OffsetDateTime.now().getOffset());
135+
long epochSeconds = timestamp.getTime() / 1000; // Getting rid of millisconds because they're captured in timestamp.getNanos()
136+
Instant instant = Instant.ofEpochSecond(epochSeconds, timestamp.getNanos());
137+
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
136138
generator.writeString(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime));
137139
}
138140
else if (value instanceof Date) {

0 commit comments

Comments
 (0)