Copilot commented on code in PR #1815:
URL: https://github.com/apache/commons-lang/pull/1815#discussion_r4272437038
##########
src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java:
##########
@@ -1591,6 +1598,38 @@ protected List<Rule> parsePattern() {
return rules;
}
+ /**
+ * Tests whether the month is the only field in the pattern.
+ * <p>
+ * {@link SimpleDateFormat} formats the text month of such a pattern with
the standalone month names, which in some languages, like the Slavic and Baltic
+ * ones, are a different grammatical form from the names used next to a
day ("{@code MMMM}" alone prints Russian February as nominative
+ * "{@code февраль}" where "{@code d MMMM}" prints genitive "{@code
февраля}").
+ * </p>
+ *
+ * @return Whether the pattern contains exactly one field, the month.
+ */
+ private boolean isMonthOnlyPattern() {
+ final int[] indexRef = new int[1];
+ boolean monthField = false;
+ for (int i = 0; i < pattern.length(); i++) {
+ indexRef[0] = i;
+ final String token = parseToken(pattern, indexRef);
+ i = indexRef[0];
+ if (token.isEmpty()) {
+ break;
+ }
+ final char c = token.charAt(0);
+ if (c == '\'') {
+ continue;
+ }
+ if (c != 'M' || monthField) {
+ return false;
+ }
Review Comment:
`isMonthOnlyPattern()` currently treats any unquoted literal (e.g., space,
hyphen, comma) as “another field” and returns `false`, even though
`SimpleDateFormat` treats non-letter characters as literals and still considers
patterns like `MMMM-` or `MMMM ` to be month-only. To match date-pattern
semantics, ignore tokens that start with non-letter characters (similar to how
quoted literals are ignored) so that both quoted and unquoted literals are
permitted in a month-only pattern.
##########
src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java:
##########
@@ -210,6 +214,36 @@ void testEraNonGregorianCalendar() {
}
}
+ /**
+ * Tests that a month-only pattern uses the standalone month names, like
SimpleDateFormat. In some languages, like the Slavic and Baltic ones, they are a
+ * different grammatical form from the names used next to a day: "MMMM"
alone prints Russian February as nominative "февраль" where "d MMMM" prints
+ * genitive "февраля".
+ */
+ @Test
+ void testStandaloneMonthForMonthOnlyPattern() throws ParseException {
+ // SimpleDateFormat forces the standalone form for a month-only
pattern only since Java 9 (JDK-8114833); Java 8 has no such rule.
+ assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
+ final Locale[] locales = { Locale.US, Locale.GERMANY, new
Locale("ru"), new Locale("cs"), new Locale("pl"), new Locale("fi") };
+ // Month-only patterns, with or without literals, use the standalone
form; patterns with another field keep the format form.
+ final String[] patterns = { "MMM", "MMMM", "'m:'MMMM", "MMMM''", "d
MMMM", "MMMM yyyy", "MMM MMM" };
Review Comment:
The test claims month-only patterns “with or without literals” use the
standalone form, but all literal examples here are quoted. Add at least one
month-only pattern containing an unquoted literal (e.g., `MMMM `, `MMMM-`, or
`MMMM,`) to prevent regressions and to validate the intended behavior once
`isMonthOnlyPattern()` is updated to ignore non-letter literal tokens.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]