/// <summary> /// Determines if two reporting periods overlap. /// For example, the following reporting periods have an overlap: 1Q2017-3Q2017 and 3Q2017-4Q2017. /// </summary> /// <remarks> /// If the endpoint of one reporting period is the same as the endpoint /// of the second reporting period, the reporting periods are deemed to overlap. /// </remarks> /// <param name="reportingPeriod1">A reporting period.</param> /// <param name="reportingPeriod2">A second reporting period to check for overlap against the first reporting period.</param> /// <returns> /// true if there is an overlap between the reporting periods; false otherwise. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod1"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod2"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="reportingPeriod1"/> cannot be compared against <paramref name="reportingPeriod2"/> because they represent different <see cref="UnitOfTime.UnitOfTimeKind"/>.</exception> public static bool HasOverlapWith( this ReportingPeriod reportingPeriod1, ReportingPeriod reportingPeriod2) { if (reportingPeriod1 == null) { throw new ArgumentNullException(nameof(reportingPeriod1)); } if (reportingPeriod2 == null) { throw new ArgumentNullException(nameof(reportingPeriod2)); } if (reportingPeriod1.GetUnitOfTimeKind() != reportingPeriod2.GetUnitOfTimeKind()) { throw new ArgumentException(Invariant($"{nameof(reportingPeriod1)} cannot be compared against {nameof(reportingPeriod2)} because they represent different {nameof(UnitOfTimeKind)}")); } var result = reportingPeriod1.Contains(reportingPeriod2.Start) || reportingPeriod1.Contains(reportingPeriod2.End) || reportingPeriod2.Contains(reportingPeriod1.Start) || reportingPeriod2.Contains(reportingPeriod1.End); return(result); }
/// <summary> /// Determines if a unit-of-time is contained within a reporting period. /// For example, 2Q2017 is contained within a reporting period of 1Q2017-4Q2017. /// </summary> /// <remarks> /// If the unit-of-time is equal to one of the endpoints of the reporting period, /// that unit-of-time is considered to be within the reporting period. /// </remarks> /// <param name="reportingPeriod">The reporting period.</param> /// <param name="unitOfTime">The unit-of-time to check against a reporting period.</param> /// <returns> /// true if the unit-of-time is contained within the reporting period; false otherwise. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="unitOfTime"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="unitOfTime"/> cannot be compared against <paramref name="reportingPeriod"/> because they represent different <see cref="UnitOfTime.UnitOfTimeKind"/>.</exception> public static bool Contains( this ReportingPeriod reportingPeriod, UnitOfTime unitOfTime) { if (reportingPeriod == null) { throw new ArgumentNullException(nameof(reportingPeriod)); } if (unitOfTime == null) { throw new ArgumentNullException(nameof(unitOfTime)); } if (unitOfTime.UnitOfTimeKind != reportingPeriod.GetUnitOfTimeKind()) { throw new ArgumentException(Invariant($"{nameof(unitOfTime)} cannot be compared against {nameof(reportingPeriod)} because they represent different {nameof(UnitOfTimeKind)}.")); } var result = reportingPeriod.Contains(new ReportingPeriod(unitOfTime, unitOfTime)); return(result); }