From 6e0ecbb0072e236a235204a7a8e3d49bc345a5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sun, 7 Feb 2021 14:22:16 -0400 Subject: [PATCH] Fixed #7, treating entry.Finished for running entries as time.Now() --- z/calendar.go | 17 ++++++++++++----- z/entry.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/z/calendar.go b/z/calendar.go index a09a894..bb515bc 100644 --- a/z/calendar.go +++ b/z/calendar.go @@ -40,6 +40,7 @@ func NewCalendar(entries []Entry) (Calendar, error) { projects := make(map[string]Project) for _, entry := range entries { + var entryFinish time.Time endOfBeginDay := now.With(entry.Begin).EndOfDay() sameDayHours := decimal.NewFromInt(0) nextDayHours := decimal.NewFromInt(0) @@ -55,23 +56,29 @@ func NewCalendar(entries []Entry) (Calendar, error) { projects[projectId] = project } + if entry.Finish.IsZero() { + entryFinish = time.Now() + } else { + entryFinish = entry.Finish + } + /* * Apparently the activity end is on a new day. * This means we have to split the activity across two days. */ - if endOfBeginDay.Before(entry.Finish) == true { - startOfFinishDay := now.With(entry.Finish).BeginningOfDay() + if endOfBeginDay.Before(entryFinish) == true { + startOfFinishDay := now.With(entryFinish).BeginningOfDay() sameDayDuration := endOfBeginDay.Sub(entry.Begin) sameDay := sameDayDuration.Hours() sameDayHours = decimal.NewFromFloat(sameDay) - nextDayDuration := entry.Finish.Sub(startOfFinishDay) + nextDayDuration := entryFinish.Sub(startOfFinishDay) nextDay := nextDayDuration.Hours() nextDayHours = decimal.NewFromFloat(nextDay) } else { - sameDayDuration := entry.Finish.Sub(entry.Begin) + sameDayDuration := entryFinish.Sub(entry.Begin) sameDay := sameDayDuration.Hours() sameDayHours = decimal.NewFromFloat(sameDay) } @@ -97,7 +104,7 @@ func NewCalendar(entries []Entry) (Calendar, error) { } if nextDayHours.GreaterThan(decimal.NewFromInt(0)) { - month, weeknumber := GetISOWeekInMonth(entry.Finish) + month, weeknumber := GetISOWeekInMonth(entryFinish) month0 := month - 1 weeknumber0 := weeknumber - 1 weekday := entry.Begin.Weekday() diff --git a/z/entry.go b/z/entry.go index 3aec902..298f3d3 100644 --- a/z/entry.go +++ b/z/entry.go @@ -155,13 +155,40 @@ func (entry *Entry) GetOutputForFinish() (string) { func (entry *Entry) GetOutput(full bool) (string) { var output string = "" - trackDiff := entry.Finish.Sub(entry.Begin) + var entryFinish time.Time + var isRunning string = "" + + if entry.Finish.IsZero() { + entryFinish = time.Now() + isRunning = "[running]" + } else { + entryFinish = entry.Finish + } + + trackDiff := entryFinish.Sub(entry.Begin) trackDiffOut := time.Time{}.Add(trackDiff) if full == false { - output = fmt.Sprintf("%s %s on %s from %s to %s (%sh)", color.FgGray.Render(entry.ID), color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(entry.Finish.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(trackDiffOut.Format("15:04"))) + output = fmt.Sprintf("%s %s on %s from %s to %s (%sh) %s", + color.FgGray.Render(entry.ID), + color.FgLightWhite.Render(entry.Task), + color.FgLightWhite.Render(entry.Project), + color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), + color.FgLightWhite.Render(entryFinish.Format("2006-01-02 15:04 -0700")), + color.FgLightWhite.Render(trackDiffOut.Format("15:04")), + color.FgLightYellow.Render(isRunning), + ) } else { - output = fmt.Sprintf("%s\n %s on %s\n %sh from %s to %s\n\n Notes:\n %s\n", color.FgGray.Render(entry.ID), color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(trackDiffOut.Format("15:04")), color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(entry.Finish.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(strings.Replace(entry.Notes, "\n", "\n ", -1)) ) + output = fmt.Sprintf("%s\n %s on %s\n %sh from %s to %s %s\n\n Notes:\n %s\n", + color.FgGray.Render(entry.ID), + color.FgLightWhite.Render(entry.Task), + color.FgLightWhite.Render(entry.Project), + color.FgLightWhite.Render(trackDiffOut.Format("15:04")), + color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), + color.FgLightWhite.Render(entryFinish.Format("2006-01-02 15:04 -0700")), + color.FgLightYellow.Render(isRunning), + color.FgLightWhite.Render(strings.Replace(entry.Notes, "\n", "\n ", -1)), + ) } return output