Add rounding direction

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

View File

@ -53,7 +53,7 @@ func opI3Status(cmd *cobra.Command, args []string) error {
} }
var text string var text string
if wrk.FinishDate.IsZero() { if wrk.FinishDate.IsZero() {
wrkDur := wrk.Duration().Round(time.Minute * 15) wrkDur := util.Round(wrk.Duration())
hrs := int(wrkDur.Hours()) hrs := int(wrkDur.Hours())
mins := int(wrkDur.Minutes()) - hrs*60 mins := int(wrkDur.Minutes()) - hrs*60
if hrs > 0 { if hrs > 0 {
@ -99,7 +99,7 @@ func opI3Status(cmd *cobra.Command, args []string) error {
} }
total += dur total += dur
} }
total = total.Round(util.GetRoundToDuration()) total = util.Round(total)
if isActive { if isActive {
return fmt.Sprintf("%.2f+", util.DurationToDecimal(total)) return fmt.Sprintf("%.2f+", util.DurationToDecimal(total))
} else { } else {

View File

@ -89,7 +89,7 @@ func opListTimers(cmd *cobra.Command, args []string) error {
dayStr = v.StartDate.Format("2006/01/02") dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr { if dayStr != oldDayStr {
// TODO: // TODO:
wrkDur := dayTotals[dayStr].Round(util.GetRoundToDuration()) wrkDur := util.Round(dayTotals[dayStr])
fmtStr := dayStr + " ( %.2f )\n" fmtStr := dayStr + " ( %.2f )\n"
fmt.Printf(fmtStr, util.DurationToDecimal(wrkDur)) fmt.Printf(fmtStr, util.DurationToDecimal(wrkDur))
} }

View File

@ -44,7 +44,8 @@ func opStatus(cmd *cobra.Command, args []string) error {
currDur += v.Duration() 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)) fmt.Printf("%s ( %.2f hrs )\n", time.Now().Format(time.Stamp), util.DurationToDecimal(d))
for _, v := range *p.TimerList.GetActiveTimers() { for _, v := range *p.TimerList.GetActiveTimers() {
fmt.Println(util.TimerToFriendlyString(v)) fmt.Println(util.TimerToFriendlyString(v))

View File

@ -45,7 +45,7 @@ func opShowTimers(cmd *cobra.Command, args []string) error {
} }
total += dur total += dur
} }
total = total.Round(util.GetRoundToDuration()) total = util.Round(total)
if isActive { if isActive {
fmt.Printf("%.2f+\n", util.DurationToDecimal(total)) fmt.Printf("%.2f+\n", util.DurationToDecimal(total))
} else { } else {

View File

@ -12,6 +12,12 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const (
ROUND_UP = 1
ROUND_EITHER = 0
ROUND_DOWN = -1
)
func PromptUser(text string) string { func PromptUser(text string) string {
var resp string var resp string
fmt.Println(text) fmt.Println(text)
@ -57,6 +63,34 @@ func TimerToString(t *timertxt.Timer) string {
return ret 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 { func GetRoundToDuration() time.Duration {
var dur time.Duration var dur time.Duration
dur, _ = time.ParseDuration(viper.GetString("roundto")) dur, _ = time.ParseDuration(viper.GetString("roundto"))
@ -180,7 +214,7 @@ func TimerToFriendlyString(t *timertxt.Timer) string {
} else { } else {
dur = t.FinishDate.Sub(t.StartDate) 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)) return fmt.Sprintf("% 2d. %s - %s [ %s] [ %s] [ %s] %s ( %.2f )", t.Id, start, end, contexts, projects, tags, t.Notes, DurationToDecimal(dur))
} }