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

schedule/rotation: fix endtime calculation bug #254

Merged
merged 4 commits into from
Dec 6, 2019
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
32 changes: 26 additions & 6 deletions schedule/rotation/rotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,37 @@ func (r Rotation) EndTime(t time.Time) time.Time {
t = t.Truncate(time.Minute)
cTime := r.Start.Truncate(time.Minute)

if r.Type == TypeWeekly {
r.ShiftLength *= 7
}

if cTime.After(t) {
// reverse search
last := cTime
switch r.Type {
case TypeHourly:
for cTime.After(t) {
last = cTime
cTime = addHoursAlwaysInc(cTime, -r.ShiftLength)
}
case TypeWeekly, TypeDaily:
for cTime.After(t) {
last = cTime
// getting next end of shift
cTime = cTime.AddDate(0, 0, -r.ShiftLength)
}
default:
panic("unexpected rotation type")
}
return last
}

switch r.Type {
case TypeHourly:
// while cTime (rotation start) is before t
for !cTime.After(t) {
dctalbot marked this conversation as resolved.
Show resolved Hide resolved
cTime = addHoursAlwaysInc(cTime, r.ShiftLength)
}
case TypeWeekly:
r.ShiftLength *= 7
fallthrough
case TypeDaily:
// while cTime (rotation start) is before t
case TypeWeekly, TypeDaily:
for !cTime.After(t) {
// getting end of shift
cTime = cTime.AddDate(0, 0, r.ShiftLength)
Expand Down
31 changes: 31 additions & 0 deletions schedule/rotation/rotation_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rotation

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
Expand Down Expand Up @@ -167,6 +168,36 @@ func TestRotation_Normalize(t *testing.T) {
}
}

func TestRotation_FutureStart(t *testing.T) {
rot := Rotation{
Type: TypeDaily,
ShiftLength: 1,

// StartTime and EndTime should work correctly even if Start
// is in the future.
Start: time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC),
}

assert.Equal(t, time.Date(2019, 0, 6, 0, 0, 0, 0, time.UTC),
rot.EndTime(time.Date(2019, 0, 5, 0, 0, 0, 0, time.UTC)),
)
assert.Equal(t, time.Date(2019, 0, 5, 0, 0, 0, 0, time.UTC),
rot.StartTime(time.Date(2019, 0, 5, 0, 0, 0, 0, time.UTC)),
)
assert.Equal(t, time.Date(2019, 0, 4, 0, 0, 0, 0, time.UTC),
rot.StartTime(time.Date(2019, 0, 5, 0, 0, 0, -1, time.UTC)),
)
assert.Equal(t, time.Date(2019, 0, 11, 0, 0, 0, 0, time.UTC),
rot.EndTime(time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC)),
)
assert.Equal(t, time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC),
rot.StartTime(time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC)),
)
assert.Equal(t, time.Date(2019, 0, 9, 0, 0, 0, 0, time.UTC),
rot.StartTime(time.Date(2019, 0, 10, 0, 0, 0, -1, time.UTC)),
)
}

func TestRotation_StartTime(t *testing.T) {
test := func(start, end string, len int, dur time.Duration, typ Type) {
t.Run(string(typ), func(t *testing.T) {
Expand Down