Skip to content

Commit

Permalink
Use date abstraction everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent 1ead21a commit 11ce285
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
17 changes: 9 additions & 8 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,19 +23,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)")
Expand All @@ -55,11 +56,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
}
Expand Down Expand Up @@ -107,7 +108,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
}
Expand Down
6 changes: 3 additions & 3 deletions date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package date

import "time"

const layout = "2006-01-02"
const ISO8601 = "2006-01-02"

type Date struct {
time time.Time
Expand All @@ -17,7 +17,7 @@ func From(time time.Time) Date {
}

func Parse(date string) (Date, error) {
t, err := time.Parse(layout, date)
t, err := time.Parse(ISO8601, date)
if err != nil {
return Date{}, err
}
Expand All @@ -30,5 +30,5 @@ func (d Date) Add(years int, months int, days int) Date {
}

func (d Date) String() string {
return d.time.Format(layout)
return d.time.Format(ISO8601)
}
1 change: 0 additions & 1 deletion mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand Down
19 changes: 10 additions & 9 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mite

import (
"fmt"
"github.com/leanovate/mite-go/date"
"math"
"net/url"
"time"
Expand All @@ -10,14 +11,14 @@ import (
type TimeEntry struct {
Id string
Note string
Date date.Date
Duration time.Duration
Date time.Time
ProjectName string
ServiceName string
}

type TimeEntryCommand struct {
Date *time.Time
Date *date.Date
Duration *time.Duration
Note string
ProjectId string
Expand All @@ -27,7 +28,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
Expand All @@ -46,19 +47,19 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
}

type TimeEntryQuery struct {
From *time.Time
To *time.Time
From *date.Date
To *date.Date
Direction string
}

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)
Expand Down Expand Up @@ -90,7 +91,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)
}
Expand All @@ -99,7 +100,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,
ProjectName: r.TimeEntry.ProjectName,
ServiceName: r.TimeEntry.ServiceName,
}
Expand Down

0 comments on commit 11ce285

Please sign in to comment.