Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dividend"></param>
        /// <param name="divisor"></param>
        /// <returns></returns>
        public static double Div(Period dividend, Period divisor)
        {
            if (HaveTheSamePeriodType(dividend, divisor))
            {
                double periodsInDividend = dividend.GetPeriodMultiplier();
                double periodsInDivisor  = divisor.GetPeriodMultiplier();

                return(periodsInDividend / periodsInDivisor);
            }
            // Y DIV M is the only supported div operation as of now.
            //
            if (!(dividend.period == PeriodEnum.Y & divisor.period == PeriodEnum.M))
            {
                throw new NotSupportedException();
            }

            double monthPeriodsInDividend = dividend.GetPeriodMultiplier() * GetMonthMultiplier(dividend.period);
            double monthPeriodsInDivisor  = divisor.GetPeriodMultiplier() * GetMonthMultiplier(divisor.period);

            return(monthPeriodsInDividend / monthPeriodsInDivisor);
        }
Example #2
0
        public static Period Multiply(this Period thisPeriod, int multiplier)
        {
            int periodMultiplierAsInt = thisPeriod.GetPeriodMultiplier();
            var result = new Period
            {
                period           = thisPeriod.period,
                periodMultiplier =
                    (multiplier * periodMultiplierAsInt).ToString(CultureInfo.InvariantCulture)
            };

            //Period result = BinarySerializerHelper.Clone(thisPeriod);//TODO there is a problem with the binary serializer!
            return(result);
        }
Example #3
0
        public static Period Sum(this Period thisPeriod, Period periodToAdd)
        {
            if (periodToAdd == null)
            {
                throw new ArgumentNullException("periodToAdd");
            }
            if (thisPeriod.period != periodToAdd.period)
            {
                string message = String.Format("Intervals must be of the same period type and they are not. Interval1 : {0}, interval2 : {1}", thisPeriod.ToString(), periodToAdd.ToString());
                throw new Exception(message);
            }
            string newPeriodMultiplier = (thisPeriod.GetPeriodMultiplier() + periodToAdd.GetPeriodMultiplier()).ToString(CultureInfo.InvariantCulture);
            var    sum
                = new Period
                {
                period           = thisPeriod.period,
                periodMultiplier = newPeriodMultiplier
                };

            return(sum);
        }
Example #4
0
        public static Period Subtract(this Period thisPeriod, Period periodToSubtract)
        {
            if (periodToSubtract == null)
            {
                throw new ArgumentNullException("periodToSubtract");
            }
            if (thisPeriod.period != periodToSubtract.period)
            {
                string message = String.Format("Periods must be of the same period type. This period '{0}', period to subtract '{1}'", thisPeriod.ToString(), periodToSubtract.ToString());
                throw new ArgumentException(message, "periodToSubtract");
            }

            double multiplier = thisPeriod.GetPeriodMultiplier() - periodToSubtract.GetPeriodMultiplier();
            var    difference
                = new Period
                {
                period           = thisPeriod.period,
                periodMultiplier = multiplier.ToString(CultureInfo.InvariantCulture)
                };

            return(difference);
        }
Example #5
0
        public static DateTime Add(this Period thisPeriod, DateTime dateTime)
        {
            int periodMultiplierAsInt = thisPeriod.GetPeriodMultiplier();

            switch (thisPeriod.period)
            {
            case PeriodEnum.D:
                return(Calendar.AddDays(dateTime, periodMultiplierAsInt));

            case PeriodEnum.W:
                return(Calendar.AddWeeks(dateTime, periodMultiplierAsInt));

            case PeriodEnum.M:
                return(Calendar.AddMonths(dateTime, periodMultiplierAsInt));

            case PeriodEnum.Y:
                return(Calendar.AddYears(dateTime, periodMultiplierAsInt));

            default:
                throw new ArgumentException(string.Format("PeriodEnum '{0}' is not supported in this function", thisPeriod.period));
            }
        }