diff --git a/timer.go b/timer.go index cb4e4c1..f428d78 100644 --- a/timer.go +++ b/timer.go @@ -38,17 +38,19 @@ type Timer struct { // // For example: // "2019-02-15T11:43:00-0600 Working on Go Library @home @personal +timertxt customTag1:Important! due:Today" -// "x 2019-02-15T10:00:00-0600 2019-02-15T06:00:00-0600 Creating Go Library Repo @home @personal +timertxt customTag1:Important! due:Today" +// "x 2019-02-15T06:00:00-0600 2019-02-15T10:00:00-0600 Creating Go Library Repo @home @personal +timertxt customTag1:Important! due:Today" func (timer Timer) String() string { var text string if timer.Finished { text += "x " - if !timer.FinishDate.IsZero() { - text += fmt.Sprintf("%s ", timer.FinishDate.Format(DateLayout)) - } } text += fmt.Sprintf("%s ", timer.StartDate.Format(DateLayout)) - text += timer.Notes + if !timer.FinishDate.IsZero() { + text += fmt.Sprintf("%s", timer.FinishDate.Format(DateLayout)) + } + if len(timer.Notes) > 0 { + text += " " + timer.Notes + } if len(timer.Contexts) > 0 { sort.Strings(timer.Contexts) for _, context := range timer.Contexts { @@ -79,6 +81,7 @@ func (timer Timer) String() string { func NewTimer() *Timer { timer := Timer{} timer.StartDate = time.Now() + timer.AdditionalTags = make(map[string]string) return &timer } @@ -92,16 +95,19 @@ func ParseTimer(text string) (*Timer, error) { // Check for finished if originalParts[0] == "x" { timer.Finished = true - // If it's finished, there _must_ be a finished date - if timer.FinishDate, err = time.Parse(DateLayout, originalParts[1]); err != nil { - return nil, errors.New("Timer marked finished, but failed to parse FinishDate: " + err.Error()) - } - originalParts = originalParts[2:] + originalParts = originalParts[1:] } if timer.StartDate, err = time.Parse(DateLayout, originalParts[0]); err != nil { return nil, errors.New("Unable to parse StartDate: " + err.Error()) } originalParts = originalParts[1:] + if timer.Finished { + // If it's finished, there _must_ be a finished date + if timer.FinishDate, err = time.Parse(DateLayout, originalParts[0]); err != nil { + return nil, errors.New("Timer marked finished, but failed to parse FinishDate: " + err.Error()) + } + originalParts = originalParts[1:] + } var notes []string for _, v := range originalParts { if strings.HasPrefix(v, "@") { @@ -159,6 +165,18 @@ func (timer *Timer) Duration() time.Duration { return end.Sub(timer.StartDate) } +func (timer *Timer) StartsToday() bool { + currTime := time.Now() + dur := int64(currTime.Hour())*int64(time.Hour) + int64(currTime.Minute())*int64(time.Minute) + return int64(time.Since(timer.StartDate)) < dur +} + +func (timer *Timer) EndsToday() bool { + currTime := time.Now() + dur := int64(currTime.Hour())*int64(time.Hour) + int64(currTime.Minute())*int64(time.Minute) + return int64(time.Since(timer.FinishDate)) < dur +} + func (timer *Timer) ActiveToday() bool { return timer.ActiveOnDay(time.Now()) } diff --git a/timerlist.go b/timerlist.go index 4268cc3..637177b 100644 --- a/timerlist.go +++ b/timerlist.go @@ -63,16 +63,18 @@ func (timerlist *TimerList) String() string { return ret } -// AddTimer appends a Timer to the current TimerList and takes care to set the Timer.Id correctly +// AddTimer prepends a Timer to the current TimerList and takes care to set the Timer.Id correctly func (timerlist *TimerList) AddTimer(timer *Timer) { - timer.Id = 0 + // The new timer is going to be id 1 + timer.Id = 1 for _, t := range *timerlist { - if t.Id > timer.Id { - timer.Id = t.Id - } + // Everything else gets incremented + t.Id++ } - timer.Id += 1 - *timerlist = append(*timerlist, *timer) + // Now prepend the timer to the slice + *timerlist = append(*timerlist, Timer{}) + copy((*timerlist)[1:], (*timerlist)[0:]) + (*timerlist)[0] = *timer } // GetTimer returns the Timer with the given timer 'id' from the TimerList.