Пример #1
0
        public void Calculate()
        {
            var elapsedMonths = new DateDiff(Account.OpenDate, DateTime.Now).Months;

            // Check for early close of deposit
            if (Account.Type == AccountType.Deposit)
            {
                if (elapsedMonths < 12)
                {
                    Account.Balance += Account.Balance * (elapsedMonths / 12) * PenaltyRate;
                    Account.History.Add(new OperationHistory("PENALTY", true, DateTime.Now));
                }
                else
                {
                    Account.Balance += Account.Balance * (elapsedMonths / 12) * Rate;
                    Account.History.Add(new OperationHistory("NORMAL", true, DateTime.Now));
                }
            }
            else if (Account.Type == AccountType.Loan)
            {
                Account.Balance += Account.Balance * (elapsedMonths / 12) * Rate;
            }
            else
            {
                Account.Balance += Account.Balance * Rate;
            }

            Check();
        }
Пример #2
0
        public void DefaultsTest()
        {
            DateTime test = new DateTime( 2008, 10, 12, 15, 32, 44, 243 );

            DateDiff dateDiff = new DateDiff( test, test );

            Assert.AreEqual( dateDiff.YearBaseMonth, TimeSpec.CalendarYearStartMonth );
            Assert.AreEqual( dateDiff.FirstDayOfWeek, DateDiff.SafeCurrentInfo.FirstDayOfWeek );
        }
Пример #3
0
        }         // ToString

        // ----------------------------------------------------------------------
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            DateDiff comp = (DateDiff)obj;

            return(calendar == comp.calendar &&
                   yearBaseMonth == comp.yearBaseMonth &&
                   firstDayOfWeek == comp.firstDayOfWeek &&
                   date1 == comp.date1 &&
                   date2 == comp.date2 &&
                   difference == comp.difference);
        }         // Equals
Пример #4
0
        public void DaysTest()
        {
            DateTime date1 = new DateTime( 2008, 5, 14, 15, 32, 44, 243 );
            DateTime date2 = date1.AddDays( 1 );
            DateTime date3 = date1.AddDays( -1 );

            DateDiff dateDiff12 = new DateDiff( date1, date2 );
            Assert.AreEqual( dateDiff12.ElapsedYears, 0 );
            Assert.AreEqual( dateDiff12.ElapsedMonths, 0 );
            Assert.AreEqual( dateDiff12.ElapsedDays, 1 );
            Assert.AreEqual( dateDiff12.ElapsedHours, 0 );
            Assert.AreEqual( dateDiff12.ElapsedMinutes, 0 );
            Assert.AreEqual( dateDiff12.ElapsedSeconds, 0 );

            Assert.AreEqual( dateDiff12.Years, 0 );
            Assert.AreEqual( dateDiff12.Quarters, 0 );
            Assert.AreEqual( dateDiff12.Months, 0 );
            Assert.AreEqual( dateDiff12.Weeks, 0 );
            Assert.AreEqual( dateDiff12.Days, 1 );
            Assert.AreEqual( dateDiff12.Hours, TimeSpec.HoursPerDay );
            Assert.AreEqual( dateDiff12.Minutes, TimeSpec.HoursPerDay * TimeSpec.MinutesPerHour );
            Assert.AreEqual( dateDiff12.Seconds, TimeSpec.HoursPerDay * TimeSpec.MinutesPerHour * TimeSpec.SecondsPerMinute );

            DateDiff dateDiff13 = new DateDiff( date1, date3 );
            Assert.AreEqual( dateDiff13.ElapsedYears, 0 );
            Assert.AreEqual( dateDiff13.ElapsedMonths, 0 );
            Assert.AreEqual( dateDiff13.ElapsedDays, -1 );
            Assert.AreEqual( dateDiff13.ElapsedHours, 0 );
            Assert.AreEqual( dateDiff13.ElapsedMinutes, 0 );
            Assert.AreEqual( dateDiff13.ElapsedSeconds, 0 );

            Assert.AreEqual( dateDiff13.Years, 0 );
            Assert.AreEqual( dateDiff13.Quarters, 0 );
            Assert.AreEqual( dateDiff13.Months, 0 );
            Assert.AreEqual( dateDiff13.Weeks, 0 );
            Assert.AreEqual( dateDiff13.Days, -1 );
            Assert.AreEqual( dateDiff13.Hours, -TimeSpec.HoursPerDay );
            Assert.AreEqual( dateDiff13.Minutes, TimeSpec.HoursPerDay * TimeSpec.MinutesPerHour * -1 );
            Assert.AreEqual( dateDiff13.Seconds, TimeSpec.HoursPerDay * TimeSpec.MinutesPerHour * TimeSpec.SecondsPerMinute * -1 );
        }
Пример #5
0
        private void UpdatePerson3UI(DateDiff diff)
        {
            Person3YearsTextBlock.Text = diff.Years.ToString(CultureInfo.InvariantCulture);
            Person3MonthsTextBlock.Text = diff.Months.ToString(CultureInfo.InvariantCulture);
            Person3DaysTextBlock.Text = diff.Days.ToString(CultureInfo.InvariantCulture);
            Person3HoursTextBlock.Text = diff.Hours.ToString(CultureInfo.InvariantCulture);
            Person3MinutesTextBlock.Text = diff.Minutes.ToString(CultureInfo.InvariantCulture);
            Person3SecondsTextBlock.Text = diff.Seconds.ToString(CultureInfo.InvariantCulture);

            Person3ElapsedYearsTextBlock.Text = diff.ElapsedYears.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedMonthsTextBlock.Text = diff.ElapsedMonths.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedDaysTextBlock.Text = diff.ElapsedDays.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedHoursTextBlock.Text = diff.ElapsedHours.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedMinutesTextBlock.Text = diff.ElapsedMinutes.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedSecondsTextBlock.Text = diff.ElapsedSeconds.ToString(CultureInfo.InvariantCulture);
        }
Пример #6
0
        public void DateDiffSample()
        {
            DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
            Console.WriteLine( "Date1: {0}", date1 );
            // > Date1: 08.11.2009 07:13:59
            DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
            Console.WriteLine( "Date2: {0}", date2 );
            // > Date2: 20.03.2011 19:55:28

            DateDiff dateDiff = new DateDiff( date1, date2 );

            // differences
            Console.WriteLine( "DateDiff.Years: {0}", dateDiff.Years );
            // > DateDiff.Years: 1
            Console.WriteLine( "DateDiff.Quarters: {0}", dateDiff.Quarters );
            // > DateDiff.Quarters: 5
            Console.WriteLine( "DateDiff.Months: {0}", dateDiff.Months );
            // > DateDiff.Months: 16
            Console.WriteLine( "DateDiff.Weeks: {0}", dateDiff.Weeks );
            // > DateDiff.Weeks: 70
            Console.WriteLine( "DateDiff.Days: {0}", dateDiff.Days );
            // > DateDiff.Days: 497
            Console.WriteLine( "DateDiff.Weekdays: {0}", dateDiff.Weekdays );
            // > DateDiff.Weekdays: 71
            Console.WriteLine( "DateDiff.Hours: {0}", dateDiff.Hours );
            // > DateDiff.Hours: 11940
            Console.WriteLine( "DateDiff.Minutes: {0}", dateDiff.Minutes );
            // > DateDiff.Minutes: 716441
            Console.WriteLine( "DateDiff.Seconds: {0}", dateDiff.Seconds );
            // > DateDiff.Seconds: 42986489

            // elapsed
            Console.WriteLine( "DateDiff.ElapsedYears: {0}", dateDiff.ElapsedYears );
            // > DateDiff.ElapsedYears: 1
            Console.WriteLine( "DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths );
            // > DateDiff.ElapsedMonths: 4
            Console.WriteLine( "DateDiff.ElapsedDays: {0}", dateDiff.ElapsedDays );
            // > DateDiff.ElapsedDays: 12
            Console.WriteLine( "DateDiff.ElapsedHours: {0}", dateDiff.ElapsedHours );
            // > DateDiff.ElapsedHours: 12
            Console.WriteLine( "DateDiff.ElapsedMinutes: {0}", dateDiff.ElapsedMinutes );
            // > DateDiff.ElapsedMinutes: 41
            Console.WriteLine( "DateDiff.ElapsedSeconds: {0}", dateDiff.ElapsedSeconds );
            // > DateDiff.ElapsedSeconds: 29

            // description
            Console.WriteLine( "DateDiff.GetDescription(1): {0}", dateDiff.GetDescription( 1 ) );
            // > DateDiff.GetDescription(1): 1 Year
            Console.WriteLine( "DateDiff.GetDescription(2): {0}", dateDiff.GetDescription( 2 ) );
            // > DateDiff.GetDescription(2): 1 Year 4 Months
            Console.WriteLine( "DateDiff.GetDescription(3): {0}", dateDiff.GetDescription( 3 ) );
            // > DateDiff.GetDescription(3): 1 Year 4 Months 12 Days
            Console.WriteLine( "DateDiff.GetDescription(4): {0}", dateDiff.GetDescription( 4 ) );
            // > DateDiff.GetDescription(4): 1 Year 4 Months 12 Days 12 Hours
            Console.WriteLine( "DateDiff.GetDescription(5): {0}", dateDiff.GetDescription( 5 ) );
            // > DateDiff.GetDescription(5): 1 Year 4 Months 12 Days 12 Hours 41 Mins
            Console.WriteLine( "DateDiff.GetDescription(6): {0}", dateDiff.GetDescription( 6 ) );
            // > DateDiff.GetDescription(6): 1 Year 4 Months 12 Days 12 Hours 41 Mins 29 Secs
        }
Пример #7
0
        public void EmptyDateDiffTest()
        {
            DateTime test = new DateTime( 2008, 10, 12, 15, 32, 44, 243 );

            DateDiff dateDiff = new DateDiff( test, test );

            Assert.IsTrue( dateDiff.IsEmpty );
            Assert.AreEqual( dateDiff.Difference, TimeSpan.Zero );
            Assert.AreEqual( dateDiff.Years, 0 );
            Assert.AreEqual( dateDiff.Quarters, 0 );
            Assert.AreEqual( dateDiff.Months, 0 );
            Assert.AreEqual( dateDiff.Weeks, 0 );
            Assert.AreEqual( dateDiff.Days, 0 );
            Assert.AreEqual( dateDiff.Hours, 0 );
            Assert.AreEqual( dateDiff.Minutes, 0 );
            Assert.AreEqual( dateDiff.Seconds, 0 );

            Assert.AreEqual( dateDiff.ElapsedYears, 0 );
            Assert.AreEqual( dateDiff.ElapsedMonths, 0 );
            Assert.AreEqual( dateDiff.ElapsedDays, 0 );
            Assert.AreEqual( dateDiff.ElapsedHours, 0 );
            Assert.AreEqual( dateDiff.ElapsedMinutes, 0 );
            Assert.AreEqual( dateDiff.ElapsedSeconds, 0 );
        }
Пример #8
0
        public void DifferenceTest()
        {
            DateTime date1 = new DateTime( 2008, 10, 12, 15, 32, 44, 243 );
            DateTime date2 = new DateTime( 2010, 1, 3, 23, 22, 9, 345 );

            DateDiff dateDiff = new DateDiff( date1, date2 );

            Assert.AreEqual( dateDiff.Difference, date2.Subtract( date1 ) );
        }
Пример #9
0
        public void PositiveDurationTest()
        {
            DateTime date1 = new DateTime( 2008, 5, 14, 15, 32, 44, 243 );
            DateTime date2 = date1.AddYears( 1 ).AddMonths( 1 ).AddDays( 1 ).AddHours( 1 ).AddMinutes( 1 ).AddSeconds( 1 );

            DateDiff dateDiff = new DateDiff( date1, date2 );
            Assert.AreEqual( dateDiff.ElapsedYears, 1 );
            Assert.AreEqual( dateDiff.ElapsedMonths, 1 );
            Assert.AreEqual( dateDiff.ElapsedDays, 1 );
            Assert.AreEqual( dateDiff.ElapsedHours, 1 );
            Assert.AreEqual( dateDiff.ElapsedMinutes, 1 );
            Assert.AreEqual( dateDiff.ElapsedSeconds, 1 );
        }
Пример #10
0
        // ----------------------------------------------------------------------
        public bool IsWeekRecuringDay( DateTime start, DateTime test, int recuringInterval, params DayOfWeek[] weekDays )
        {
            if ( test < start || recuringInterval <= 0 )
            {
                return false;
            }

            bool isValidDayOfWeek = false;
            DayOfWeek testDayOfWeek = test.DayOfWeek;
            foreach ( DayOfWeek weekDay in weekDays )
            {
                if ( weekDay == testDayOfWeek )
                {
                    isValidDayOfWeek = true;
                    break;
                }
            }
            if ( !isValidDayOfWeek )
            {
                return false;
            }

            DateDiff dateDiff = new DateDiff( start, test );
            return ( dateDiff.Weeks % recuringInterval ) == 0;
        }
Пример #11
0
        // ----------------------------------------------------------------------
        public int CountWorkingDays( DateTime start, DateTime end, IList<DayOfWeek> workingDays )
        {
            if ( workingDays.Count == 0 )
            {
                return 0;
            }

            Week startWeek = new Week( start );
            Week endWeek = new Week( end );
            int dayCount = 0;

            // start week
            DateTime currentDay = start.Date;
            while ( currentDay < startWeek.End )
            {
                if ( workingDays.Contains( currentDay.DayOfWeek ) )
                {
                    dayCount++;
                }
                currentDay = currentDay.AddDays( 1 );
            }

            // between weeks
            DateDiff inBetweenWeekDiff = new DateDiff( startWeek.End, endWeek.Start );
            dayCount += inBetweenWeekDiff.Weeks * workingDays.Count;

            // end week
            currentDay = endWeek.Start.Date;
            while ( currentDay < end )
            {
                if ( workingDays.Contains( currentDay.DayOfWeek ) )
                {
                    dayCount++;
                }
                currentDay = currentDay.AddDays( 1 );
            }

            return dayCount;
        }