protected override bool OnVisitYear(YearRange year, CalendarPeriodCollectorContext context) { if (IsDebugEnabled) { log.Debug("Visit Year... year=[{0}]", year); } if (context.Scope != CalendarPeriodCollectorContext.CollectKind.Month) { return(true); // continue } // all month if (Filter.CollectingMonths.Count == 0) { var months = year.GetMonths().Where(m => IsMatchingMonth(m, context) && CheckLimits(m)); _periods.AddAll(months.Cast <ITimePeriod>().AsEnumerable()); } else { var months = Filter.CollectingMonths .Select(m => { if (m.IsSingleMonth) { var month = new MonthRange(year.YearValue, m.Min, year.TimeCalendar); if (IsMatchingMonth(month, context) && CheckLimits(month)) { return((ITimePeriod)month); } } else { var monthRanges = new MonthRangeCollection(year.YearValue, m.Min, m.Max - m.Min, year.TimeCalendar); var isMatching = monthRanges.GetMonths().All(month => IsMatchingMonth(month, context)); if (isMatching && CheckLimits(monthRanges)) { return((ITimePeriod)monthRanges); } } return((ITimePeriod)null); }) .Where(m => m != null); _periods.AddAll(months.Cast <ITimePeriod>().AsEnumerable()); } return(false); // abort }
public void FiscalYearTest() { var testDate = new DateTime(2008, 11, 18); var year = new YearRange(testDate, TimeCalendar.New(October)); year.YearBaseMonth.Should().Be(October); year.BaseYear.Should().Be(testDate.Year); // Start & End year.Start.Year.Should().Be(testDate.Year); year.Start.Month.Should().Be(October); year.Start.Day.Should().Be(1); year.End.Year.Should().Be(testDate.Year + 1); year.End.Month.Should().Be(September); year.End.Day.Should().Be(CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(year.End.Year, year.End.Month)); // Half Years var halfyears = year.GetHalfyears(); foreach (var halfyear in year.GetHalfyears()) { if (halfyear.Halfyear == HalfyearKind.First) { halfyear.Start.Should().Be(year.Start); halfyear.Start.Year.Should().Be(testDate.Year); halfyear.Start.Month.Should().Be(October); halfyear.Start.Day.Should().Be(1); halfyear.End.Year.Should().Be(testDate.Year + 1); halfyear.End.Month.Should().Be(March); halfyear.End.Day.Should().Be(CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(halfyear.End.Year, halfyear.End.Month)); } else { halfyear.Start.Should().Be(year.Start.AddMonths(TimeSpec.MonthsPerHalfyear)); halfyear.Start.Year.Should().Be(testDate.Year + 1); halfyear.Start.Month.Should().Be(April); halfyear.Start.Day.Should().Be(1); halfyear.End.Year.Should().Be(testDate.Year + 1); halfyear.End.Month.Should().Be(September); halfyear.End.Day.Should().Be(CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(halfyear.End.Year, halfyear.End.Month)); } } // Quarters foreach (var quarter in year.GetQuarters()) { switch (quarter.Quarter) { case QuarterKind.First: quarter.Start.Should().Be(year.Start); quarter.Start.Year.Should().Be(testDate.Year); quarter.Start.Month.Should().Be(October); quarter.Start.Day.Should().Be(1); quarter.End.Year.Should().Be(testDate.Year); quarter.End.Month.Should().Be(December); quarter.End.Day.Should().Be(quarter.TimeCalendar.GetDaysInMonth(quarter.End.Year, quarter.End.Month)); break; case QuarterKind.Second: quarter.Start.Year.Should().Be(testDate.Year + 1); quarter.Start.Month.Should().Be(January); quarter.Start.Day.Should().Be(1); quarter.End.Year.Should().Be(testDate.Year + 1); quarter.End.Month.Should().Be(March); quarter.End.Day.Should().Be(quarter.TimeCalendar.GetDaysInMonth(quarter.End.Year, quarter.End.Month)); break; case QuarterKind.Third: quarter.Start.Year.Should().Be(testDate.Year + 1); quarter.Start.Month.Should().Be(April); quarter.Start.Day.Should().Be(1); quarter.End.Year.Should().Be(testDate.Year + 1); quarter.End.Month.Should().Be(June); quarter.End.Day.Should().Be(quarter.TimeCalendar.GetDaysInMonth(quarter.End.Year, quarter.End.Month)); break; case QuarterKind.Fourth: quarter.End.Should().Be(year.End); quarter.Start.Year.Should().Be(testDate.Year + 1); quarter.Start.Month.Should().Be(July); quarter.Start.Day.Should().Be(1); quarter.End.Year.Should().Be(testDate.Year + 1); quarter.End.Month.Should().Be(September); quarter.End.Day.Should().Be(quarter.TimeCalendar.GetDaysInMonth(quarter.End.Year, quarter.End.Month)); break; } } // months // var monthIndex = 0; foreach (var month in year.GetMonths()) { if (monthIndex == 0) { Assert.AreEqual(month.Start, year.Start); } else if (monthIndex == TimeSpec.MonthsPerYear - 1) { Assert.AreEqual(month.End, year.End); } var startDate = new DateTime(year.BaseYear, year.Start.Month, 1).AddMonths(monthIndex); month.Start.Year.Should().Be(startDate.Year); month.Start.Month.Should().Be(startDate.Month); month.Start.Day.Should().Be(startDate.Day); month.End.Year.Should().Be(startDate.Year); month.End.Month.Should().Be(startDate.Month); monthIndex++; } }