diff --git a/timerlist.go b/timerlist.go index ac7c55f..6463326 100644 --- a/timerlist.go +++ b/timerlist.go @@ -23,6 +23,40 @@ func NewTimerList() *TimerList { return &TimerList{} } func (timerlist *TimerList) Size() int { return len(timerlist.timers) } func (timerlist *TimerList) GetTimerSlice() []*Timer { return timerlist.timers } +func (timerlist *TimerList) Contains(t *Timer) bool { + for _, tmr := range timerlist.timers { + if tmr == t { + return true + } + } + return false +} +func (timerlist *TimerList) GetActiveOrMostRecent() (*Timer, error) { + var found *Timer + var latest time.Time + activeTimers := timerlist.Filter(func(t *Timer) bool { return !t.Finished }) + if len(activeTimers.timers) > 0 { + return activeTimers.GetMostRecentTimer() + } + + for _, t := range timerlist.timers { + if t.FinishDate.IsZero() { + if t.StartDate.After(latest) { + found = t + latest = t.StartDate + } + } else { + if t.FinishDate.After(latest) { + latest = t.FinishDate + found = t + } + } + } + if found == nil { + return nil, errors.New("No timer found") + } + return found, nil +} func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) { var found *Timer var latest time.Time