gime/cmd/time.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-01-19 18:56:31 +00:00
/*
2022-01-19 21:30:30 +00:00
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
2022-01-19 18:56:31 +00:00
*/
package cmd
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"github.com/spf13/cobra"
)
// timeCmd represents the time command
var timeCmd = &cobra.Command{
Use: "time",
Short: "A brief description of your command",
RunE: opShowTimers,
}
func init() {
rootCmd.AddCommand(timeCmd)
}
func opShowTimers(cmd *cobra.Command, args []string) error {
var err error
p := cli.Program{}
err = p.Initialize()
if err != nil {
return err
}
if err := p.LoadTimerList(); err != nil {
return err
}
list := p.GetFilteredTimerList(args)
var isActive bool
var total time.Duration
2023-01-12 12:04:17 +00:00
for _, v := range list.GetTimerSlice() {
2022-01-19 18:56:31 +00:00
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
isActive = true
}
total += dur
}
2022-10-04 17:37:11 +00:00
total = util.Round(total)
2022-01-19 18:56:31 +00:00
if isActive {
fmt.Printf("%.2f+\n", util.DurationToDecimal(total))
} else {
fmt.Printf("%.2f\n", util.DurationToDecimal(total))
}
return nil
}