Add rounding direction

This commit is contained in:
2022-10-04 12:37:11 -05:00
parent b0f5e928d9
commit a8c1812bbf
5 changed files with 41 additions and 6 deletions

View File

@@ -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))
}