Skip to content
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 @@ -34,6 +34,7 @@
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -73,36 +74,55 @@ public final class DateUtils {

/**
* Date format pattern used to parse HTTP date headers in RFC 1036 format.
*
* @deprecated use {@link #FORMATTER_RFC1036}. This pattern reads a two-digit year
* from 70 to 99 as 2070 to 2099.
*/
@Deprecated
public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";

/**
* Date formatter used to parse HTTP date headers in RFC 1036 format.
* <p>
* A two-digit year from 70 to 99 is read as 1970 to 1999, and a two-digit
* year from 00 to 69 as 2000 to 2069, as RFC 6265 section 5.1.1 requires.
* </p>
*
* @since 5.2
*/
public static final DateTimeFormatter FORMATTER_RFC1036 = new DateTimeFormatterBuilder()
.parseLenient()
.parseCaseInsensitive()
.appendPattern(PATTERN_RFC1036)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shinseongsu PATTERN_RFC1036 is now no longer being used other than in javadocs, which feels wrong. Could you please get rid of it entirely?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ok2c Removed. PATTERN_ASCTIME was also unused after this change, so I removed that one too. japicmp fails on the removed fields, so I added both to the excludes in the root pom.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

japicmp fails on the removed fields, so I added both to the excludes in the root pom.

@shinseongsu Ah, damn. Those static variables are public. This is my bad. Please keep them but deprecate instead. Sorry about that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ok2c No worries. Put both back and marked them @Deprecated, pointing to FORMATTER_RFC1036 / FORMATTER_ASCTIME, and dropped the japicmp excludes. japicmp now only reports the new annotation on those two fields.

.appendPattern("EEE, dd-MMM-")
.appendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2, 1970)
.appendPattern(" HH:mm:ss zzz")
.toFormatter(Locale.ENGLISH);

/**
* Date format pattern used to parse HTTP date headers in ANSI C
* {@code asctime()} format.
*
* @deprecated use {@link #FORMATTER_ASCTIME}. This pattern has no time zone
* and does not accept a space-padded day of month.
*/
@Deprecated
public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";

/**
* Date formatter used to parse HTTP date headers in in ANSI C {@code asctime()} format.
* Date formatter used to parse HTTP date headers in ANSI C {@code asctime()} format.
* <p>
* This format has no time zone. Values are read as UTC, as RFC 9110 section 5.6.7 requires.
* A day of month below 10 may be padded with a space ({@code Nov 6}).
* </p>
*
* @since 5.2
*/
public static final DateTimeFormatter FORMATTER_ASCTIME = new DateTimeFormatterBuilder()
.parseLenient()
.parseCaseInsensitive()
.appendPattern(PATTERN_ASCTIME)
.toFormatter(Locale.ENGLISH);
.appendPattern("EEE MMM ppd HH:mm:ss yyyy")
.toFormatter(Locale.ENGLISH)
.withZone(ZoneOffset.UTC);

/**
* Standard date formatters: {@link #FORMATTER_RFC1123}, {@link #FORMATTER_RFC1036}, {@link #FORMATTER_ASCTIME}.
Expand Down Expand Up @@ -192,8 +212,8 @@ public static Instant parseDate(final CharSequence dateValue, final DateTimeForm
}

/**
* Parses the instant value using the standard date/time formats ({@link #PATTERN_RFC1123},
* {@link #PATTERN_RFC1036}, {@link #PATTERN_ASCTIME}).
* Parses the instant value using the standard date/time formats ({@link #FORMATTER_RFC1123},
* {@link #FORMATTER_RFC1036}, {@link #FORMATTER_ASCTIME}).
*
* @param dateValue the instant value to parse
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ void testTwoDigitYearDateParse() {
DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
}

@Test
void testTwoDigitYearBefore2000DateParse() {
Assertions.assertEquals(createInstant(1970, Month.JANUARY, 1),
DateUtils.parseDate("Thursday, 01-Jan-70 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
Assertions.assertEquals(createInstant(1994, Month.NOVEMBER, 6).plus(8, ChronoUnit.HOURS)
.plus(49, ChronoUnit.MINUTES).plus(37, ChronoUnit.SECONDS),
DateUtils.parseStandardDate("Sunday, 06-Nov-94 08:49:37 GMT"));
Assertions.assertEquals(createInstant(2069, Month.DECEMBER, 31),
DateUtils.parseStandardDate("Tuesday, 31-Dec-69 00:00:00 GMT"));
Assertions.assertEquals(createInstant(1999, Month.DECEMBER, 31),
DateUtils.parseStandardDate("Friday, 31-Dec-99 00:00:00 GMT"));
Assertions.assertEquals(createInstant(2000, Month.JANUARY, 1),
DateUtils.parseStandardDate("Saturday, 01-Jan-00 00:00:00 GMT"));
}

@Test
void testAsctimeDateParse() {
Assertions.assertEquals(createInstant(1994, Month.NOVEMBER, 6).plus(8, ChronoUnit.HOURS)
.plus(49, ChronoUnit.MINUTES).plus(37, ChronoUnit.SECONDS),
DateUtils.parseDate("Sun Nov 6 08:49:37 1994", DateUtils.FORMATTER_ASCTIME));
Assertions.assertEquals(createInstant(1994, Month.NOVEMBER, 6).plus(8, ChronoUnit.HOURS)
.plus(49, ChronoUnit.MINUTES).plus(37, ChronoUnit.SECONDS),
DateUtils.parseDate("Sun Nov 6 08:49:37 1994", DateUtils.FORMATTER_ASCTIME));
Assertions.assertEquals(createInstant(2024, Month.FEBRUARY, 22).plus(17, ChronoUnit.HOURS)
.plus(20, ChronoUnit.MINUTES).plus(18, ChronoUnit.SECONDS),
DateUtils.parseStandardDate("Thu Feb 22 17:20:18 2024"));
}

@Test
void testParseQuotedDate() {
Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14),
Expand All @@ -133,6 +161,8 @@ void testBasicDateFormat() {
Assertions.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1123));
Assertions.assertEquals("Fri, 14-Oct-05 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1036));
Assertions.assertEquals("Fri Oct 14 00:00:00 2005", DateUtils.formatDate(instant, DateUtils.FORMATTER_ASCTIME));
Assertions.assertEquals("Sun Nov 6 00:00:00 1994",
DateUtils.formatDate(createInstant(1994, Month.NOVEMBER, 6), DateUtils.FORMATTER_ASCTIME));
}

}
Loading