Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for setting '24h' as end work hour #119

Merged
merged 1 commit into from
Nov 4, 2023
Merged
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
14 changes: 11 additions & 3 deletions v2/cal_business.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,17 @@ func (c *BusinessCalendar) IsWorkTime(date time.Time) bool {
startSecond = startTime.Second()
}
if c.WorkdayEndFunc == nil {
endHour = int(c.workdayEnd.Hours()) % 24
endMinute = int(c.workdayEnd.Minutes()) % 60
endSecond = int(c.workdayEnd.Seconds()) % 60
// Using 24h as "till the end of day" seems to be pretty common.
// By subtracting a second we get the desired behavior.
if c.workdayEnd == 24*time.Hour {
endHour = 23
endMinute = 59
endSecond = 59
} else {
endHour = int(c.workdayEnd.Hours()) % 24
endMinute = int(c.workdayEnd.Minutes()) % 60
endSecond = int(c.workdayEnd.Seconds()) % 60
}
} else {
endTime := c.WorkdayEndFunc(date)
endHour = endTime.Hour()
Expand Down
14 changes: 13 additions & 1 deletion v2/cal_business_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ func TestSetWorkday(t *testing.T) {
}
}

func TestSetWorkingHours(t *testing.T) {
func TestSetWorkHours(t *testing.T) {
b := NewBusinessCalendar()
b.SetWorkHours(8*time.Hour+30*time.Minute, 18*time.Hour+15*time.Minute)
if b.workdayStart != 8*time.Hour+30*time.Minute || b.workdayEnd != 18*time.Hour+15*time.Minute {
t.Errorf("invalid work hours; want 9am-5pm; got: %d, %d", b.workdayStart, b.workdayEnd)
}

b.SetWorkHours(6*time.Hour, 24*time.Hour)
if b.workdayStart != 6*time.Hour || b.workdayEnd != 24*time.Hour {
t.Errorf("invalid work hours; want 6am to end of day; got: %d, %d", b.workdayStart, b.workdayEnd)
}
}

func TestIsWorkday(t *testing.T) {
Expand Down Expand Up @@ -100,12 +105,16 @@ 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)
}
cal2.WorkdayEndFunc = func(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12+6, 45, 0, 0, time.UTC)
}
cal3.SetWorkday(time.Thursday, true)
cal3.SetWorkday(time.Friday, true)
cal3.SetWorkHours(6*time.Hour, 24*time.Hour)

tests := []struct {
c *BusinessCalendar
Expand All @@ -121,6 +130,9 @@ 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(2023, 10, 19, 23, 59), true},
{cal3, dt(2023, 10, 20, 0, 0), false},
}

for i, test := range tests {
Expand Down
Loading