private static bool IsPeriodActiveForDayAndTime(ValidationPeriod period, DayOfWeek day, TimeSpan time, bool testOnlyAfterMidnight) { var configuration = new PeriodRangeConfiguration { StartTime = period.StartingTimeForDay(day), EndTime = period.EndingTimeForDay(day), EndsTomorrow = period.IsEndTimeAfterMidnightForDay(day), IsActiveOnlyWithinBounds = period.IsTimeBoundedForDay(day), }; int currentTime = Convert.ToInt32(time.TotalSeconds); return(IsTimeActiveForConfiguration(currentTime, configuration, testOnlyAfterMidnight)); }
/// <summary> /// For a given time, and period time-range setup, and whether to restrict our search to after midnight, /// this method tells if the given time is active or inactive within the context of the range. /// </summary> /// <param name="currentTime">Current time in seconds past midnight.</param> /// <param name="configuration">Period time range setup parameters.</param> /// <param name="testOnlyAfterMidnight">Whether we only check for activity after midnight.</param> /// <returns>Result telling if given time is active in the configuration.</returns> private static bool IsTimeActiveForConfiguration(int currentTime, PeriodRangeConfiguration configuration, bool testOnlyAfterMidnight) { // if time falls between start and end times, return true if set to be active in range bool rangeAppliesBeforeMidnight = configuration.StartTime <= currentTime && ((configuration.EndTime >= currentTime) || configuration.EndsTomorrow); if (!testOnlyAfterMidnight && rangeAppliesBeforeMidnight) { return(configuration.IsActiveOnlyWithinBounds); } // if time is before end time for ending times past midnight, return true if set to be active in range bool rangeAppliesAfterMidnight = configuration.EndsTomorrow && configuration.EndTime >= currentTime; if (rangeAppliesAfterMidnight) { return(configuration.IsActiveOnlyWithinBounds); } return(!configuration.IsActiveOnlyWithinBounds); }
private static bool IsValidationPeriodActive(ValidationPeriod validationPeriod, DateTimeOffset transDateTime) { if (validationPeriod == null || string.IsNullOrEmpty(validationPeriod.PeriodId)) { // If no period Id given, then it is always a valid period return(true); } DateTime transDate = transDateTime.Date; TimeSpan transTime = transDateTime.TimeOfDay; // Is the discount valid within the start and end date period? if (InternalValidationPeriod.IsDateWithinStartEndDate(transDate, validationPeriod.ValidFrom.Date, validationPeriod.ValidTo.Date)) { bool answerFound = false; bool isActive = false; // does today's configuration tell if period is active? if (IsRangeDefinedForDay(validationPeriod, transDate.DayOfWeek)) { isActive = IsPeriodActiveForDayAndTime(validationPeriod, transDate.DayOfWeek, transTime, false); answerFound = true; } // if we don't know or got negative result, see if yesterday will activate it (if its range ends after midnight) DayOfWeek yesterday = transDate.AddDays(-1).DayOfWeek; bool lastRangeDefinedAfterMidnight = IsRangeDefinedForDay(validationPeriod, yesterday) && validationPeriod.IsEndTimeAfterMidnightForDay(yesterday); if ((!answerFound || isActive == false) && lastRangeDefinedAfterMidnight) { // if yesterday makes it active, set isActive = true isActive = IsPeriodActiveForDayAndTime(validationPeriod, yesterday, transTime, true); answerFound = true; } // if we still don't know, try using general configuration if (!answerFound) { var configuration = new PeriodRangeConfiguration { StartTime = validationPeriod.StartingTime, EndTime = validationPeriod.EndingTime, IsActiveOnlyWithinBounds = validationPeriod.IsTimeBounded != 0, EndsTomorrow = validationPeriod.IsEndTimeAfterMidnight != 0 }; if ((validationPeriod.StartingTime != 0) && (validationPeriod.EndingTime != 0)) { int currentTime = Convert.ToInt32(transTime.TotalSeconds); isActive = IsTimeActiveForConfiguration(currentTime, configuration, false); answerFound = true; } } return(answerFound ? isActive : (validationPeriod.IsTimeBounded == 1)); } // not within date range, so active if not set to be within date range return(validationPeriod.IsTimeBounded != 1); }