From c62767a5ef139577cbc8974089ef7a897c004356 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 3 Aug 2023 16:02:43 -0500 Subject: [PATCH] Some Changes --- timerlist.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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