Skip to content

[7.16] Fix ingest timezone parsing backport(#63876) #79838

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 1 commit into from
Oct 27, 2021
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 @@ -78,14 +78,10 @@ private long parseMillis(String date) {

@Override
Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale locale) {
boolean isUtc = ZoneOffset.UTC.equals(zoneId);

DateFormatter dateFormatter = DateFormatter.forPattern(format)
.withLocale(locale);
// if UTC zone is set here, the time zone specified in the format will be ignored, leading to wrong dates
if (isUtc == false) {
dateFormatter = dateFormatter.withZone(zoneId);
}

final DateFormatter formatter = dateFormatter;
return text -> {
TemporalAccessor accessor = formatter.parse(text);
Expand All @@ -105,11 +101,9 @@ Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale
accessor = newTime.withZoneSameLocal(zoneId);
}

if (isUtc) {
return DateFormatters.from(accessor, locale).withZoneSameInstant(ZoneOffset.UTC);
} else {
return DateFormatters.from(accessor, locale);
}
return DateFormatters.from(accessor, locale, zoneId)
.withZoneSameInstant(zoneId);

};
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,52 @@ public void testParseWeekBasedWithLocale() {
assumeFalse("won't work in jdk8 " +
"because SPI mechanism is not looking at classpath - needs ISOCalendarDataProvider in jre's ext/libs",
JavaVersion.current().equals(JavaVersion.parse("8")));
String format = randomFrom("YYYY-ww");
String format = "YYYY-ww";
ZoneId timezone = DateUtils.of("Europe/Amsterdam");
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.US);
ZonedDateTime dateTime = javaFunction.apply("2020-33");
//33rd week of 2020 starts on 9th August 2020 as per US locale
assertThat(dateTime, equalTo(ZonedDateTime.of(2020,8,9,0,0,0,0,timezone)));
}

public void testNoTimezoneOnPatternAndOverride() {
{
String format = "yyyy-MM-dd'T'HH:mm";
ZoneId timezone = ZoneId.of("UTC");
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
// this means that hour will be 01:00 at UTC as timezone was not on a pattern, but provided as an ingest param
ZonedDateTime dateTime = javaFunction.apply("2020-01-01T01:00");
assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 01, 01, 01, 0, 0, 0, timezone)));
}
{
String format = "yyyy-MM-dd'T'HH:mm";
ZoneId timezone = ZoneId.of("-01:00");
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
// this means that hour will be 01:00 at -01:00 as timezone was not on a pattern, but provided as an ingest param
ZonedDateTime dateTime = javaFunction.apply("2020-01-01T01:00");
assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 01, 01, 01, 0, 0, 0, timezone)));
}
}

public void testTimezoneOnAPatternAndNonUTCOverride() {
String format = "yyyy-MM-dd'T'HH:mm XXX";
ZoneId timezone = ZoneId.of("-01:00");
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
// this means that hour will be 01:00 at -02:00 as timezone on a pattern. Converted to -01:00 as requested on ingest param

ZonedDateTime dateTime = javaFunction.apply("2020-01-01T01:00 -02:00");
assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 01, 01, 02, 0, 0, 0, timezone)));
}

public void testDefaultHourDefaultedToTimezoneOverride() {
String format = "yyyy-MM-dd";
ZoneId timezone = ZoneId.of("-01:00");
Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT);
// this means that hour will be 00:00 (default) at -01:00 as timezone was not on a pattern, but -01:00 was an ingest param
ZonedDateTime dateTime = javaFunction.apply("2020-01-01");
assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 01, 01, 0, 0, 0, 0, timezone)));
}

public void testParseUnixMs() {
assertThat(DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null).apply("1000500").toInstant().toEpochMilli(),
equalTo(1000500L));
Expand Down