You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ab...@apache.org on 2024/02/23 17:45:12 UTC

(httpcomponents-client) branch master updated: HTTPCLIENT-2319 / Updated the parseDate method to solely catch DateTimeException, streamlining error handling for date parsing and conversion. This change ensures comprehensive management of parsing issues, including those due to absent time-zone information, aligning with our documentation that the method should return null when conversion to Instant is not feasible. (#551)

This is an automated email from the ASF dual-hosted git repository.

abernal pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/master by this push:
     new f2b9a374d HTTPCLIENT-2319  / Updated the parseDate method to solely catch DateTimeException, streamlining error handling for date parsing and conversion. This change ensures comprehensive management of parsing issues, including those due to absent time-zone information, aligning with our documentation that the method should return null when conversion to Instant is not feasible. (#551)
f2b9a374d is described below

commit f2b9a374d817ee630a2efe706546ba4f1e5a5ac4
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Fri Feb 23 18:45:06 2024 +0100

    HTTPCLIENT-2319  / Updated the parseDate method to solely catch DateTimeException, streamlining error handling for date parsing and conversion. This change ensures comprehensive management of parsing issues, including those due to absent time-zone information, aligning with our documentation that the method should return null when conversion to Instant is not feasible. (#551)
---
 .../src/main/java/org/apache/hc/client5/http/utils/DateUtils.java   | 6 ++++--
 .../test/java/org/apache/hc/client5/http/utils/TestDateUtils.java   | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java
index 4fe5decd8..bb1f00adf 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/DateUtils.java
@@ -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;
@@ -145,6 +145,8 @@ public final class DateUtils {
 
     /**
      * 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
@@ -165,7 +167,7 @@ public final class DateUtils {
         for (final DateTimeFormatter dateFormatter : dateFormatters) {
             try {
                 return Instant.from(dateFormatter.parse(v));
-            } catch (final DateTimeParseException ignore) {
+            } catch (final DateTimeException ignore) {
             }
         }
         return null;
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java
index d3b9662ef..155068142 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java
@@ -84,6 +84,7 @@ public class TestDateUtils {
     @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