Copilot commented on code in PR #1800:
URL: https://github.com/apache/commons-lang/pull/1800#discussion_r4086145278
##########
src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java:
##########
@@ -1285,6 +1288,35 @@ public StringBuffer format(final Object obj, final
StringBuffer toAppendTo, fina
throw new IllegalArgumentException("Unknown class: " +
ClassUtils.getName(obj, "<null>"));
}
+ /**
+ * Gets the era names for the calendar this printer formats with.
+ * <p>
+ * {@link DateFormatSymbols#getEras()} only knows the two Gregorian eras.
A locale whose default calendar is not Gregorian, like the Thai Buddhist or
+ * Japanese Imperial calendar, prints that calendar's year, so the era
names have to come from that calendar as well, as {@link SimpleDateFormat} and
+ * {@link FastDateParser} do. The Japanese Imperial calendar also has more
than two eras, so indexing the Gregorian names by its era value would throw an
+ * {@link ArrayIndexOutOfBoundsException}.
+ * </p>
+ *
+ * @param symbols the date format symbols for this printer's locale.
+ * @param style {@link Calendar#SHORT} or {@link Calendar#LONG}.
+ * @return the era names, indexed by the {@link Calendar#ERA} value.
+ */
+ private String[] getEras(final DateFormatSymbols symbols, final int style)
{
+ final Calendar calendar = newCalendar();
+ // The Buddhist calendar extends GregorianCalendar, so test the
calendar type rather than the class.
+ if ("gregory".equals(calendar.getCalendarType())) {
+ return symbols.getEras();
+ }
+ final Map<String, Integer> displayNames =
calendar.getDisplayNames(Calendar.ERA, style, locale);
+ if (displayNames == null) {
+ return symbols.getEras();
+ }
+ final String[] eras = new String[calendar.getMaximum(Calendar.ERA) +
1];
+ Arrays.fill(eras, StringUtils.EMPTY);
+ displayNames.forEach((name, era) -> eras[era] = name);
Review Comment:
This table is chosen from the printer's locale calendar, but
`format(Calendar)` applies the rules to the caller's Calendar unchanged (see
`format(Calendar, Appendable)`). For example, formatting a Gregorian Calendar
with a `ja_JP_JP` printer gives ERA=1, which this array interprets as the
Japanese Meiji era instead of Gregorian AD, producing an incorrect era label
(and other calendar-type combinations can misindex it). Resolve the era name
from the actual calendar used by the rule, or convert the Calendar overload to
the printer's calendar type, and add a regression test for mixed calendar types.
--
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]