Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions v2/cal_business.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,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
Expand Down
12 changes: 12 additions & 0 deletions v2/cal_business_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func TestIsWorkTime(t *testing.T) {
cal3.SetWorkday(time.Friday, true)
cal3.SetWorkHours(6*time.Hour, 24*time.Hour)

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
Expand All @@ -131,6 +138,11 @@ func TestIsWorkTime(t *testing.T) {
{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},
{cal3, dt(2023, 10, 19, 23, 59), true},
{cal3, dt(2023, 10, 20, 0, 0), false},
}
Expand Down