PaulStovell.TrialBalance.DomainModel.Period.CalculatePeriodForDate C# (CSharp) Method

CalculatePeriodForDate() public static method

Gets the accounting period for a given date.
public static CalculatePeriodForDate ( System.DateTime date, System.DateTime periodStartDate, PeriodLength periodLength ) : Period
date System.DateTime The date to find the accounting period for.
periodStartDate System.DateTime A date that an account period starts on.
periodLength PeriodLength The length of accounting periods.
return Period
        public static Period CalculatePeriodForDate(DateTime date, DateTime periodStartDate, PeriodLength periodLength)
        {
            Period result = new Period();

            DateTime lower =
                new DateTime(date.Year - 1, periodStartDate.Month, periodStartDate.Day).
                    Date.AddMonths(-1 * ((int)periodLength));
            DateTime upper = lower.AddMonths((int)periodLength).AddDays(-1).Date;

            while (date < lower || date > upper) {
                lower = upper.AddDays(1).Date;
                upper = lower.AddMonths((int)periodLength).AddDays(-1).Date;
            }

            result._startDate = lower.Date;
            result._length = periodLength;
            return result;
        }