Skip to content

Commit 97cfc20

Browse files
authored
Fix ingest timezone logic (#51215)
when a timezone is not provided Ingest logic should consider a time to be in a timezone provided as a parameter. When a timezone is provided Ingest should recalculate a time to the timezone provided as a parameter closes #51108
1 parent 10b7ffa commit 97cfc20

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ enum DateFormat {
4545
Iso8601 {
4646
@Override
4747
Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale) {
48-
return (date) -> DateFormatters.from(DateFormatter.forPattern("iso8601").parse(date)).withZoneSameInstant(timezone);
48+
return (date) -> DateFormatters.from(DateFormatter.forPattern("iso8601").parse(date), timezone)
49+
.withZoneSameInstant(timezone);
50+
4951
}
5052
},
5153
Unix {

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,19 @@ public void testParseUnixWithMsPrecision() {
8585
}
8686

8787
public void testParseISO8601() {
88-
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toInstant().toEpochMilli(),
89-
equalTo(978336000000L));
90-
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toString(),
91-
equalTo("2001-01-01T08:00Z"));
88+
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800")
89+
.toInstant().toEpochMilli(), equalTo(978336000000L));
9290
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toString(),
9391
equalTo("2001-01-01T08:00Z"));
9492
}
9593

94+
public void testParseWhenZoneNotPresentInText() {
95+
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00")
96+
.toInstant().toEpochMilli(), equalTo(978303600000L));
97+
assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00").toString(),
98+
equalTo("2001-01-01T00:00+01:00"));
99+
}
100+
96101
public void testParseISO8601Failure() {
97102
Function<String, ZonedDateTime> function = DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null);
98103
try {

modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/30_date_processor.yml

+53
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,59 @@ teardown:
4040
- match: { _source.date_source_field: "12/06/2010" }
4141
- match: { _source.date_target_field: "2010-06-12T00:00:00.000+02:00" }
4242

43+
44+
---
45+
"Test date processor timezone calculations":
46+
- do:
47+
ingest.put_pipeline:
48+
id: "my_pipeline_2"
49+
body: >
50+
{
51+
"description": "_description",
52+
"processors": [
53+
{
54+
"date" : {
55+
"field" : "date_source_field",
56+
"target_field" : "date_target_field",
57+
"formats" : ["ISO8601"],
58+
"timezone" : "+01:00"
59+
}
60+
}
61+
]
62+
}
63+
- match: { acknowledged: true }
64+
65+
66+
- do:
67+
index:
68+
index: test2
69+
id: 1
70+
pipeline: "my_pipeline_2"
71+
body: {date_source_field: "2010-06-01T00:00:00.000"}
72+
73+
- do:
74+
get:
75+
index: test2
76+
id: 1
77+
- match: { _source.date_source_field: "2010-06-01T00:00:00.000" }
78+
# date field without a timezone gets timezone from a pipeline
79+
- match: { _source.date_target_field: "2010-06-01T00:00:00.000+01:00" }
80+
81+
- do:
82+
index:
83+
index: test2
84+
id: 2
85+
pipeline: "my_pipeline_2"
86+
body: {date_source_field: "2010-06-01T00:00:00.000Z"}
87+
88+
- do:
89+
get:
90+
index: test2
91+
id: 2
92+
- match: { _source.date_source_field: "2010-06-01T00:00:00.000Z" }
93+
# date field with a timezone has its time recalculated to a target timezone from a pipeline
94+
- match: { _source.date_target_field: "2010-06-01T01:00:00.000+01:00" }
95+
4396
---
4497
"Test date processor with no timezone configured":
4598

server/src/main/java/org/elasticsearch/common/time/DateFormatters.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1839,13 +1839,17 @@ public class DateFormatters {
18391839
* @return The converted zoned date time
18401840
*/
18411841
public static ZonedDateTime from(TemporalAccessor accessor) {
1842+
return from(accessor, ZoneOffset.UTC);
1843+
}
1844+
1845+
public static ZonedDateTime from(TemporalAccessor accessor, ZoneId defaultZone) {
18421846
if (accessor instanceof ZonedDateTime) {
18431847
return (ZonedDateTime) accessor;
18441848
}
18451849

18461850
ZoneId zoneId = accessor.query(TemporalQueries.zone());
18471851
if (zoneId == null) {
1848-
zoneId = ZoneOffset.UTC;
1852+
zoneId = defaultZone;
18491853
}
18501854

18511855
LocalDate localDate = accessor.query(LOCAL_DATE_QUERY);

0 commit comments

Comments
 (0)