/// <summary> /// Create a time period based on a specified date. /// </summary> /// <param name="type">The type of the TimePeriod to create</param> /// <param name="date">Any date which falls into the desired period</param> /// <returns>New TimePeriod instance</returns> public static TimePeriod FromDate(TimePeriodType type, DateTime date) { DateTime val; TimePeriodType baseDateType = type; if (baseDateType == TimePeriodType.Day) { val = date.Date; } else if (baseDateType == TimePeriodType.CalendarQuarter) { int quarter = (date.Month - 1) / 3; int month = (quarter * 3) + 1; val = new DateTime(date.Year, month, 1); } else if (baseDateType == TimePeriodType.FiscalQuarter) { FiscalQuarter fq = new FiscalQuarter(); val = fq.GetStartDate(fq.GetQuarterForDate(date), date.Year); } else if (baseDateType == TimePeriodType.Month) { val = new DateTime(date.Year, date.Month, 1); } else if (baseDateType == TimePeriodType.Year) { val = new DateTime(date.Year, 1, 1); } else if (baseDateType == TimePeriodType.Week) { val = date; val.AddDays(-1 * (int)val.DayOfWeek); } else { throw new ApplicationException("Unknown TimePeriodType: " + type.ToString()); } return(new TimePeriod(type, val)); }
/// <summary> /// Returns the index in year of this TimePeriod /// </summary> /// <returns></returns> /// <remarks>The index is zero based.</remarks> public int GetIndexInYear() { int index = 0; if (periodType == TimePeriodType.Day) { index = periodStartDate.DayOfYear; } else if (periodType == TimePeriodType.Month) { index = periodStartDate.Month; } else if (periodType == TimePeriodType.CalendarQuarter) { index = ((periodStartDate.Month - 1) / 3) + 1; } else if (periodType == TimePeriodType.FiscalQuarter) { FiscalQuarter fq = new FiscalQuarter(); index = fq.GetQuarterForDate(periodStartDate); } return(index); }