tme

Toggl like Time Manager
git clone git://gtms.dev:tme
Log | Files | Refs

commit 1d0b0df80e65527e655fd5d2b40fca43ecb6c0e9
parent f89d3751b16fbf52582c3a1fe588d0156f1a21bb
Author: Tomas Nemec <nemi@skaut.cz>
Date:   Sat, 11 Feb 2023 01:29:21 +0100

feat: report since and until

Diffstat:
Mcommand.go | 33++++++++++++++++++++++++---------
Mtme/time.go | 50++++++++++++++++++++++++++++++++++++--------------
2 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/command.go b/command.go @@ -190,10 +190,16 @@ func (c Command) lsr() { func (c Command) report() { if len(c.args) < 1 || len(c.args) > 2 { - fmt.Fprintln(os.Stderr, "report <since> [<group>]") + fmt.Fprintln(os.Stderr, "report <since> [<until>]") os.Exit(1) } + // TODO(tms) 11.02.23: make as parameter --group + groupPath := "" + // if len(c.args) == 2 { + // groupPath, _ = c.nextArg() + // } + sinceArg, _ := c.nextArg() sinceTime, err := c.entryTime.ParseArg(sinceArg) if err != nil { @@ -201,9 +207,14 @@ func (c Command) report() { os.Exit(1) } - groupPath := "" - if len(c.args) == 1 { - groupPath, _ = c.nextArg() + untilTime := time.Now() + untilArgs, err := c.nextArg() + if err == nil { + untilTime, err = c.entryTime.ParseArgRight(untilArgs) + if err != nil { + fmt.Fprintf(os.Stderr, "[until] time (%s) could not be parsed\n", sinceArg) + os.Exit(1) + } } groups, err := groupsRecursive(c.rootPath, groupPath) @@ -212,7 +223,8 @@ func (c Command) report() { os.Exit(1) } - var duration time.Duration + var atLeastOneEntry bool + var totalDuration time.Duration for _, group := range groups { entries, err := group.ListCompleted(c.entryTime) if err != nil { @@ -221,15 +233,17 @@ func (c Command) report() { } for _, entry := range entries { - if entry.Start.After(sinceTime) || entry.Start.Equal(sinceTime) { - duration += entry.Duration() + if (entry.Start.After(sinceTime) || entry.Start.Equal(sinceTime)) && + (entry.Stop.Before(untilTime) || entry.Stop.Equal(untilTime)) { + atLeastOneEntry = true + totalDuration += entry.Duration() formatEntry(group, entry) } } } - if duration > 0 { - fmt.Printf("%s\n", duration.Round(time.Second)) + if atLeastOneEntry { + fmt.Printf("%s\n", totalDuration.Round(time.Second)) } } @@ -271,6 +285,7 @@ func groupsRecursive(rootPath string, groupPath string) ([]tme.Group, error) { return nil } groupPath := strings.TrimPrefix(path, rootPath) + groupPath = strings.TrimPrefix(groupPath, string(os.PathSeparator)) group := tme.NewGroup(rootPath, groupPath) groups = append(groups, group) } diff --git a/tme/time.go b/tme/time.go @@ -23,14 +23,27 @@ func NewTimeToday() *Time { return &Time{time.Now()} } -// TODO(tms) 11.02.23: Simplify func (et *Time) ParseArg(raw string) (time.Time, error) { + return et.ParseArgDir(raw, false) +} + +func (et *Time) ParseArgRight(raw string) (time.Time, error) { + return et.ParseArgDir(raw, true) +} + +// TODO(tms) 11.02.23: Simplify +func (et *Time) ParseArgDir(raw string, shiftRight bool) (time.Time, error) { parts := strings.Split(raw, " ") hour := et.context.Hour() min := et.context.Minute() sec := et.context.Second() year, mon, day := et.context.Date() + zeroTime := 0 + if shiftRight { + zeroTime = 59 + } + var parsed bool part := parts[0] @@ -38,8 +51,8 @@ func (et *Time) ParseArg(raw string) (time.Time, error) { if err == nil { parsed = true hour = t.Hour() - min = 0 - sec = 0 + min = zeroTime + sec = zeroTime } t, err = time.Parse("15:4", part) @@ -47,7 +60,7 @@ func (et *Time) ParseArg(raw string) (time.Time, error) { parsed = true hour = t.Hour() min = t.Minute() - sec = 0 + sec = zeroTime } t, err = time.Parse("15:4:5", part) @@ -61,9 +74,9 @@ func (et *Time) ParseArg(raw string) (time.Time, error) { t, err = time.Parse("2/1", part) if err == nil { parsed = true - hour = 0 - min = 0 - sec = 0 + hour = zeroTime + min = zeroTime + sec = zeroTime day = t.Day() mon = t.Month() year = et.context.Year() @@ -72,10 +85,19 @@ func (et *Time) ParseArg(raw string) (time.Time, error) { t, err = time.Parse("1/2006", part) if err == nil { parsed = true - hour = 0 - min = 0 - sec = 0 - day = 1 + hour = zeroTime + min = zeroTime + sec = zeroTime + + zeroDay := 1 + firstOfMonth := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, et.context.Location()) + lastOfMonth := firstOfMonth.AddDate(0, 1, -1) + if shiftRight { + zeroDay = lastOfMonth.Day() + } + + day = zeroDay + mon = t.Month() year = t.Year() } @@ -83,9 +105,9 @@ func (et *Time) ParseArg(raw string) (time.Time, error) { t, err = time.Parse("2/1/2006", part) if err == nil { parsed = true - hour = 0 - min = 0 - sec = 0 + hour = zeroTime + min = zeroTime + sec = zeroTime day = t.Day() mon = t.Month() year = t.Year()