diff --git a/cmd/entries.go b/cmd/entries.go index 4fc83c8..a858c30 100644 --- a/cmd/entries.go +++ b/cmd/entries.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/cheynewallace/tabby" + "github.com/leanovate/mite-go/date" "github.com/leanovate/mite-go/mite" "github.com/spf13/cobra" "strings" @@ -28,19 +29,19 @@ var ( ) func init() { - now := time.Now() - defaultFrom := now.AddDate(0, 0, -7) + today := date.Today() + defaultFrom := today.Add(0, 0, -7) defaultDuration, err := time.ParseDuration("0m") if err != nil { panic(err) } // list - entriesListCommand.Flags().StringVarP(&listTo, "to", "t", now.Format("2006-01-02"), "list only entries until date (in YYYY-MM-DD format)") - entriesListCommand.Flags().StringVarP(&listFrom, "from", "f", defaultFrom.Format("2006-01-02"), "list only entries starting at date (in YYYY-MM-DD format)") + 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)") entriesListCommand.Flags().StringVarP(&listOrder, "order", "o", "asc", "list only entries starting at date (in YYYY-MM-DD format)") entriesCommand.AddCommand(entriesListCommand) // create - entriesCreateCommand.Flags().StringVarP(&createDate, "date", "D", now.Format("2006-01-02"), "day for which to create entry (in YYYY-MM-DD format)") + 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(&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)") @@ -69,11 +70,11 @@ var entriesListCommand = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { direction := listOrder - to, err := time.Parse("2006-01-02", listTo) + to, err := date.Parse(listTo) if err != nil { return err } - from, err := time.Parse("2006-01-02", listFrom) + from, err := date.Parse(listFrom) if err != nil { return err } @@ -121,7 +122,7 @@ var entriesCreateCommand = &cobra.Command{ return errors.New("please set both the project AND service id (either via arguments or config)") } - cDate, err := time.Parse("2006-01-02", createDate) + cDate, err := date.Parse(createDate) if err != nil { return err } diff --git a/date/date.go b/date/date.go new file mode 100644 index 0000000..ac478a7 --- /dev/null +++ b/date/date.go @@ -0,0 +1,34 @@ +package date + +import "time" + +const ISO8601 = "2006-01-02" + +type Date struct { + time time.Time +} + +func Today() Date { + return From(time.Now()) +} + +func From(time time.Time) Date { + return Date{time: time} +} + +func Parse(date string) (Date, error) { + t, err := time.Parse(ISO8601, date) + if err != nil { + return Date{}, err + } + + return From(t), nil +} + +func (d Date) Add(years int, months int, days int) Date { + return Date{time: d.time.AddDate(years, months, days)} +} + +func (d Date) String() string { + return d.time.Format(ISO8601) +} diff --git a/mite/api.go b/mite/api.go index dadebfe..496e29c 100644 --- a/mite/api.go +++ b/mite/api.go @@ -8,7 +8,6 @@ import ( "net/url" ) -const layout = "2006-01-02" const contentType = "application/json" const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" diff --git a/mite/time_entry.go b/mite/time_entry.go index 14cac0a..b582bf2 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -2,6 +2,7 @@ package mite import ( "fmt" + "github.com/leanovate/mite-go/date" "math" "net/url" "time" @@ -10,8 +11,8 @@ import ( type TimeEntry struct { Id string Note string + Date date.Date Duration time.Duration - Date time.Time ProjectId string ProjectName string ServiceId string @@ -19,7 +20,7 @@ type TimeEntry struct { } type TimeEntryCommand struct { - Date *time.Time + Date *date.Date Duration *time.Duration Note string ProjectId string @@ -29,7 +30,7 @@ type TimeEntryCommand struct { func (c *TimeEntryCommand) toRequest() *timeEntryRequest { r := &timeEntryRequest{} if c.Date != nil { - r.TimeEntry.Date = c.Date.Format(layout) + r.TimeEntry.Date = c.Date.String() } if c.Duration != nil { r.TimeEntry.Minutes = int(math.Floor(math.Round(c.Duration.Minutes()))) // BOGUS @@ -48,8 +49,8 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest { } type TimeEntryQuery struct { - From *time.Time - To *time.Time + From *date.Date + To *date.Date Direction string } @@ -57,10 +58,10 @@ func (q *TimeEntryQuery) toValues() url.Values { v := url.Values{} if q != nil { if q.From != nil { - v.Add("from", q.From.Format(layout)) + v.Add("from", q.From.String()) } if q.To != nil { - v.Add("to", q.To.Format(layout)) + v.Add("to", q.To.String()) } if q.Direction != "" { v.Add("direction", q.Direction) @@ -94,7 +95,7 @@ type timeEntryResponse struct { } func (r *timeEntryResponse) ToTimeEntry() *TimeEntry { - date, err := time.Parse(layout, r.TimeEntry.Date) + d, err := date.Parse(r.TimeEntry.Date) if err != nil { panic(err) } @@ -103,7 +104,7 @@ func (r *timeEntryResponse) ToTimeEntry() *TimeEntry { Id: fmt.Sprintf("%d", r.TimeEntry.Id), Note: r.TimeEntry.Note, Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute, - Date: date, + Date: d, ProjectId: fmt.Sprintf("%d", r.TimeEntry.ProjectId), ProjectName: r.TimeEntry.ProjectName, ServiceId: fmt.Sprintf("%d", r.TimeEntry.ServiceId),