From efe02ebfd1d4290e70b2f7e2444c7b9517c14d2b Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 27 Jun 2012 11:55:43 +0300 Subject: [PATCH] added simplified weekdays --- timespans/actions.go | 15 +++++++++++++-- timespans/actions_test.go | 31 ++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/timespans/actions.go b/timespans/actions.go index 5c8d6df40..f3a200b79 100644 --- a/timespans/actions.go +++ b/timespans/actions.go @@ -128,6 +128,17 @@ func (at *ActionTiming) GetNextStartTime() (t time.Time, err error) { if i.WeekDays != nil && len(i.WeekDays) > 0 { sort.Sort(i.WeekDays) + if t.Equal(time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)) { + t = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), 0, now.Location()) + } + for _, j := range []int{0, 1, 2, 3, 4, 5, 6} { + t = time.Date(t.Year(), t.Month(), t.Day()+j, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + for _, wd := range i.WeekDays { + if t.Weekday() == wd { + return + } + } + } } if i.MonthDays != nil && len(i.MonthDays) > 0 { @@ -140,7 +151,7 @@ func (at *ActionTiming) GetNextStartTime() (t time.Time, err error) { if now.Before(t) { h, m, s := t.Clock() t = time.Date(now.Year(), now.Month(), now.Day(), h, m, s, 0, time.Local) - goto Months + goto MONTHS } if x+1 < len(i.MonthDays) { // today was found in the list, jump to the next grater day d = i.MonthDays[x+1] @@ -152,7 +163,7 @@ func (at *ActionTiming) GetNextStartTime() (t time.Time, err error) { h, m, s := t.Clock() t = time.Date(now.Year(), now.Month(), d, h, m, s, 0, time.Local) } -Months: +MONTHS: if i.Months != nil && len(i.Months) > 0 { sort.Sort(i.Months) now := time.Now() diff --git a/timespans/actions_test.go b/timespans/actions_test.go index 2293a51c6..de7e1ba74 100644 --- a/timespans/actions_test.go +++ b/timespans/actions_test.go @@ -49,7 +49,7 @@ func TestActionTimingOnlyHour(t *testing.T) { } } -/*func TestActionTimingOnlyWeekdays(t *testing.T) { +func TestActionTimingOnlyWeekdays(t *testing.T) { at := &ActionTiming{Timing: &Interval{WeekDays: []time.Weekday{time.Monday}}} st, err := at.GetNextStartTime() if err != nil { @@ -57,13 +57,20 @@ func TestActionTimingOnlyHour(t *testing.T) { } now := time.Now() y, m, d := now.Date() - expected := time.Date(y, m, d, 10, 1, 0, 0, time.Local) - if !st.Equal(expected) { - t.Errorf("Expected %v was %v", expected, st) + h, min, s := now.Clock() + e := time.Date(y, m, d, h, min, s, 0, time.Local) + for _, i := range []int{0, 1, 2, 3, 4, 5, 6} { + e = time.Date(e.Year(), e.Month(), e.Day()+i, e.Hour(), e.Minute(), e.Second(), e.Nanosecond(), e.Location()) + if e.Weekday() == time.Monday { + break + } + } + if !st.Equal(e) { + t.Errorf("Expected %v was %v", e, st) } } -func TestActionTimingWeekdaysHour(t *testing.T) { +func TestActionTimingHourWeekdays(t *testing.T) { at := &ActionTiming{Timing: &Interval{WeekDays: []time.Weekday{time.Monday}, StartTime: "10:01:00"}} st, err := at.GetNextStartTime() if err != nil { @@ -71,11 +78,17 @@ func TestActionTimingWeekdaysHour(t *testing.T) { } now := time.Now() y, m, d := now.Date() - expected := time.Date(y, m, d, 10, 1, 0, 0, time.Local) - if !st.Equal(expected) { - t.Errorf("Expected %v was %v", expected, st) + e := time.Date(y, m, d, 10, 1, 0, 0, time.Local) + for _, i := range []int{0, 1, 2, 3, 4, 5, 6} { + e = time.Date(e.Year(), e.Month(), e.Day()+i, e.Hour(), e.Minute(), e.Second(), e.Nanosecond(), e.Location()) + if e.Weekday() == time.Monday { + break + } } -}*/ + if !st.Equal(e) { + t.Errorf("Expected %v was %v", e, st) + } +} func TestActionTimingOnlyMonthdays(t *testing.T) { now := time.Now()