From 8039d1ec9f5537ab7f7c1e270a008ccdfe282ed3 Mon Sep 17 00:00:00 2001 From: SATYAsasini Date: Tue, 2 Jun 2026 16:58:03 +0530 Subject: [PATCH 1/2] fix: handle same weekdayFrom and weekdayTo bug --- common-lib/timeRangeLib/evaluator.go | 6 ++++++ common-lib/timeRangeLib/models.go | 2 ++ 2 files changed, 8 insertions(+) diff --git a/common-lib/timeRangeLib/evaluator.go b/common-lib/timeRangeLib/evaluator.go index 5d40ea253..9a49f580b 100644 --- a/common-lib/timeRangeLib/evaluator.go +++ b/common-lib/timeRangeLib/evaluator.go @@ -32,6 +32,12 @@ func (evaluator BaseTimeRangeExpressionEvaluator) getDurationForHourMinute() tim func (evaluator BaseTimeRangeExpressionEvaluator) getDurationBetweenWeekdays() time.Duration { days := calculateDaysBetweenWeekdays(int(evaluator.TimeRange.WeekdayFrom), int(evaluator.TimeRange.WeekdayTo)) + // When weekdayFrom == weekdayTo, days is 0. If the to-time is not after the from-time the window + // would collapse to a zero/negative duration, which stalls the next-window search (infinite loop). + // Treat such a same-weekday range as spanning the full week instead. + if days == 0 && isToBeforeFrom(evaluator.TimeRange.HourMinuteFrom, evaluator.TimeRange.HourMinuteTo) { + days = daysInWeek + } fromDateTime := constructDateTime(evaluator.TimeRange.HourMinuteFrom, 0) toDateTime := constructDateTime(evaluator.TimeRange.HourMinuteTo, days) return toDateTime.Sub(fromDateTime) diff --git a/common-lib/timeRangeLib/models.go b/common-lib/timeRangeLib/models.go index e56a93496..92804f253 100644 --- a/common-lib/timeRangeLib/models.go +++ b/common-lib/timeRangeLib/models.go @@ -55,6 +55,8 @@ type TimeRange struct { // random values for for understanding HH:MM format const hourMinuteFormat = "15:04" +const daysInWeek = 7 + type Frequency string const ( From 7412a542250942e8ffc64e6af78648a0c1ec1013 Mon Sep 17 00:00:00 2001 From: SATYAsasini Date: Wed, 3 Jun 2026 00:21:32 +0530 Subject: [PATCH 2/2] fix: reject zero/negative-length WeeklyRange window --- common-lib/timeRangeLib/constant.go | 2 +- common-lib/timeRangeLib/evaluator.go | 6 ------ common-lib/timeRangeLib/models.go | 2 -- common-lib/timeRangeLib/validator.go | 5 +++++ 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/common-lib/timeRangeLib/constant.go b/common-lib/timeRangeLib/constant.go index e4ed49d03..e3ebc3306 100644 --- a/common-lib/timeRangeLib/constant.go +++ b/common-lib/timeRangeLib/constant.go @@ -28,7 +28,7 @@ const ( WeekDaysNotPresent ErrorMessage = "weekdays, must be present for Weekly frequency" WeekDayFromOrToNotPresent ErrorMessage = "weekdayFrom, must be present for WeeklyRange frequency" DayFromOrToNotPresent ErrorMessage = "dayFrom, dayTo, must be present for Monthly frequency" - ToBeforeFrom ErrorMessage = "Invalid value of hourMinuteFrom or hourMinuteTo for same day ,hourMinuteFrom >hourMinuteTo" + ToBeforeFrom ErrorMessage = "Invalid value of hourMinuteFrom or hourMinuteTo for same day ,hourMinuteFrom > hourMinuteTo" BothLessThanZeroAndFromGreaterThanTo ErrorMessage = "invalid value of DayFrom or DayTo,DayFrom and DayTo is less than zero and dayFrom > dayTo" DayFromOrToNotValid ErrorMessage = "invalid value of dayFrom or dayTo" InvalidHourMinuteForWeeklyAndDaily ErrorMessage = "HourMinuteFrom should be less than HourMinuteTo in daily or weekly Frequency" diff --git a/common-lib/timeRangeLib/evaluator.go b/common-lib/timeRangeLib/evaluator.go index 9a49f580b..5d40ea253 100644 --- a/common-lib/timeRangeLib/evaluator.go +++ b/common-lib/timeRangeLib/evaluator.go @@ -32,12 +32,6 @@ func (evaluator BaseTimeRangeExpressionEvaluator) getDurationForHourMinute() tim func (evaluator BaseTimeRangeExpressionEvaluator) getDurationBetweenWeekdays() time.Duration { days := calculateDaysBetweenWeekdays(int(evaluator.TimeRange.WeekdayFrom), int(evaluator.TimeRange.WeekdayTo)) - // When weekdayFrom == weekdayTo, days is 0. If the to-time is not after the from-time the window - // would collapse to a zero/negative duration, which stalls the next-window search (infinite loop). - // Treat such a same-weekday range as spanning the full week instead. - if days == 0 && isToBeforeFrom(evaluator.TimeRange.HourMinuteFrom, evaluator.TimeRange.HourMinuteTo) { - days = daysInWeek - } fromDateTime := constructDateTime(evaluator.TimeRange.HourMinuteFrom, 0) toDateTime := constructDateTime(evaluator.TimeRange.HourMinuteTo, days) return toDateTime.Sub(fromDateTime) diff --git a/common-lib/timeRangeLib/models.go b/common-lib/timeRangeLib/models.go index 92804f253..e56a93496 100644 --- a/common-lib/timeRangeLib/models.go +++ b/common-lib/timeRangeLib/models.go @@ -55,8 +55,6 @@ type TimeRange struct { // random values for for understanding HH:MM format const hourMinuteFormat = "15:04" -const daysInWeek = 7 - type Frequency string const ( diff --git a/common-lib/timeRangeLib/validator.go b/common-lib/timeRangeLib/validator.go index 94bb1d3f0..9f0c31be0 100644 --- a/common-lib/timeRangeLib/validator.go +++ b/common-lib/timeRangeLib/validator.go @@ -86,6 +86,11 @@ func (tr TimeRange) ValidateTimeRange() error { if (tr.WeekdayFrom < 0 || tr.WeekdayFrom > 6) || (tr.WeekdayTo < 0 || tr.WeekdayTo > 6) { return errors.New(string(WeekDayOutsideRange)) } + // Same weekday with a non-positive intra-day span collapses the window to zero/negative + // duration, which stalls the next-window search. Reject it (mirrors the Monthly check). + if tr.WeekdayFrom == tr.WeekdayTo && isToBeforeFrom(tr.HourMinuteFrom, tr.HourMinuteTo) { + return errors.New(string(ToBeforeFrom)) + } case Monthly: if tr.DayFrom == 0 || tr.DayTo == 0 { return errors.New(string(DayFromOrToNotPresent))