Skip to content

Commit

Permalink
Merge pull request #109 from Tarang/patch-1
Browse files Browse the repository at this point in the history
Allow seconds to be used
  • Loading branch information
rickar authored Feb 22, 2023
2 parents 7fbb524 + ae4c578 commit 445b497
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
13 changes: 10 additions & 3 deletions v2/cal_business.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,34 @@ func (c *BusinessCalendar) IsWorkTime(date time.Time) bool {
var startMinute int
var endHour int
var endMinute int
var startSecond int
var endSecond int
if c.WorkdayStartFunc == nil {
startHour = int(c.workdayStart.Hours()) % 24
startMinute = int(c.workdayStart.Minutes()) % 60
startSecond = int(c.workdayStart.Seconds()) % 60
} else {
startTime := c.WorkdayStartFunc(date)
startHour = startTime.Hour()
startMinute = startTime.Minute()
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
} else {
endTime := c.WorkdayEndFunc(date)
endHour = endTime.Hour()
endMinute = endTime.Minute()
endSecond = endTime.Second()
}

h, m, _ := date.Clock()
return (h == startHour && m >= startMinute) ||
h, m, s := date.Clock()
return (h == startHour && m >= startMinute && s >= startSecond) ||
(h > startHour && h < endHour) ||
(h == endHour && m <= endMinute)
(h == endHour && m < endMinute) ||
(h == endHour && m == endMinute && s <= endSecond)
}

// WorkdaysRemain reports the total number of remaining workdays in the month
Expand Down
40 changes: 40 additions & 0 deletions v2/cal_business_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func dt(y, m, d, h, min int) time.Time {
return time.Date(y, time.Month(m), d, h, min, 0, 0, time.UTC)
}

func dts(y, m, d, h, min, sec int) time.Time {
return time.Date(y, time.Month(m), d, h, min, sec, 0, time.UTC)
}

func TestNewBusinessCalendar(t *testing.T) {
b := NewBusinessCalendar()
if b.workday[time.Sunday] || !b.workday[time.Monday] || !b.workday[time.Tuesday] || !b.workday[time.Wednesday] ||
Expand Down Expand Up @@ -127,6 +131,42 @@ func TestIsWorkTime(t *testing.T) {
}
}

func TestIsWorkTimeSeconds(t *testing.T) {
cal := NewBusinessCalendar()
cal.WorkdayStartFunc = func(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12, 30, 30, 0, time.UTC)
}
cal.WorkdayEndFunc = func(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), date.Day()%12+6, 45, 30, 0, time.UTC)
}

tests := []struct {
c *BusinessCalendar
d time.Time
want bool
}{
//Start boundary
{cal, dts(2020, 4, 1, 1, 30, 29), false},
{cal, dts(2020, 4, 1, 1, 30, 30), true},
{cal, dts(2020, 4, 1, 1, 30, 31), true},

//During day
{cal, dts(2020, 4, 1, 5, 30, 31), true},

//End boundary
{cal, dts(2020, 4, 1, 7, 45, 29), true},
{cal, dts(2020, 4, 1, 7, 45, 30), true},
{cal, dts(2020, 4, 1, 7, 45, 31), false},
}

for i, test := range tests {
got := test.c.IsWorkTime(test.d)
if got != test.want {
t.Errorf("got: %t; want: %t (%d)", got, test.want, i)
}
}
}

func TestWorkdaysRemain(t *testing.T) {
c := NewBusinessCalendar()
hol := &Holiday{
Expand Down

0 comments on commit 445b497

Please sign in to comment.