This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 9e964a88ebb3249ec5a144d7cf2020eeea99f402 Author: Gary Gregory <[email protected]> AuthorDate: Sun Nov 15 10:22:33 2026 -0500 [LANG-1730] Parse GMT independently of platform aliases (#1874) Strengthen GMT parsing coverage and clarify time-zone strategy Javadoc. Test DateUtils lenient and strict GMT parsing with a nonzero default time zone. Verify both time-zone strategies consume longer GMT-prefixed display names and preserve their offsets. Replace stale skipTimeZone() documentation. Validation: mvn -B -ntp passed; mutation check confirmed prefix regressions are detected. Assisted-By: OpenAI Codex --- .../apache/commons/lang3/time/FastDateParser.java | 5 ++-- .../apache/commons/lang3/time/DateUtilsTest.java | 10 +++++++ .../commons/lang3/time/FastDateParserTest.java | 34 +++++++++++----------- .../time/FastDateParser_TimeZoneStrategyTest.java | 22 ++++++++++++++ 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java index a624d67c1..9301a5d9f 100644 --- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java +++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java @@ -569,9 +569,10 @@ static Strategy getIsoExtendedStrategy(final Locale locale) { } /** - * Tests whether to skip the given time zone, true if TimeZone.getTimeZone(). + * Tests whether to skip the GMT time zone when collecting display names. + * * <p> - * On Java 25 and up, skips short IDs if {@code ignoreTimeZoneShortIDs} is true. + * Bare GMT and GMT offsets are explicitly included in the parsing pattern and handled by {@link FastTimeZone#getGmtTimeZone(String)}. * </p> * <p> * This method is package private only for testing. diff --git a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java index e2943c102..96ae94a96 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -1193,6 +1193,16 @@ void testParseDate_Null() { assertNullPointerException(() -> DateUtils.parseDate(null, parsers)); } + @Test + @DefaultTimeZone("GMT+05:30") + void testParseDateGmt() throws ParseException { + final String source = "Mon, 01 May 2023 08:52:19 GMT"; + final String pattern = "EEE, dd MMM yyyy HH:mm:ss zzz"; + final Date expected = Date.from(Instant.parse("2023-05-01T08:52:19Z")); + assertEquals(expected, DateUtils.parseDate(source, Locale.US, pattern)); + assertEquals(expected, DateUtils.parseDateStrictly(source, Locale.US, pattern)); + } + @DefaultLocale(language = "en") @ParameterizedTest @CsvSource({"0, 0", "1, 1", "0000286, 286", "999, 999"}) diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java index b3aef46e9..7c46ac833 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java @@ -569,6 +569,23 @@ void testParseErrorMessageJapaneseImperial(final TriFunction<String, TimeZone, L assertTrue(message.contains("does not support dates before 1868-01-01."), message); } + @ParameterizedTest + @MethodSource(DATE_PARSER_PARAMETERS) + void testParseGmt(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider) throws ParseException { + final Date expected = Date.from(Instant.parse("2023-05-01T08:52:19Z")); + for (final String zonePattern : new String[] { "z", "zzz", "Z", "ZZZ" }) { + final DateParser parser = getInstance(dpProvider, "EEE, dd MMM yyyy HH:mm:ss " + zonePattern, NEW_YORK, Locale.US); + for (final String zoneName : new String[] { "GMT", "gmt", "GmT" }) { + final String source = "Mon, 01 May 2023 08:52:19 " + zoneName; + final ParsePosition position = new ParsePosition(0); + assertEquals(expected, parser.parse(source), source); + assertEquals(expected, parser.parse(source, position), source); + assertEquals(source.length(), position.getIndex(), source); + assertEquals(-1, position.getErrorIndex(), source); + } + } + } + @ParameterizedTest @CsvSource({ "Z, GMT+0:99", "Z, GMT-0:99", "Z, GMT+24:00", "Z, GMT-24:00", @@ -744,23 +761,6 @@ void testParseUnicodeTextLookupSuccess(final TriFunction<String, TimeZone, Local assertEquals(german.parse("01 Oktober 2024"), german.parse("01 O\u212atober 2024")); } - @ParameterizedTest - @MethodSource(DATE_PARSER_PARAMETERS) - void testParseGmt(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider) throws ParseException { - final Date expected = Date.from(Instant.parse("2023-05-01T08:52:19Z")); - for (final String zonePattern : new String[] { "z", "zzz", "Z", "ZZZ" }) { - final DateParser parser = getInstance(dpProvider, "EEE, dd MMM yyyy HH:mm:ss " + zonePattern, NEW_YORK, Locale.US); - for (final String zoneName : new String[] { "GMT", "gmt", "GmT" }) { - final String source = "Mon, 01 May 2023 08:52:19 " + zoneName; - final ParsePosition position = new ParsePosition(0); - assertEquals(expected, parser.parse(source), source); - assertEquals(expected, parser.parse(source, position), source); - assertEquals(source.length(), position.getIndex(), source); - assertEquals(-1, position.getErrorIndex(), source); - } - } - } - @ParameterizedTest @MethodSource(DATE_PARSER_PARAMETERS) void testParseValidTimeZoneOffsetBoundaries(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider) { diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParser_TimeZoneStrategyTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParser_TimeZoneStrategyTest.java index b1b83d2e8..3cba8a8a2 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateParser_TimeZoneStrategyTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateParser_TimeZoneStrategyTest.java @@ -95,6 +95,28 @@ private String[][] getZoneStringsSorted(final Locale locale) { return ArraySorter.sort(DateFormatSymbols.getInstance(locale).getZoneStrings(), Comparator.comparing(array -> array[0])); } + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void testGmtPrefixDoesNotTruncateDisplayName(final boolean iso8601Extended) { + final String zoneName = "GMT Example Time"; + final TimeZone timeZone = TimeZone.getTimeZone("GMT+05:30"); + final DateFormatSymbols symbols = new DateFormatSymbols(Locale.US); + symbols.setZoneStrings(new String[][] { { timeZone.getID(), zoneName, zoneName, zoneName, zoneName } }); + final FastDateParser.TimeZoneStrategy strategy; + try (MockedStatic<DateFormatSymbols> symbolFactory = mockStatic(DateFormatSymbols.class)) { + symbolFactory.when(() -> DateFormatSymbols.getInstance(Locale.US)).thenReturn(symbols); + strategy = new FastDateParser.TimeZoneStrategy(Locale.US, iso8601Extended); + } + final Calendar calendar = Calendar.getInstance(TimeZones.GMT, Locale.US); + calendar.clear(); + final ParsePosition position = new ParsePosition(0); + assertTrue(strategy.parse(null, calendar, zoneName, position, 0)); + assertEquals(zoneName.length(), position.getIndex()); + assertEquals(-1, position.getErrorIndex()); + assertEquals(timeZone.getRawOffset(), calendar.get(Calendar.ZONE_OFFSET)); + assertEquals(0, calendar.get(Calendar.DST_OFFSET)); + } + @ParameterizedTest @CsvSource({ "false, GMT", "false, gmt", "false, GmT", "true, GMT", "true, gmt", "true, GmT" }) void testGmtWithoutDisplayNameAlias(final boolean iso8601Extended, final String zoneName) {
