Fixed #7, treating entry.Finished for running entries as time.Now()

This commit is contained in:
マリウス 2021-02-07 14:22:16 -04:00
parent 31014917ef
commit 6e0ecbb007
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F
2 changed files with 42 additions and 8 deletions

View file

@ -40,6 +40,7 @@ func NewCalendar(entries []Entry) (Calendar, error) {
projects := make(map[string]Project) projects := make(map[string]Project)
for _, entry := range entries { for _, entry := range entries {
var entryFinish time.Time
endOfBeginDay := now.With(entry.Begin).EndOfDay() endOfBeginDay := now.With(entry.Begin).EndOfDay()
sameDayHours := decimal.NewFromInt(0) sameDayHours := decimal.NewFromInt(0)
nextDayHours := decimal.NewFromInt(0) nextDayHours := decimal.NewFromInt(0)
@ -55,23 +56,29 @@ func NewCalendar(entries []Entry) (Calendar, error) {
projects[projectId] = project projects[projectId] = project
} }
if entry.Finish.IsZero() {
entryFinish = time.Now()
} else {
entryFinish = entry.Finish
}
/* /*
* Apparently the activity end is on a new day. * Apparently the activity end is on a new day.
* This means we have to split the activity across two days. * This means we have to split the activity across two days.
*/ */
if endOfBeginDay.Before(entry.Finish) == true { if endOfBeginDay.Before(entryFinish) == true {
startOfFinishDay := now.With(entry.Finish).BeginningOfDay() startOfFinishDay := now.With(entryFinish).BeginningOfDay()
sameDayDuration := endOfBeginDay.Sub(entry.Begin) sameDayDuration := endOfBeginDay.Sub(entry.Begin)
sameDay := sameDayDuration.Hours() sameDay := sameDayDuration.Hours()
sameDayHours = decimal.NewFromFloat(sameDay) sameDayHours = decimal.NewFromFloat(sameDay)
nextDayDuration := entry.Finish.Sub(startOfFinishDay) nextDayDuration := entryFinish.Sub(startOfFinishDay)
nextDay := nextDayDuration.Hours() nextDay := nextDayDuration.Hours()
nextDayHours = decimal.NewFromFloat(nextDay) nextDayHours = decimal.NewFromFloat(nextDay)
} else { } else {
sameDayDuration := entry.Finish.Sub(entry.Begin) sameDayDuration := entryFinish.Sub(entry.Begin)
sameDay := sameDayDuration.Hours() sameDay := sameDayDuration.Hours()
sameDayHours = decimal.NewFromFloat(sameDay) sameDayHours = decimal.NewFromFloat(sameDay)
} }
@ -97,7 +104,7 @@ func NewCalendar(entries []Entry) (Calendar, error) {
} }
if nextDayHours.GreaterThan(decimal.NewFromInt(0)) { if nextDayHours.GreaterThan(decimal.NewFromInt(0)) {
month, weeknumber := GetISOWeekInMonth(entry.Finish) month, weeknumber := GetISOWeekInMonth(entryFinish)
month0 := month - 1 month0 := month - 1
weeknumber0 := weeknumber - 1 weeknumber0 := weeknumber - 1
weekday := entry.Begin.Weekday() weekday := entry.Begin.Weekday()

View file

@ -155,13 +155,40 @@ func (entry *Entry) GetOutputForFinish() (string) {
func (entry *Entry) GetOutput(full bool) (string) { func (entry *Entry) GetOutput(full bool) (string) {
var output 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) trackDiffOut := time.Time{}.Add(trackDiff)
if full == false { 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 { } 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 return output