HandCoded.Finance.Interval.Plus C# (CSharp) Méthode

Plus() public méthode

Calculates the result of adding another Interval to this one.
If the two time periods /// cannot be combined.
public Plus ( Interval other ) : Interval
other Interval The Interval to add.
Résultat Interval
        public Interval Plus(Interval other)
        {
            // One of the Intervals is zero length?
            if (multiplier == 0) return (other);
            if (other.multiplier == 0) return (this);

            // Both Intervals have the same unit?
            if (period == other.period)
                return (new Interval (multiplier + other.multiplier, period));

            // Otherwise check for equivalences
            if ((period == Period.YEAR) && (other.period == Period.MONTH))
                return (new Interval (12 * multiplier + other.multiplier, Period.MONTH));
            if ((period == Period.MONTH) && (other.period == Period.YEAR))
                return (new Interval (multiplier + 12 * other.multiplier, Period.MONTH));
            if ((period == Period.WEEK) && (other.period == Period.DAY))
                return (new Interval (7 * multiplier + other.multiplier, Period.DAY));
            if ((period == Period.DAY) && (other.period == Period.WEEK))
                return (new Interval (multiplier + 7 * other.multiplier, Period.DAY));

            throw new ArgumentException ("Intervals cannot be combined");
        }

Usage Example

        /// <summary>
        /// Tests if the payment date falls on a calculated date.
        /// </summary>
        /// <param name="paymentDate">The payment date.</param>
        /// <param name="startDate">The calculation period start date.</param>
        /// <param name="endDate">The calculation perion end date.</param>
        /// <param name="freq">	The period frequency.</param>
        /// <returns><c>true</c> if the payment date falls on a calculated date.</returns>
        private static bool IsUnadjustedCalculationPeriodDate(Date paymentDate, Date startDate, Date endDate, Interval freq)
        {
            Interval	step = new Interval (0, Period.DAY);

            for (;;) {
                Date		targetDate = startDate.Plus (step);

                if (targetDate.CompareTo (endDate) > 0) return (false);
                if (targetDate.Equals (paymentDate)) return (true);

                step = step.Plus (freq);
            }
        }