From 80fc60736bd7054affded9582fec91aecaaf3cce Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 4 Dec 2019 16:02:57 -0600 Subject: [PATCH 1/4] handle future start time in EndTime method --- schedule/rotation/rotation.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/schedule/rotation/rotation.go b/schedule/rotation/rotation.go index 667e661dd4..aa7ab3ad07 100644 --- a/schedule/rotation/rotation.go +++ b/schedule/rotation/rotation.go @@ -73,16 +73,39 @@ 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: + // while cTime (rotation start) is before t + for cTime.After(t) { + last = cTime + // getting 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) { cTime = addHoursAlwaysInc(cTime, r.ShiftLength) } - case TypeWeekly: - r.ShiftLength *= 7 - fallthrough - case TypeDaily: + case TypeWeekly, TypeDaily: // while cTime (rotation start) is before t for !cTime.After(t) { // getting end of shift From 551e52aa5257f3db7a1c35751fa0f07857882046 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 4 Dec 2019 16:04:03 -0600 Subject: [PATCH 2/4] add unit test for future start time --- schedule/rotation/rotation_test.go | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/schedule/rotation/rotation_test.go b/schedule/rotation/rotation_test.go index 3ef1483e43..e21c2c5925 100644 --- a/schedule/rotation/rotation_test.go +++ b/schedule/rotation/rotation_test.go @@ -1,6 +1,7 @@ package rotation import ( + "github.com/stretchr/testify/assert" "testing" "time" ) @@ -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) { From bb4c7b1dd7d10bec173c199b79ec936af2929590 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 4 Dec 2019 16:13:00 -0600 Subject: [PATCH 3/4] fix comments --- schedule/rotation/rotation.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schedule/rotation/rotation.go b/schedule/rotation/rotation.go index aa7ab3ad07..eb229b56f0 100644 --- a/schedule/rotation/rotation.go +++ b/schedule/rotation/rotation.go @@ -87,10 +87,10 @@ func (r Rotation) EndTime(t time.Time) time.Time { cTime = addHoursAlwaysInc(cTime, -r.ShiftLength) } case TypeWeekly, TypeDaily: - // while cTime (rotation start) is before t + // while cTime (rotation start) is after t for cTime.After(t) { last = cTime - // getting end of shift + // getting next end of shift cTime = cTime.AddDate(0, 0, -r.ShiftLength) } default: From c0e1f7ca0e932ec9bf42f83c856cd32bb0118125 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Thu, 5 Dec 2019 17:34:51 -0600 Subject: [PATCH 4/4] remove old redundant comments --- schedule/rotation/rotation.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/schedule/rotation/rotation.go b/schedule/rotation/rotation.go index eb229b56f0..973d51cfe2 100644 --- a/schedule/rotation/rotation.go +++ b/schedule/rotation/rotation.go @@ -87,7 +87,6 @@ func (r Rotation) EndTime(t time.Time) time.Time { cTime = addHoursAlwaysInc(cTime, -r.ShiftLength) } case TypeWeekly, TypeDaily: - // while cTime (rotation start) is after t for cTime.After(t) { last = cTime // getting next end of shift @@ -101,12 +100,10 @@ func (r Rotation) EndTime(t time.Time) time.Time { switch r.Type { case TypeHourly: - // while cTime (rotation start) is before t for !cTime.After(t) { cTime = addHoursAlwaysInc(cTime, r.ShiftLength) } case TypeWeekly, TypeDaily: - // while cTime (rotation start) is before t for !cTime.After(t) { // getting end of shift cTime = cTime.AddDate(0, 0, r.ShiftLength)