} // EndWeekOfYearName // ---------------------------------------------------------------------- public DateTime GetStartOfWeek(int weekIndex) { if (weekIndex < 0 || weekIndex >= weekCount) { throw new ArgumentOutOfRangeException("weekIndex"); } DateTime startDate = TimeTool.GetStartOfYearWeek(year, startWeek, Calendar.Culture, Calendar.YearWeekType); return(startDate.AddDays(weekIndex * TimeSpec.DaysPerWeek)); } // GetStartOfWeek
} // EndWeekOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = TimeTool.GetStartOfYearWeek(year, startWeek, Calendar.Culture, Calendar.YearWeekType); int dayCount = weekCount * TimeSpec.DaysPerWeek; for (int i = 0; i < dayCount; i++) { days.Add(new Day(startDate.AddDays(i), Calendar)); } return(days); } // GetDays
} // GetPeriodOf // ---------------------------------------------------------------------- private static TimeRange GetPeriodOf(int year, int weekOfYear, int weekCount, ITimeCalendar calendar) { if (weekCount < 1) { throw new ArgumentOutOfRangeException("weekCount"); } DateTime start = TimeTool.GetStartOfYearWeek(year, weekOfYear, calendar.Culture, calendar.YearWeekType); DateTime end = start.AddDays(weekCount * TimeSpec.DaysPerWeek); return(new TimeRange(start, end)); } // GetPeriodOf
} // GetStartOfWeek // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = TimeTool.GetStartOfYearWeek(year, startWeek, Calendar.Culture, Calendar.YearWeekType); int dayCount = weekCount * TimeSpec.DaysPerWeek; foreach (var i in Enumerable.Range(0, dayCount)) { days.Add(new Day(startDate.AddDays(i), Calendar)); } return(days); } // GetDays
} // IsSameQuarter // ---------------------------------------------------------------------- public static bool IsSameQuarter(YearMonth yearStartMonth, DateTime left, DateTime right) { int leftYear = TimeTool.GetYearOf(yearStartMonth, left); int rightYear = TimeTool.GetYearOf(yearStartMonth, right); if (leftYear != rightYear) { return(false); } return(TimeTool.GetQuarterOfMonth(yearStartMonth, (YearMonth)left.Month) == TimeTool.GetQuarterOfMonth(yearStartMonth, (YearMonth)right.Month)); } // IsSameQuarter
} // Halfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, HalfyearCount)) { int year; YearHalfyear halfyear; TimeTool.AddHalfyear(BaseYear, StartHalfyear, i, out year, out halfyear); halfyears.Add(new Halfyear(year, halfyear, Calendar)); } return(halfyears); } // GetHalfyears
} // Quarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, QuarterCount)) { int year; YearQuarter quarter; TimeTool.AddQuarter(BaseYear, StartQuarter, i, out year, out quarter); quarters.Add(new Quarter(year, quarter, Calendar)); } return(quarters); } // GetQuarters
} // Months // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); for (int i = 0; i < MonthCount; i++) { int year; YearMonth month; TimeTool.AddMonth(StartYear, StartMonth, i, out year, out month); months.Add(new Month(year, month, Calendar)); } return(months); } // GetMonths
} // Quarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); for (int i = 0; i < QuarterCount; i++) { int year; YearQuarter quarter; TimeTool.AddQuarter(BaseYear, StartQuarter, i, out year, out quarter); quarters.Add(new Quarter(year, quarter, Calendar)); } return(quarters); } // GetQuarters
} // Months // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, MonthCount)) { int year; YearMonth month; TimeTool.AddMonth(StartYear, StartMonth, i, out year, out month); months.Add(new Month(year, month, Calendar)); } return(months); } // GetMonths
} // Halfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); for (int i = 0; i < HalfyearCount; i++) { int year; YearHalfyear halfyear; TimeTool.AddHalfyear(StartYear, StartHalfyear, i, out year, out halfyear); halfyears.Add(new Halfyear(year, halfyear, Calendar)); } return(halfyears); } // GetHalfyears
} // CalendarQuarter // ---------------------------------------------------------------------- public static DateTime Quarter(YearMonth yearStartMonth) { DateTime now = ClockProxy.Clock.Now; int year = now.Year; if (now.Month - (int)yearStartMonth < 0) { year--; } YearQuarter quarter = TimeTool.GetQuarterOfMonth(yearStartMonth, (YearMonth)now.Month); int months = ((int)quarter - 1) * TimeSpec.MonthsPerQuarter; return(new DateTime(year, (int)yearStartMonth, 1).AddMonths(months)); } // Quarter
} // GetHalfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); for (int i = 0; i < yearCount; i++) { for (int quarter = 0; quarter < TimeSpec.QuartersPerYear; quarter++) { int year; YearQuarter yearQuarter; TimeTool.AddQuarter(startYear, YearQuarter.First, (i * TimeSpec.QuartersPerYear) + quarter, out year, out yearQuarter); quarters.Add(new Quarter(year, yearQuarter, Calendar)); } } return(quarters); } // GetQuarters
} // EndYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); for (int i = 0; i < yearCount; i++) { for (int halfyear = 0; halfyear < TimeSpec.HalfyearsPerYear; halfyear++) { int year; YearHalfyear yearHalfyear; TimeTool.AddHalfyear(startYear, YearHalfyear.First, (i * TimeSpec.HalfyearsPerYear) + halfyear, out year, out yearHalfyear); halfyears.Add(new Halfyear(year, yearHalfyear, Calendar)); } } return(halfyears); } // GetHalfyears
} // EndMonthOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = new DateTime(startYear, (int)startMonth, 1); for (int month = 0; month < monthCount; month++) { DateTime monthStart = startDate.AddMonths(month); int daysOfMonth = TimeTool.GetDaysInMonth(monthStart.Year, monthStart.Month); for (int day = 0; day < daysOfMonth; day++) { days.Add(new Day(monthStart.AddDays(day), Calendar)); } } return(days); } // GetDays
} // EndMonthOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = GetStartOfMonth(Calendar, startYear, startMonth); foreach (var month in Enumerable.Range(0, monthCount)) { DateTime monthStart = startDate.AddMonths(month); int daysOfMonth = TimeTool.GetDaysInMonth(monthStart.Year, monthStart.Month); foreach (var day in Enumerable.Range(0, daysOfMonth)) { days.Add(new Day(monthStart.AddDays(day), Calendar)); } } return(days); } // GetDays
} // CalcYears // ---------------------------------------------------------------------- private int CalcQuarters() { if (TimeCompare.IsSameMonth(date1, date2)) { return(0); } int year1 = TimeTool.GetYearOf(yearBaseMonth, Year1, Month1); YearQuarter quarter1 = TimeTool.GetQuarterOfMonth(yearBaseMonth, (YearMonth)Month1); int year2 = TimeTool.GetYearOf(yearBaseMonth, Year2, Month2); YearQuarter quarter2 = TimeTool.GetQuarterOfMonth(yearBaseMonth, (YearMonth)Month2); return(((year2 * TimeSpec.QuartersPerYear) + quarter2) - ((year1 * TimeSpec.QuartersPerYear) + quarter1)); } // CalcQuarters
} // GetHalfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, yearCount)) { foreach (var quarter in Enumerable.Range(0, TimeSpec.QuartersPerYear)) { int year; YearQuarter yearQuarter; TimeTool.AddQuarter(startYear, YearQuarter.First, (i * TimeSpec.QuartersPerYear) + quarter, out year, out yearQuarter); quarters.Add(new Quarter(year, yearQuarter, Calendar)); } } return(quarters); } // GetQuarters
} // GetQuarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, yearCount)) { foreach (var month in Enumerable.Range(0, TimeSpec.MonthsPerYear)) { int year; YearMonth yearMonth; TimeTool.AddMonth(startYear, YearBaseMonth, (i * TimeSpec.MonthsPerYear) + month, out year, out yearMonth); months.Add(new Month(year, yearMonth, Calendar)); } } return(months); } // GetMonths
} // EndQuarterOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); for (int i = 0; i < quarterCount; i++) { for (int month = 0; month < TimeSpec.MonthsPerQuarter; month++) { int year; YearMonth yearMonth; TimeTool.AddMonth(startYear, YearBaseMonth, (i * TimeSpec.MonthsPerQuarter) + month, out year, out yearMonth); months.Add(new Month(year, yearMonth, Calendar)); } } return(months); } // GetMonths
} // EndYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, yearCount)) { foreach (var halfyear in Enumerable.Range(0, TimeSpec.HalfyearsPerYear)) { int year; YearHalfyear yearHalfyear; TimeTool.AddHalfyear(startYear, YearHalfyear.First, (i * TimeSpec.HalfyearsPerYear) + halfyear, out year, out yearHalfyear); halfyears.Add(new Halfyear(year, yearHalfyear, Calendar)); } } return(halfyears); } // GetHalfyears
} // GetStartOfHalfyear // ---------------------------------------------------------------------- private static TimeRange GetPeriodOf(ITimeCalendar calendar, int startYear, YearHalfyear startHalfyear, int halfyearCount) { if (halfyearCount < 1) { throw new ArgumentOutOfRangeException("halfyearCount"); } DateTime start = GetStartOfHalfyear(calendar, startYear, startHalfyear); int endYear; YearHalfyear endHalfyear; TimeTool.AddHalfyear(startYear, startHalfyear, halfyearCount, out endYear, out endHalfyear); DateTime end = GetStartOfHalfyear(calendar, endYear, endHalfyear); return(new TimeRange(start, end)); } // GetPeriodOf
} // GetStartOfQuarter // ---------------------------------------------------------------------- private static TimeRange GetPeriodOf(ITimeCalendar calendar, int startYear, YearQuarter startQuarter, int quarterCount) { if (quarterCount < 1) { throw new ArgumentOutOfRangeException("quarterCount"); } DateTime start = GetStartOfQuarter(calendar, startYear, startQuarter); int endYear; YearQuarter endQuarter; TimeTool.AddQuarter(startYear, startQuarter, quarterCount, out endYear, out endQuarter); DateTime end = GetStartOfQuarter(calendar, endYear, endQuarter); return(new TimeRange(start, end)); } // GetPeriodOf
} // CalcMonths // ---------------------------------------------------------------------- private int CalcWeeks() { if (TimeCompare.IsSameDay(date1, date2)) { return(0); } DateTime week1 = TimeTool.GetStartOfWeek(date1, firstDayOfWeek); DateTime week2 = TimeTool.GetStartOfWeek(date2, firstDayOfWeek); if (week1.Equals(week2)) { return(0); } return((int)(week2.Subtract(week1).TotalDays / TimeSpec.DaysPerWeek)); } // CalcWeeks
} // IsSameWeek // ---------------------------------------------------------------------- public static bool IsSameWeek(DateTime left, DateTime right, CultureInfo culture, CalendarWeekRule weekRule, DayOfWeek firstDayOfWeek, YearWeekType weekType) { if (culture == null) { throw new ArgumentNullException("culture"); } // left int leftWeekOfYear; int leftYear; TimeTool.GetWeekOfYear(left, culture, weekRule, firstDayOfWeek, weekType, out leftYear, out leftWeekOfYear); // rught int rightWeekOfYear; int rightYear; TimeTool.GetWeekOfYear(right, culture, weekRule, firstDayOfWeek, weekType, out rightYear, out rightWeekOfYear); return(leftYear == rightYear && leftWeekOfYear == rightWeekOfYear); } // IsSameWeek
} // GetQuarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); YearMonth startMonth = YearBaseMonth; if (StartHalfyear == YearHalfyear.Second) { int year; TimeTool.AddMonth(startYear, startMonth, TimeSpec.MonthsPerHalfyear, out year, out startMonth); } for (int i = 0; i < halfyearCount; i++) { for (int month = 0; month < TimeSpec.MonthsPerHalfyear; month++) { int year; YearMonth yearMonth; TimeTool.AddMonth(startYear, startMonth, (i * TimeSpec.MonthsPerHalfyear) + month, out year, out yearMonth); months.Add(new Month(year, yearMonth, Calendar)); } } return(months); } // GetMonths
} // GetStartOfMonth // ---------------------------------------------------------------------- private static TimeRange GetPeriodOf(ITimeCalendar calendar, int startYear, YearMonth startMonth, int monthCount) { if (monthCount < 1) { throw new ArgumentOutOfRangeException("monthCount"); } DateTime start = GetStartOfMonth(calendar, startYear, startMonth); DateTime end; if (calendar.YearType == YearType.FiscalYear) { int endYear; YearMonth endMonth; TimeTool.AddMonth(startYear, startMonth, monthCount, out endYear, out endMonth); end = GetStartOfMonth(calendar, endYear, endMonth); } else { end = start.AddMonths(monthCount); } return(new TimeRange(start, end)); } // GetPeriodOf
} // GetCalendarPeriod #endregion #region Interval // ---------------------------------------------------------------------- public string GetInterval(DateTime start, DateTime end, IntervalEdge startEdge, IntervalEdge endEdge, TimeSpan duration) { if (end < start) { throw new ArgumentOutOfRangeException("end"); } StringBuilder sb = new StringBuilder(); // interval start switch (startEdge) { case IntervalEdge.Closed: sb.Append(IntervalStartClosed); break; case IntervalEdge.Open: sb.Append(UseIsoIntervalNotation ? intervalStartOpenIso : intervalStartOpen); break; } bool addDuration = true; bool startHasTimeOfDay = TimeTool.HasTimeOfDay(start); // no duration - schow start date (optionally with the time part) if (duration == TimeSpec.MinPeriodDuration) { sb.Append(startHasTimeOfDay ? GetDateTime(start) : GetShortDate(start)); addDuration = false; } // within one day: show full start, end time and suration else if (TimeCompare.IsSameDay(start, end)) { sb.Append(GetDateTime(start)); sb.Append(startEndSeparator); sb.Append(GetLongTime(end)); } else { bool endHasTimeOfDay = TimeTool.HasTimeOfDay(start); bool hasTimeOfDays = startHasTimeOfDay || endHasTimeOfDay; if (hasTimeOfDays) { sb.Append(GetDateTime(start)); sb.Append(startEndSeparator); sb.Append(GetDateTime(end)); } else { sb.Append(GetShortDate(start)); sb.Append(startEndSeparator); sb.Append(GetShortDate(end)); } } // interval end switch (endEdge) { case IntervalEdge.Closed: sb.Append(IntervalEndClosed); break; case IntervalEdge.Open: sb.Append(UseIsoIntervalNotation ? IntervalEndOpenIso : IntervalEndOpen); break; } // duration if (addDuration) { sb.Append(durationSeparator); sb.Append(GetDuration(duration)); } return(sb.ToString()); } // GetInterval
} // Year // ---------------------------------------------------------------------- public Year(DateTime moment, ITimeCalendar calendar) : this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)), calendar) { } // Year
} // Quarter // ---------------------------------------------------------------------- public Quarter(DateTime moment, ITimeCalendar calendar) : this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)), TimeTool.GetQuarterOfMonth(calendar.YearBaseMonth, (YearMonth)moment.Month), calendar) { } // Quarter