} // 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
} // 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
} // 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
} // 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
} // IsSameYear // ---------------------------------------------------------------------- public static bool IsSameYear(YearMonth yearStartMonth, DateTime left, DateTime right) { return(TimeTool.GetYearOf(yearStartMonth, left) == TimeTool.GetYearOf(yearStartMonth, right)); } // IsSameYear
} // 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