/// <summary> /// For the given date/time is the validation period instance active. /// </summary> /// <param name="currentTime">Date/time to check for activity.</param> /// <returns>True is validation period instance is active for date/time; False otherwise.</returns> public bool IsActive(DateTimeOffset currentTime) { bool promoPeriodValid = false; if (this.ValidationType == DateValidationType.Advanced) { if (this.Period != null) { promoPeriodValid = IsValidationPeriodActive(this.Period, currentTime); } } else if (this.ValidationType == DateValidationType.Standard) { promoPeriodValid = InternalValidationPeriod.IsDateWithinStartEndDate(currentTime.Date, this.StartDate, this.EndDate); } else { string error = string.Format(CultureInfo.InvariantCulture, "The validation type '{0}' is not supported.", this.ValidationType); throw new NotSupportedException(error); } return(promoPeriodValid); }
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); }