File size: 1,423 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package rotationmanager
import (
"time"
"github.com/target/goalert/schedule/rotation"
)
// legacyAddHours is preserved from the old rotation calculation code to migrate from version 1 to 2.
// It simply reinterprets the date with the number of hours added to the clock time.
func legacyAddHours(t time.Time, n int) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+n, t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
// legacyAddHoursAlwaysInc is preserved from the old rotation calculation code to migrate
// from version 1 to 2. It adds a number of hours ensuring the unix timestamp always progresses
// forward.
func legacyAddHoursAlwaysInc(t time.Time, n int) time.Time {
res := legacyAddHours(t, n)
if n < 0 {
for !res.Before(t) {
n--
res = legacyAddHours(t, n)
}
} else {
for !res.After(t) {
n++
res = legacyAddHours(t, n)
}
}
return res
}
func calcVersion1EndTime(rot *rotation.Rotation, shiftStart time.Time) time.Time {
if rot.Type != rotation.TypeHourly {
return rot.EndTime(shiftStart)
}
cTime := rot.Start.Truncate(time.Minute)
t := shiftStart.In(cTime.Location())
if cTime.After(t) {
// reverse search
last := cTime
for cTime.After(t) {
last = cTime
cTime = legacyAddHoursAlwaysInc(cTime, -rot.ShiftLength)
}
return last
}
for !cTime.After(t) {
cTime = legacyAddHoursAlwaysInc(cTime, rot.ShiftLength)
}
return cTime
}
|