public long Subtract(LocalInstant minuendInstant, LocalInstant subtrahendInstant) { int minuendYear = calculator.GetYear(minuendInstant); int subtrahendYear = calculator.GetYear(subtrahendInstant); int minuendMonth = calculator.GetMonthOfYear(minuendInstant); int subtrahendMonth = calculator.GetMonthOfYear(subtrahendInstant); int diff = (minuendYear - subtrahendYear) * calculator.MonthsInYear + minuendMonth - subtrahendMonth; // If we just add the difference in months to subtrahendInstant, what do we get? LocalInstant simpleAddition = Add(subtrahendInstant, diff); if (subtrahendInstant <= minuendInstant) { // Moving forward: if the result of the simple addition is before or equal to the minuend, // we're done. Otherwise, rewind a month because we've overshot. return(simpleAddition <= minuendInstant ? diff : diff - 1); } else { // Moving backward: if the result of the simple addition (of a non-positive number) // is after or equal to the minuend, we're done. Otherwise, increment by a month because // we've overshot backwards. return(simpleAddition >= minuendInstant ? diff : diff + 1); } }
internal int GetMonthOfYear(LocalInstant localInstant) { return(yearMonthDayCalculator.GetMonthOfYear(localInstant)); }