Skip to content

Commit fae457e

Browse files
authored
Merge pull request #718 from haoyang1994/fix_timerange
Fix the issue of incorrect time range calculation across days
2 parents 6c00549 + 79154e8 commit fae457e

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

internal/time_range.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,12 @@ func NewWeekRangeInLocation(startTime, endTime TimeOfDay, startDay, endDay time.
7878
return r, nil
7979
}
8080

81-
func (r *TimeRange) isInTimeRange(t time.Time) bool {
82-
t = t.In(r.loc)
83-
ts := NewTimeOfDay(t.Clock()).d
84-
81+
func (r *TimeRange) isInWeekdays(day time.Weekday) bool {
8582
if len(r.weekdays) > 0 {
8683
found := false
8784

8885
for _, weekday := range r.weekdays {
89-
if t.Weekday() == weekday {
86+
if day == weekday {
9087
found = true
9188
break
9289
}
@@ -97,11 +94,34 @@ func (r *TimeRange) isInTimeRange(t time.Time) bool {
9794
}
9895
}
9996

97+
return true
98+
}
99+
100+
func (r *TimeRange) addWeekdayOffset(day time.Weekday, offset int) time.Weekday {
101+
return (day + time.Weekday(offset)) % 7
102+
}
103+
104+
func (r *TimeRange) isInTimeRange(t time.Time) bool {
105+
t = t.In(r.loc)
106+
ts := NewTimeOfDay(t.Clock()).d
107+
100108
if r.startTime.d < r.endTime.d {
101-
return r.startTime.d <= ts && ts <= r.endTime.d
109+
if r.isInWeekdays(t.Weekday()) {
110+
return r.startTime.d <= ts && ts <= r.endTime.d
111+
}
112+
113+
return false
114+
}
115+
116+
if ts <= r.endTime.d {
117+
return r.isInWeekdays(r.addWeekdayOffset(t.Weekday(), -1))
118+
}
119+
120+
if ts >= r.startTime.d {
121+
return r.isInWeekdays(t.Weekday())
102122
}
103123

104-
return !(r.endTime.d < ts && ts < r.startTime.d)
124+
return false
105125
}
106126

107127
func (r *TimeRange) isInWeekRange(t time.Time) bool {

internal/time_range_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,33 @@ func TestTimeRangeIsInRange(t *testing.T) {
245245
now: time.Date(2016, time.August, 10, 2, 0, 0, 0, time.UTC),
246246
expectedInRange: false,
247247
},
248+
{
249+
label: "18:59:00 Sunday, crossing midnight",
250+
start: NewTimeOfDay(19, 0, 0),
251+
end: NewTimeOfDay(6, 0, 0),
252+
weekdays: []time.Weekday{time.Sunday},
253+
location: time.UTC,
254+
now: time.Date(2006, time.December, 3, 18, 59, 0, 0, time.UTC),
255+
expectedInRange: false,
256+
},
257+
{
258+
label: "5:59AM Monday, crossing midnight",
259+
start: NewTimeOfDay(19, 0, 0),
260+
end: NewTimeOfDay(6, 0, 0),
261+
weekdays: []time.Weekday{time.Sunday},
262+
location: time.UTC,
263+
now: time.Date(2006, time.December, 4, 5, 59, 0, 0, time.UTC),
264+
expectedInRange: true,
265+
},
266+
{
267+
label: "6:01AM Monday, crossing midnight",
268+
start: NewTimeOfDay(19, 0, 0),
269+
end: NewTimeOfDay(6, 0, 0),
270+
weekdays: []time.Weekday{time.Sunday},
271+
location: time.UTC,
272+
now: time.Date(2006, time.December, 4, 6, 1, 0, 0, time.UTC),
273+
expectedInRange: false,
274+
},
248275
}
249276

250277
for _, tc := range testcases {

0 commit comments

Comments
 (0)