use HH:MM format by default everywhere

add --decimal option to toggle HH.ZZ format; ref #13
This commit is contained in:
Derek Stevens 2021-12-18 21:33:12 -07:00
parent 0df4a0bc2e
commit ba824325bc
No known key found for this signature in database
GPG key ID: 3B7FBC22144E6398
6 changed files with 43 additions and 12 deletions

View file

@ -161,7 +161,7 @@ func (calendar *Calendar) GetOutputForWeekCalendar(date time.Time, month int, we
bars = append(bars, bar) bars = append(bars, bar)
} }
output = fmt.Sprintf("CW %02d %s H\n", GetISOCalendarWeek(date), totalHours.StringFixed(2)) output = fmt.Sprintf("CW %02d %s H\n", GetISOCalendarWeek(date), fmtHours(totalHours))
for row := 0; row < len(bars[0]); row++ { for row := 0; row < len(bars[0]); row++ {
output = fmt.Sprintf("%s%2d │", output, ((6 - row) * 4)) output = fmt.Sprintf("%s%2d │", output, ((6 - row) * 4))
for col := 0; col < len(bars); col++ { for col := 0; col < len(bars); col++ {
@ -184,7 +184,7 @@ func (calendar *Calendar) GetOutputForDistribution() (string) {
for _, stat := range calendar.Distribution { for _, stat := range calendar.Distribution {
divided := stat.Hours.Div(calendar.TotalHours) divided := stat.Hours.Div(calendar.TotalHours)
percentage := divided.Mul(decimal.NewFromInt(100)) percentage := divided.Mul(decimal.NewFromInt(100))
hoursStr := stat.Hours.StringFixed(2) hoursStr := fmtHours(stat.Hours)
percentageStr := percentage.StringFixed(2) percentageStr := percentage.StringFixed(2)
dividedByBarLength := percentage.Div(decimal.NewFromInt(100)) dividedByBarLength := percentage.Div(decimal.NewFromInt(100))

View file

@ -141,9 +141,9 @@ func (entry *Entry) GetOutputForFinish() (string) {
var outputSuffix string = "" var outputSuffix string = ""
trackDiff := entry.Finish.Sub(entry.Begin) trackDiff := entry.Finish.Sub(entry.Begin)
trackDiffOut := time.Time{}.Add(trackDiff) taskDuration := fmtDuration(trackDiff)
outputSuffix = fmt.Sprintf(" for %sh", color.FgLightWhite.Render(trackDiffOut.Format("15:04"))) outputSuffix = fmt.Sprintf(" for %s h", color.FgLightWhite.Render(taskDuration))
if entry.Task != "" && entry.Project != "" { if entry.Task != "" && entry.Project != "" {
return fmt.Sprintf("%s finished tracking %s on %s%s\n", CharFinish, color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), outputSuffix) return fmt.Sprintf("%s finished tracking %s on %s%s\n", CharFinish, color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), outputSuffix)
@ -169,16 +169,16 @@ func (entry *Entry) GetOutput(full bool) (string) {
} }
trackDiff := entryFinish.Sub(entry.Begin) trackDiff := entryFinish.Sub(entry.Begin)
trackDiffOut := time.Time{}.Add(trackDiff) taskDuration := fmtDuration(trackDiff)
if full == false { if full == false {
output = fmt.Sprintf("%s %s on %s from %s to %s (%s h) %s", output = fmt.Sprintf("%s %s on %s from %s to %s (%s h) %s",
color.FgGray.Render(entry.ID), color.FgGray.Render(entry.ID),
color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Task),
color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(entry.Project),
color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), 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(entryFinish.Format("2006-01-02 15:04 -0700")),
color.FgLightWhite.Render(trackDiffOut.Format("15:04")), color.FgLightWhite.Render(taskDuration) ,
color.FgLightYellow.Render(isRunning), color.FgLightYellow.Render(isRunning),
) )
} else { } else {
@ -186,7 +186,7 @@ func (entry *Entry) GetOutput(full bool) (string) {
color.FgGray.Render(entry.ID), color.FgGray.Render(entry.ID),
color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Task),
color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(entry.Project),
color.FgLightWhite.Render(trackDiffOut.Format("15:04")), color.FgLightWhite.Render(taskDuration),
color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), 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(entryFinish.Format("2006-01-02 15:04 -0700")),
color.FgLightYellow.Render(isRunning), color.FgLightYellow.Render(isRunning),

View file

@ -71,6 +71,7 @@ func init() {
entryCmd.Flags().StringVarP(&project, "project", "p", "", "Update activity project") entryCmd.Flags().StringVarP(&project, "project", "p", "", "Update activity project")
entryCmd.Flags().StringVarP(&notes, "notes", "n", "", "Update activity notes") entryCmd.Flags().StringVarP(&notes, "notes", "n", "", "Update activity notes")
entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task") entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task")
entryCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
var err error var err error
database, err = InitDatabase() database, err = InitDatabase()

View file

@ -13,6 +13,7 @@ var listTotalTime bool
var listOnlyProjectsAndTasks bool var listOnlyProjectsAndTasks bool
var appendProjectIDToTask bool var appendProjectIDToTask bool
var listCmd = &cobra.Command{ var listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List activities", Short: "List activities",
@ -89,7 +90,7 @@ var listCmd = &cobra.Command{
} }
if listTotalTime == true { if listTotalTime == true {
fmt.Printf("\nTOTAL: %s H\n\n", totalHours.StringFixed(2)) fmt.Printf("\nTOTAL: %s H\n\n", fmtHours(totalHours));
} }
return return
}, },
@ -101,6 +102,7 @@ func init() {
listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until") listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until")
listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed") listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed")
listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed") listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed")
listCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
listCmd.Flags().BoolVar(&listTotalTime, "total", false, "Show total time of hours for listed activities") listCmd.Flags().BoolVar(&listTotalTime, "total", false, "Show total time of hours for listed activities")
listCmd.Flags().BoolVar(&listOnlyProjectsAndTasks, "only-projects-and-tasks", false, "Only list projects and their tasks, no entries") listCmd.Flags().BoolVar(&listOnlyProjectsAndTasks, "only-projects-and-tasks", false, "Only list projects and their tasks, no entries")
listCmd.Flags().BoolVar(&appendProjectIDToTask, "append-project-id-to-task", false, "Append project ID to tasks in the list") listCmd.Flags().BoolVar(&appendProjectIDToTask, "append-project-id-to-task", false, "Append project ID to tasks in the list")

View file

@ -51,7 +51,7 @@ var statsCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(statsCmd) rootCmd.AddCommand(statsCmd)
statsCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
var err error var err error
database, err = InitDatabase() database, err = InitDatabase()
if err != nil { if err != nil {

28
z/util.go Normal file
View file

@ -0,0 +1,28 @@
package z
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
var fractional bool
func fmtDuration(dur time.Duration) (string) {
return fmtHours(decimal.NewFromFloat(dur.Hours()))
}
func fmtHours(hours decimal.Decimal) (string) {
if fractional {
return hours.StringFixed(2)
} else {
return fmt.Sprintf(
"%s:%02s",
hours.Floor(), // hours
hours.Sub(hours.Floor()).
Mul(decimal.NewFromFloat(.6)).
Mul(decimal.NewFromInt(100)).
Floor())
}
}