Skip to content
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

Resolve DateTimeException for Unzoned Timestamps in parseDate #551

Merged
merged 1 commit into from
Feb 23, 2024
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 @@ -27,13 +27,13 @@

package org.apache.hc.client5.http.utils;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -145,6 +145,8 @@ public static LocalDateTime toUTC(final Date date) {

/**
* Parses the date value using the given date/time formats.
* <p>This method can handle strings without time-zone information by failing gracefully, in which case
* it returns {@code null}.</p>
*
* @param dateValue the instant value to parse
* @param dateFormatters the date/time formats to use
Expand All @@ -165,7 +167,7 @@ public static Instant parseDate(final String dateValue, final DateTimeFormatter.
for (final DateTimeFormatter dateFormatter : dateFormatters) {
try {
return Instant.from(dateFormatter.parse(v));
} catch (final DateTimeParseException ignore) {
} catch (final DateTimeException ignore) {
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testDateParseMessage() throws Exception {
@Test
public void testMalformedDate() {
Assertions.assertNull(DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", new DateTimeFormatter[] {}));
Assertions.assertNull(DateUtils.parseDate("Thu Feb 22 17:20:18 2024", new DateTimeFormatter[] {}));
}

@Test
Expand Down
Loading