From 65bced0eeea91e0a14b3c050bcdb3c292d7c2ec9 Mon Sep 17 00:00:00 2001 From: Alpha Date: Wed, 1 Nov 2023 13:17:13 +1100 Subject: [PATCH] Fix work time check issue when startHour and endHour are the same --- v2/cal_business.go | 15 ++++++++++----- v2/cal_business_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/v2/cal_business.go b/v2/cal_business.go index 9b0e770..6fa4fab 100644 --- a/v2/cal_business.go +++ b/v2/cal_business.go @@ -120,11 +120,16 @@ func (c *BusinessCalendar) IsWorkTime(date time.Time) bool { } h, m, s := date.Clock() - return (h == startHour && m == startMinute && s >= startSecond) || - (h == startHour && m > startMinute) || - (h > startHour && h < endHour) || - (h == endHour && m < endMinute) || - (h == endHour && m == endMinute && s <= endSecond) + + timeOfDateCalculator := func(hour, minute, second int) time.Duration { + return time.Duration(hour)*time.Hour + time.Duration(minute)*time.Minute + time.Duration(second)*time.Second + } + + timeOfDate := timeOfDateCalculator(h, m, s) + startTimeOfDate := timeOfDateCalculator(startHour, startMinute, startSecond) + endTimeOfDate := timeOfDateCalculator(endHour, endMinute, endSecond) + + return timeOfDate >= startTimeOfDate && timeOfDate <= endTimeOfDate } // WorkdaysRemain reports the total number of remaining workdays in the month diff --git a/v2/cal_business_test.go b/v2/cal_business_test.go index fe561f4..9100f60 100644 --- a/v2/cal_business_test.go +++ b/v2/cal_business_test.go @@ -100,6 +100,7 @@ func TestIsWorkday(t *testing.T) { func TestIsWorkTime(t *testing.T) { cal1 := NewBusinessCalendar() cal2 := NewBusinessCalendar() + cal3 := NewBusinessCalendar() cal2.WorkdayStartFunc = func(date time.Time) time.Time { return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12, 30, 0, 0, time.UTC) } @@ -107,6 +108,13 @@ func TestIsWorkTime(t *testing.T) { return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12+6, 45, 0, 0, time.UTC) } + cal3.WorkdayStartFunc = func(date time.Time) time.Time { + return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12, 30, 0, 0, time.UTC) + } + cal3.WorkdayEndFunc = func(date time.Time) time.Time { + return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12, 45, 0, 0, time.UTC) + } + tests := []struct { c *BusinessCalendar d time.Time @@ -121,6 +129,12 @@ func TestIsWorkTime(t *testing.T) { {cal2, dt(2020, 4, 1, 0, 00), false}, {cal2, dt(2020, 4, 1, 7, 00), true}, {cal2, dt(2020, 4, 1, 7, 50), false}, + + {cal3, dt(2020, 4, 1, 1, 30), true}, + {cal3, dt(2020, 4, 1, 0, 00), false}, + {cal3, dt(2020, 4, 1, 1, 35), true}, + {cal3, dt(2020, 4, 1, 1, 45), true}, + {cal3, dt(2020, 4, 1, 1, 46), false}, } for i, test := range tests {