tme

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

commit d56b7643c73c67460180b3af60f4e53917aa3bae
parent 4d8db75d7019b916b1b2c2cac917fbcb97f925b1
Author: Tomas Nemec <nemi@skaut.cz>
Date:   Tue, 21 Feb 2023 17:29:47 +0100

update

Diffstat:
Mcommand.go | 26++++++++++++++++++--------
Mtime_context.go | 5+++++
Mtime_context_test.go | 30++++++++++++++++++++++++++++++
3 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/command.go b/command.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strings" "time" ) @@ -45,7 +46,7 @@ func (c Command) add() { os.Exit(1) } - stop, err := c.entryTimeContext.ParseArg(stopArg) + stop, err := tryParseRelative(*c.entryTimeContext, start, stopArg) if err != nil { fmt.Fprintf(os.Stderr, "[stop] time (%s) could not be parsed\n", stopArg) os.Exit(1) @@ -105,22 +106,22 @@ func (c Command) stop() { groupArg, _ := c.nextArg() group := NewGroup(groupArg) + entry, err := c.repository.RunningEntry(group, c.entryTimeContext) + if err != nil { + fmt.Fprintf(os.Stderr, "no entry running in %q\n", groupArg) + os.Exit(1) + } + stop := time.Now() stopArg, err := c.nextArg() if err == nil { - stop, err = c.entryTimeContext.ParseArg(stopArg) + stop, err = tryParseRelative(*c.entryTimeContext, entry.TimeRange.Start, stopArg) if err != nil { fmt.Fprintf(os.Stderr, "[start] time (%s) could not be parsed\n", stopArg) os.Exit(1) } } - entry, err := c.repository.RunningEntry(group, c.entryTimeContext) - if err != nil { - fmt.Fprintf(os.Stderr, "no entry running in %q\n", groupArg) - os.Exit(1) - } - entry.Complete(stop) if c.repository.ExistsEntry(group, entry) { @@ -262,3 +263,12 @@ func formatEntry(group Group, entry Entry) { fmt.Printf("%s\t%s\t%v\t%s\n", groupPath, start, stop, duration) } + +func tryParseRelative(timeContext TimeContext, start time.Time, rawStop string) (stop time.Time, err error) { + if strings.IndexAny(rawStop, "+") == 0 { + stop, err = timeContext.ParseArgRelative(start, rawStop) + } else { + stop, err = timeContext.ParseArg(rawStop) + } + return stop, err +} diff --git a/time_context.go b/time_context.go @@ -23,6 +23,11 @@ func NewTimeToday() *TimeContext { return &TimeContext{time.Now()} } +func (et *TimeContext) ParseArgRelative(start time.Time, raw string) (time.Time, error) { + dur, err := time.ParseDuration(raw) + return start.Add(dur), err +} + func (et *TimeContext) ParseArg(raw string) (time.Time, error) { return et.ParseArgDir(raw, false) } diff --git a/time_context_test.go b/time_context_test.go @@ -42,3 +42,33 @@ func TestParseArg(t *testing.T) { check("5:5 5/05/2005", "5:5:0 5/5/2005") check("5:05 5/05/2005", "5:5:0 5/5/2005") } + +func TestParseRelative(t *testing.T) { + layout := "15:4:5 2/1/2006" + context, _ := time.Parse(layout, "1:1:1 1/1/2001") + var check = func(raw string, rel string, expected string) { + t.Helper() + t.Run("", func(b *testing.T) { + b.Helper() + tmeTime := NewTime(context) + expected, _ := time.Parse(layout, expected) + + start, err := tmeTime.ParseArg(raw) + if err != nil { + b.Fatal(err) + } + + parsed, err := tmeTime.ParseArgRelative(start, rel) + if err != nil { + b.Fatal(err) + } + + if !parsed.Equal(expected) { + b.Errorf("Parsed %v; Expected %v", parsed, expected) + } + }) + } + + check("5", "+1h", "6:0:0 1/1/2001") + check("5:5:5", "+1h5m", "6:10:5 1/1/2001") +}