Skip to content
Permalink
Browse files

Fix behaviour of DayTimeSchedule

  • Loading branch information...
erizocosmico committed Nov 1, 2016
1 parent 9a42549 commit cbd3e2b134f9ef4a7c8d79a75002c4766c81ed62
Showing with 23 additions and 10 deletions.
  1. +20 −7 schedule.go
  2. +3 −3 schedule_test.go
@@ -56,8 +56,8 @@ func (s *timeSchedule) Next(now time.Time) time.Time {
}

type dayTimeSchedule struct {
days map[time.Weekday]struct{}
timeSchedule ScheduleTime
days map[time.Weekday]struct{}
hour, minutes, seconds int
}

// NewDayTimeSchedule creates a ScheduleTime that runs once a day at a given
@@ -70,7 +70,7 @@ func NewDayTimeSchedule(days []time.Weekday, hour, minutes, seconds int) Schedul

return &dayTimeSchedule{
daySet,
NewTimeSchedule(hour, minutes, seconds),
hour, minutes, seconds,
}
}

@@ -81,12 +81,25 @@ func (s *dayTimeSchedule) Next(now time.Time) time.Time {
return zero
}

var t = now
for {
if _, ok := s.days[now.Weekday()]; ok {
t := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
return s.timeSchedule.Next(t)
if _, ok := s.days[t.Weekday()]; ok {
d := time.Date(
t.Year(),
t.Month(),
t.Day(),
s.hour,
s.minutes,
s.seconds,
0,
t.Location(),
)

if d.After(now) {
return d
}
}

now = now.Add(24 * time.Hour)
t = t.Add(24 * time.Hour)
}
}
@@ -100,15 +100,15 @@ func TestDayTimeScheduleAfter(t *testing.T) {
},
{
time.Date(2016, time.October, 5, 16, 0, 0, 0, time.Local),
time.Wednesday,
time.Thursday,
},
{
time.Date(2016, time.October, 6, 16, 0, 0, 0, time.Local),
time.Thursday,
time.Friday,
},
{
time.Date(2016, time.October, 7, 16, 0, 0, 0, time.Local),
time.Friday,
time.Wednesday,
},
{
time.Date(2016, time.October, 8, 16, 0, 0, 0, time.Local),

0 comments on commit cbd3e2b

Please sign in to comment.
You can’t perform that action at this time.