Alwaysgaurav1 commented on PR #1780:
URL: https://github.com/apache/commons-lang/pull/1780#issuecomment-5507011770

   Thanks for catching this edge case, @garydgregory!
   
   ### Root Cause
   The issue occurred because `start.add(Calendar.YEAR, 1)` clamped 
`2020-02-29` to `2021-02-28`. Since the initial calculation for `days` had 
already subtracted `29` (`1 - 29 = -28`), adding the 28 days of February 2021 
yielded `-28 + 28 = 0` days instead of `1` day.
   
   ### Solution
   We don't need to change the normalization direction. Instead, we can unify 
the calculation with the rest of the token cascading hierarchy (`y -> M -> d -> 
H -> m -> s -> S`):
   
   1. Apply standard hierarchical borrowing for `days` and `months` first 
(identical to when `M` is present).
   2. If `y` is omitted, roll `years` into `months` (`months += 12 * years; 
years = 0;`).
   3. If `M` is omitted, roll remaining `months` into `days`:
      ```java
      if (!Token.containsTokenWithValue(tokens, M)) {
          while (months > 0) {
              days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
              months -= 1;
              start.add(Calendar.MONTH, 1);
          }
      }


-- 
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]

Reply via email to