From a8c1812bbfc757a7c6e0f2ef21b7d244c1bd6465 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 4 Oct 2022 12:37:11 -0500 Subject: [PATCH] Add rounding direction --- cmd/i3status.go | 4 ++-- cmd/list.go | 2 +- cmd/status.go | 3 ++- cmd/time.go | 2 +- util/helpers.go | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/cmd/i3status.go b/cmd/i3status.go index 0bd748e..46dd643 100644 --- a/cmd/i3status.go +++ b/cmd/i3status.go @@ -53,7 +53,7 @@ func opI3Status(cmd *cobra.Command, args []string) error { } var text string if wrk.FinishDate.IsZero() { - wrkDur := wrk.Duration().Round(time.Minute * 15) + wrkDur := util.Round(wrk.Duration()) hrs := int(wrkDur.Hours()) mins := int(wrkDur.Minutes()) - hrs*60 if hrs > 0 { @@ -99,7 +99,7 @@ func opI3Status(cmd *cobra.Command, args []string) error { } total += dur } - total = total.Round(util.GetRoundToDuration()) + total = util.Round(total) if isActive { return fmt.Sprintf("%.2f+", util.DurationToDecimal(total)) } else { diff --git a/cmd/list.go b/cmd/list.go index c60d1c3..b9730ea 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -89,7 +89,7 @@ func opListTimers(cmd *cobra.Command, args []string) error { dayStr = v.StartDate.Format("2006/01/02") if dayStr != oldDayStr { // TODO: - wrkDur := dayTotals[dayStr].Round(util.GetRoundToDuration()) + wrkDur := util.Round(dayTotals[dayStr]) fmtStr := dayStr + " ( %.2f )\n" fmt.Printf(fmtStr, util.DurationToDecimal(wrkDur)) } diff --git a/cmd/status.go b/cmd/status.go index c95aead..760bf94 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -44,7 +44,8 @@ func opStatus(cmd *cobra.Command, args []string) error { currDur += v.Duration() } } - d := currDur.Round(util.GetRoundToDuration()) + d := util.Round(currDur) + fmt.Printf("%s ( %.2f hrs )\n", time.Now().Format(time.Stamp), util.DurationToDecimal(d)) for _, v := range *p.TimerList.GetActiveTimers() { fmt.Println(util.TimerToFriendlyString(v)) diff --git a/cmd/time.go b/cmd/time.go index e5c0342..4df246b 100644 --- a/cmd/time.go +++ b/cmd/time.go @@ -45,7 +45,7 @@ func opShowTimers(cmd *cobra.Command, args []string) error { } total += dur } - total = total.Round(util.GetRoundToDuration()) + total = util.Round(total) if isActive { fmt.Printf("%.2f+\n", util.DurationToDecimal(total)) } else { diff --git a/util/helpers.go b/util/helpers.go index a9ce052..fdb7ca7 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -12,6 +12,12 @@ import ( "github.com/spf13/viper" ) +const ( + ROUND_UP = 1 + ROUND_EITHER = 0 + ROUND_DOWN = -1 +) + func PromptUser(text string) string { var resp string fmt.Println(text) @@ -57,6 +63,34 @@ func TimerToString(t *timertxt.Timer) string { return ret } +func Round(dur time.Duration) time.Duration { + roundDur := GetRoundToDuration() + wrk := dur.Round(roundDur) + switch GetRoundDirection() { + case ROUND_UP: + if wrk < dur { + return wrk + roundDur + } + case ROUND_DOWN: + if wrk > dur { + return wrk - roundDur + } + } + return wrk +} + +func GetRoundDirection() int { + dir := viper.GetString("round") + switch dir { + case "up": + return ROUND_UP + case "down": + return ROUND_DOWN + default: + return ROUND_EITHER + } +} + func GetRoundToDuration() time.Duration { var dur time.Duration dur, _ = time.ParseDuration(viper.GetString("roundto")) @@ -180,7 +214,7 @@ func TimerToFriendlyString(t *timertxt.Timer) string { } else { dur = t.FinishDate.Sub(t.StartDate) } - dur = dur.Round(GetRoundToDuration()) + dur = Round(dur) return fmt.Sprintf("% 2d. %s - %s [ %s] [ %s] [ %s] %s ( %.2f )", t.Id, start, end, contexts, projects, tags, t.Notes, DurationToDecimal(dur)) }