sebbASF commented on code in PR #408:
URL: https://github.com/apache/commons-validator/pull/408#discussion_r3484177309
##########
src/main/java/org/apache/commons/validator/routines/AbstractCalendarValidator.java:
##########
@@ -416,4 +421,38 @@ protected Object parse(String value, final String pattern,
final Locale locale,
*/
@Override
protected abstract Object processParsedValue(Object value, Format
formatter);
+
+ /**
+ * Compares the week two calendars fall in, ordering by the actual week
rather than by the
+ * {@code WEEK_OF_YEAR} or {@code WEEK_OF_MONTH} number alone. Those
numbers repeat across the
+ * boundaries they reset on (for example 31 December may be week 1 of the
following year, and
+ * the first week of a month can hold days carried over from the previous
month), so the day
+ * distance is checked first: dates a week or more apart are always in
different weeks, and
+ * nearer dates share a week only when the week number also matches.
+ *
+ * @param value The Calendar value.
+ * @param compare The {@link Calendar} to check the value against.
+ * @param field {@code Calendar.WEEK_OF_YEAR} or {@code
Calendar.WEEK_OF_MONTH}.
+ * @return Zero if both calendars are in the same week, -1 or +1 otherwise.
+ */
+ private int compareWeek(final Calendar value, final Calendar compare,
final int field) {
+ final long days = epochDay(value) - epochDay(compare);
+ if (Math.abs(days) >= DAYS_PER_WEEK || calculateCompareResult(value,
compare, field) != 0) {
+ return Long.signum(days);
+ }
+ return 0;
+ }
+
+ /**
+ * Returns the number of whole days from the epoch to the calendar's local
date, so two
+ * calendars can be compared by date regardless of the time of day.
+ *
+ * @param calendar The calendar to convert.
+ * @return the day count from the epoch in the calendar's own time zone.
+ */
+ private static long epochDay(final Calendar calendar) {
+ final long localMillis = calendar.getTimeInMillis()
Review Comment:
getTimeInMillis returns the time as UTC milliseconds from the epoch, so uses
the same base for both calendars.
Rather than calculating the day that this represents, it would be simpler to
see if the times are more than a week of milliseconds apart.
--
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]