예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="thisPeriod"></param>
        /// <param name="multiplier"></param>
        /// <returns></returns>
        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);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="thisPeriod"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        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($"PeriodEnum '{thisPeriod.period}' is not supported in this function");
            }
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="thisPeriod"></param>
        /// <param name="periodToSubtract"></param>
        /// <returns></returns>
        public static Period Subtract(this Period thisPeriod, Period periodToSubtract)
        {
            if (periodToSubtract == null)
            {
                throw new ArgumentNullException(nameof(periodToSubtract));
            }
            if (thisPeriod.period != periodToSubtract.period)
            {
                string message =
                    $"Periods must be of the same period type. This period '{thisPeriod}', period to subtract '{periodToSubtract}'";
                throw new ArgumentException(message, nameof(periodToSubtract));
            }
            double multiplier = thisPeriod.GetPeriodMultiplier() - periodToSubtract.GetPeriodMultiplier();
            var    difference
                = new Period
                {
                period           = thisPeriod.period,
                periodMultiplier = multiplier.ToString(CultureInfo.InvariantCulture)
                };

            return(difference);
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="thisPeriod"></param>
        /// <param name="periodToAdd"></param>
        /// <returns></returns>
        public static Period Sum(this Period thisPeriod, Period periodToAdd)
        {
            if (periodToAdd == null)
            {
                throw new ArgumentNullException(nameof(periodToAdd));
            }
            if (thisPeriod.period != periodToAdd.period)
            {
                string message =
                    $"Intervals must be of the same period type and they are not. Interval1 : {thisPeriod}, interval2 : {periodToAdd}";
                throw new System.Exception(message);
            }
            string newPeriodMultiplier = (thisPeriod.GetPeriodMultiplier() + periodToAdd.GetPeriodMultiplier()).ToString(CultureInfo.InvariantCulture);
            var    sum
                = new Period
                {
                period           = thisPeriod.period,
                periodMultiplier = newPeriodMultiplier
                };

            return(sum);
        }