Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public LocalInstant Add(LocalInstant localInstant, long value)
        {
            int currentYear = calculator.GetYear(localInstant);

            // Adjust argument range based on current year
            Preconditions.CheckArgumentRange("value", value, calculator.MinYear - currentYear, calculator.MaxYear - currentYear);
            // If we got this far, the conversion to int must be fine.
            int intValue = (int)value;

            return(calculator.SetYear(localInstant, intValue + currentYear));
        }
Ejemplo n.º 3
0
 internal int GetYear(LocalInstant localInstant)
 {
     return(yearMonthDayCalculator.GetYear(localInstant));
 }