From bb001321609c563b7471bbb6a3561c3cc80ea1af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Mon, 8 Apr 2019 10:59:04 +0200 Subject: [PATCH] Replace time.Duration with Minutes --- cmd/entries.go | 31 ++++++++++++++++--------------- cmd/tracker.go | 8 ++++---- mite/time_entry.go | 11 +++++------ mite/tracker.go | 21 +++++++++++---------- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/cmd/entries.go b/cmd/entries.go index 4d2b7c3..6b91ddd 100644 --- a/cmd/entries.go +++ b/cmd/entries.go @@ -8,7 +8,6 @@ import ( "github.com/leanovate/mite-go/mite" "github.com/spf13/cobra" "strings" - "time" ) var ( @@ -16,14 +15,14 @@ var ( listFrom string listOrder string createDate string - createDuration time.Duration + createMinutes string createNote string createProjectId string createServiceId string createActivity string editTimeEntryId string editDate string - editDuration string + editMinutes string editNote string editProjectId string editServiceId string @@ -34,10 +33,8 @@ var ( func init() { today := datetime.Today() defaultFrom := today.Add(0, 0, -7) - defaultDuration, err := time.ParseDuration("0m") - if err != nil { - panic(err) - } + defaultMinutes := datetime.NewMinutes(0).String() + // list entriesListCommand.Flags().StringVarP(&listTo, "to", "t", today.String(), "list only entries until date (in YYYY-MM-DD format)") entriesListCommand.Flags().StringVarP(&listFrom, "from", "f", defaultFrom.String(), "list only entries starting at date (in YYYY-MM-DD format)") @@ -45,7 +42,7 @@ func init() { entriesCommand.AddCommand(entriesListCommand) // create entriesCreateCommand.Flags().StringVarP(&createDate, "date", "D", today.String(), "day for which to create entry (in YYYY-MM-DD format)") - entriesCreateCommand.Flags().DurationVarP(&createDuration, "duration", "d", defaultDuration, "duration of entry (format examples: '1h15m' or '300m' or '6h')") + entriesCreateCommand.Flags().StringVarP(&createMinutes, "duration", "d", defaultMinutes, "duration of entry (format examples: '1h15m' or '300m' or '6h')") entriesCreateCommand.Flags().StringVarP(&createNote, "note", "n", "", "a note describing what was worked on") entriesCreateCommand.Flags().StringVarP(&createProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)") entriesCreateCommand.Flags().StringVarP(&createServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)") @@ -53,7 +50,7 @@ func init() { entriesCommand.AddCommand(entriesCreateCommand) // edit entriesEditCommand.Flags().StringVarP(&editDate, "date", "D", "", "day for which to edit entry (in YYYY-MM-DD format)") - entriesEditCommand.Flags().StringVarP(&editDuration, "duration", "d", "", "duration of entry (format examples: '1h15m' or '300m' or '6h')") + entriesEditCommand.Flags().StringVarP(&editMinutes, "duration", "d", "", "duration of entry (format examples: '1h15m' or '300m' or '6h')") entriesEditCommand.Flags().StringVarP(&editNote, "note", "n", "", "a note describing what was worked on") entriesEditCommand.Flags().StringVarP(&editTimeEntryId, "id", "i", "", "the time entry id to edit") entriesEditCommand.Flags().StringVarP(&editProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)") @@ -109,7 +106,7 @@ func printEntries(entries []*mite.TimeEntry) { shortenedNotes := fmt.Sprintf("%.50s", trimmedNotes) shortenedProject := fmt.Sprintf("%.25s", entry.ProjectName) shortenedService := fmt.Sprintf("%.25s", entry.ServiceName) - t.AddLine(entry.Id, shortenedNotes, entry.Date, entry.Duration.String(), shortenedProject, shortenedService) + t.AddLine(entry.Id, shortenedNotes, entry.Date, entry.Minutes.String(), shortenedProject, shortenedService) } t.Print() } @@ -128,10 +125,14 @@ var entriesCreateCommand = &cobra.Command{ if err != nil { return err } + cMinutes, err := datetime.ParseMinutes(createMinutes) + if err != nil { + return err + } timeEntry := mite.TimeEntryCommand{ Date: &cDate, - Duration: &createDuration, + Minutes: &cMinutes, Note: createNote, ProjectId: projectId, ServiceId: servicesId, @@ -181,7 +182,7 @@ var entriesEditCommand = &cobra.Command{ // use retrieved values as defaults timeEntry := mite.TimeEntryCommand{ Date: &entry.Date, - Duration: &entry.Duration, + Minutes: &entry.Minutes, Note: entry.Note, ProjectId: entry.ProjectId, ServiceId: entry.ServiceId, @@ -196,12 +197,12 @@ var entriesEditCommand = &cobra.Command{ timeEntry.Date = &eDate } - if editDuration != "" { - eDuration, err := time.ParseDuration(editDuration) + if editMinutes != "" { + eMinutes, err := datetime.ParseMinutes(editMinutes) if err != nil { return err } - timeEntry.Duration = &eDuration + timeEntry.Minutes = &eMinutes } if editNote != "" { diff --git a/cmd/tracker.go b/cmd/tracker.go index 3033e43..a532635 100644 --- a/cmd/tracker.go +++ b/cmd/tracker.go @@ -41,7 +41,7 @@ var trackerStatusCommand = &cobra.Command{ t := tabby.New() t.AddHeader("id", "time", "state", "since") - t.AddLine(tracking.Id, tracking.Duration, "tracking", tracking.Since) + t.AddLine(tracking.Id, tracking.Minutes, "tracking", tracking.Since) t.Print() return nil @@ -66,9 +66,9 @@ var trackerStartCommand = &cobra.Command{ t := tabby.New() t.AddHeader("id", "time", "state", "since") - t.AddLine(tracking.Id, tracking.Duration, "tracking", tracking.Since) + t.AddLine(tracking.Id, tracking.Minutes, "tracking", tracking.Since) if stopped != nil { - t.AddLine(stopped.Id, stopped.Duration, "stopped") + t.AddLine(stopped.Id, stopped.Minutes, "stopped") } t.Print() @@ -94,7 +94,7 @@ var trackerStopCommand = &cobra.Command{ t := tabby.New() t.AddHeader("id", "time", "state") - t.AddLine(stopped.Id, stopped.Duration, "stopped") + t.AddLine(stopped.Id, stopped.Minutes, "stopped") t.Print() return nil diff --git a/mite/time_entry.go b/mite/time_entry.go index b369024..b0d04ee 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -3,7 +3,6 @@ package mite import ( "fmt" "github.com/leanovate/mite-go/datetime" - "math" "net/url" "strconv" "time" @@ -11,7 +10,7 @@ import ( type TimeEntry struct { Id string - Duration time.Duration + Minutes datetime.Minutes Date datetime.LocalDate Note string Billable bool @@ -32,7 +31,7 @@ type TimeEntry struct { type TimeEntryCommand struct { Date *datetime.LocalDate - Duration *time.Duration + Minutes *datetime.Minutes Note string UserId string ProjectId string @@ -45,8 +44,8 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest { if c.Date != nil { r.TimeEntry.Date = c.Date.String() } - if c.Duration != nil { - r.TimeEntry.Minutes = int(math.Floor(math.Round(c.Duration.Minutes()))) // BOGUS + if c.Minutes != nil { + r.TimeEntry.Minutes = c.Minutes.Value() } r.TimeEntry.Note = c.Note r.TimeEntry.UserId = c.UserId @@ -123,7 +122,7 @@ func (r *timeEntryResponse) toTimeEntry() *TimeEntry { return &TimeEntry{ Id: strconv.Itoa(r.TimeEntry.Id), - Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute, + Minutes: datetime.NewMinutes(r.TimeEntry.Minutes), Date: d, Note: r.TimeEntry.Note, Billable: r.TimeEntry.Billable, diff --git a/mite/tracker.go b/mite/tracker.go index f64810a..97ed1d2 100644 --- a/mite/tracker.go +++ b/mite/tracker.go @@ -2,19 +2,20 @@ package mite import ( "fmt" + "github.com/leanovate/mite-go/datetime" "strconv" "time" ) type TrackingTimeEntry struct { - Id string - Duration time.Duration - Since time.Time + Id string + Minutes datetime.Minutes + Since time.Time } type StoppedTimeEntry struct { - Id string - Duration time.Duration + Id string + Minutes datetime.Minutes } type trackerResponse struct { @@ -37,9 +38,9 @@ func (r *trackerResponse) toTrackingTimeEntry() *TrackingTimeEntry { } return &TrackingTimeEntry{ - Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id), - Duration: time.Duration(r.Tracker.TrackingTimeEntry.Minutes) * time.Minute, - Since: r.Tracker.TrackingTimeEntry.Since, + Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id), + Minutes: datetime.NewMinutes(r.Tracker.TrackingTimeEntry.Minutes), + Since: r.Tracker.TrackingTimeEntry.Since, } } @@ -49,8 +50,8 @@ func (r *trackerResponse) toStoppedTimeEntry() *StoppedTimeEntry { } return &StoppedTimeEntry{ - Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id), - Duration: time.Duration(r.Tracker.StoppedTimeEntry.Minutes) * time.Minute, + Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id), + Minutes: datetime.NewMinutes(r.Tracker.StoppedTimeEntry.Minutes), } }