Skip to content

Fix ingest timezone logic #51215

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 9 commits into from
Feb 3, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ enum DateFormat {
Iso8601 {
@Override
Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale) {
return (date) -> DateFormatters.from(DateFormatter.forPattern("iso8601").parse(date)).withZoneSameInstant(timezone);
return (date) -> DateFormatters.from(DateFormatter.forPattern("iso8601").parse(date), timezone)
.withZoneSameInstant(timezone);

}
},
Unix {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,19 @@ public void testParseUnixWithMsPrecision() {
}

public void testParseISO8601() {
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toInstant().toEpochMilli(),
equalTo(978336000000L));
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toString(),
equalTo("2001-01-01T08:00Z"));
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800")
.toInstant().toEpochMilli(), equalTo(978336000000L));
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toString(),
equalTo("2001-01-01T08:00Z"));
}

public void testParseWhenZoneNotPresentInText() {
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00")
.toInstant().toEpochMilli(), equalTo(978303600000L));
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00").toString(),
equalTo("2001-01-01T00:00+01:00"));
}

public void testParseISO8601Failure() {
Function<String, ZonedDateTime> function = DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,59 @@ teardown:
- match: { _source.date_source_field: "12/06/2010" }
- match: { _source.date_target_field: "2010-06-12T00:00:00.000+02:00" }


---
"Test date processor timezone calculations":
- do:
ingest.put_pipeline:
id: "my_pipeline_2"
body: >
{
"description": "_description",
"processors": [
{
"date" : {
"field" : "date_source_field",
"target_field" : "date_target_field",
"formats" : ["ISO8601"],
"timezone" : "+01:00"
}
}
]
}
- match: { acknowledged: true }


- do:
index:
index: test2
id: 1
pipeline: "my_pipeline_2"
body: {date_source_field: "2010-06-01T00:00:00.000"}

- do:
get:
index: test2
id: 1
- match: { _source.date_source_field: "2010-06-01T00:00:00.000" }
# date field without a timezone gets timezone from a pipeline
- match: { _source.date_target_field: "2010-06-01T00:00:00.000+01:00" }

- do:
index:
index: test2
id: 2
pipeline: "my_pipeline_2"
body: {date_source_field: "2010-06-01T00:00:00.000Z"}

- do:
get:
index: test2
id: 2
- match: { _source.date_source_field: "2010-06-01T00:00:00.000Z" }
# date field with a timezone has its time recalculated to a target timezone from a pipeline
- match: { _source.date_target_field: "2010-06-01T01:00:00.000+01:00" }

---
"Test date processor with no timezone configured":

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1839,13 +1839,17 @@ public class DateFormatters {
* @return The converted zoned date time
*/
public static ZonedDateTime from(TemporalAccessor accessor) {
return from(accessor, ZoneOffset.UTC);
}

public static ZonedDateTime from(TemporalAccessor accessor, ZoneId defaultZone) {
if (accessor instanceof ZonedDateTime) {
return (ZonedDateTime) accessor;
}

ZoneId zoneId = accessor.query(TemporalQueries.zone());
if (zoneId == null) {
zoneId = ZoneOffset.UTC;
zoneId = defaultZone;
}

LocalDate localDate = accessor.query(LOCAL_DATE_QUERY);
Expand Down