} // Halfyear // ---------------------------------------------------------------------- public static TimeSpan Halfyear(Calendar calendar, int year, YearHalfyear yearHalfyear) { YearMonth[] halfyearMonths = TimeTool.GetMonthsOfHalfyear(yearHalfyear); TimeSpan duration = TimeSpec.NoDuration; foreach (YearMonth halfyearMonth in halfyearMonths) { duration = duration.Add(Month(calendar, year, halfyearMonth)); } return(duration); } // Halfyear
} // Quarter // ---------------------------------------------------------------------- public static TimeSpan Quarter(Calendar calendar, int year, YearQuarter yearQuarter) { YearMonth[] quarterMonths = TimeTool.GetMonthsOfQuarter(yearQuarter); TimeSpan duration = TimeSpec.NoDuration; foreach (YearMonth quarterMonth in quarterMonths) { duration = duration.Add(Month(calendar, year, quarterMonth)); } return(duration); } // Quarter
} // 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
} // 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; for (int i = 0; i < dayCount; i++) { days.Add(new Day(startDate.AddDays(i), Calendar)); } return(days); } // GetDays
} // Halfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); for (int i = 0; i < HalfyearCount; i++) { int year; YearHalfyear halfyear; TimeTool.AddHalfyear(BaseYear, StartHalfyear, i, out year, out halfyear); halfyears.Add(new Halfyear(year, halfyear, Calendar)); } return(halfyears); } // GetHalfyears
} // 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
} // 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
} // 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
} // 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
} // GetQuarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); for (int i = 0; i < halfyearCount; i++) { for (int month = 0; month < TimeSpec.MonthsPerHalfyear; month++) { int year; YearMonth yearMonth; TimeTool.AddMonth(startYear, YearBaseMonth, (i * TimeSpec.MonthsPerHalfyear) + month, out year, out yearMonth); months.Add(new Month(year, yearMonth, Calendar)); } } return(months); } // GetMonths
} // EndMonthOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = GetStartOfMonth(Calendar, startYear, startMonth); 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
} // 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
} // 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
} // 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
} // GetWeekOfYear // ---------------------------------------------------------------------- public virtual DateTime GetStartOfYearWeek(int year, int weekOfYear) { return(TimeTool.GetStartOfYearWeek(year, weekOfYear, culture, yearWeekType)); } // GetStartOfYearWeek
} // WeekTimeRange // ---------------------------------------------------------------------- protected WeekTimeRange(DateTime moment, int weekCount, ITimeCalendar calendar) : base(GetPeriodOf(moment, weekCount, calendar), calendar) { TimeTool.GetWeekOfYear(moment, calendar.Culture, calendar.YearWeekType, out year, out startWeek); this.weekCount = weekCount; } // WeekTimeRange
} // 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
} // GetNextWeek // ---------------------------------------------------------------------- public Week AddWeeks(int weeks) { DateTime startDate = TimeTool.GetStartOfYearWeek(Year, StartWeek, Calendar.Culture, Calendar.YearWeekType); return(new Week(startDate.AddDays(weeks * TimeSpec.DaysPerWeek), Calendar)); } // AddWeeks
} // IsSameYear // ---------------------------------------------------------------------- public static bool IsSameYear(YearMonth yearStartMonth, DateTime left, DateTime right) { return(TimeTool.GetYearOf(yearStartMonth, left) == TimeTool.GetYearOf(yearStartMonth, right)); } // IsSameYear
} // 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
} // Years // ---------------------------------------------------------------------- public Years(DateTime moment, int count, ITimeCalendar calendar) : this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)), count, calendar) { } // Years
} // Halfyear // ---------------------------------------------------------------------- public Halfyear(DateTime moment, ITimeCalendar calendar) : this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)), TimeTool.GetHalfyearOfMonth(calendar.YearBaseMonth, (YearMonth)calendar.GetMonth(moment)), calendar) { } // Halfyear
} // Halfyears // ---------------------------------------------------------------------- public Halfyears(DateTime moment, YearHalfyear startHalfyear, int count, ITimeCalendar calendar) : this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)), startHalfyear, count, calendar) { } // Halfyears
} // Quarters // ---------------------------------------------------------------------- public Quarters(DateTime moment, YearQuarter startYearQuarter, int count, ITimeCalendar calendar) : this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)), startYearQuarter, count, calendar) { } // Quarters