Skip to content

Commit

Permalink
Refactor LocalDate to canonical method names
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 5, 2019
1 parent 9c693da commit f0a96d0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
8 changes: 4 additions & 4 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ var entriesListCommand = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
direction := listOrder

to, err := date.Parse(listTo)
to, err := date.ParseLocalDate(listTo)
if err != nil {
return err
}
from, err := date.Parse(listFrom)
from, err := date.ParseLocalDate(listFrom)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,7 +124,7 @@ var entriesCreateCommand = &cobra.Command{
return errors.New("please set both the project AND service id (either via arguments or config)")
}

cDate, err := date.Parse(createDate)
cDate, err := date.ParseLocalDate(createDate)
if err != nil {
return err
}
Expand Down Expand Up @@ -189,7 +189,7 @@ var entriesEditCommand = &cobra.Command{

// override only fields affected by set parameters of edit
if editDate != "" {
eDate, err := date.Parse(editDate)
eDate, err := date.ParseLocalDate(editDate)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions date/local_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ type LocalDate struct {
time time.Time
}

func Today() LocalDate {
return From(time.Now().Local())
func NewLocalDate(t time.Time) LocalDate {
return LocalDate{time: t}
}

func From(t time.Time) LocalDate {
return LocalDate{time: t}
func Today() LocalDate {
return NewLocalDate(time.Now().Local())
}

func Parse(s string) (LocalDate, error) {
func ParseLocalDate(s string) (LocalDate, error) {
t, err := time.ParseInLocation(ISO8601, s, time.Local)
if err != nil {
return LocalDate{}, err
}

return From(t), nil
return NewLocalDate(t), nil
}

func (d LocalDate) Add(years int, months int, days int) LocalDate {
Expand Down
23 changes: 15 additions & 8 deletions date/local_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,37 @@ import (
"time"
)

func TestDate_Today(t *testing.T) {
func TestToday(t *testing.T) {
expected := time.Now().Local().Format("2006-01-02")
actual := date.Today().String()

assert.Equal(t, expected, actual)
}

func TestDate_Parse(t *testing.T) {
expected := date.From(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local))
actual, err := date.Parse("1970-01-01")
func TestParseLocalDate(t *testing.T) {
expected := date.NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local))
actual, err := date.ParseLocalDate("1970-01-01")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

_, err = date.Parse("1970-01-01T00:00:00Z")
_, err = date.ParseLocalDate("1970-01-01T00:00:00Z")

assert.IsType(t, &time.ParseError{}, err)
}

func TestDate_Add(t *testing.T) {
expected := date.From(time.Date(1971, time.February, 2, 0, 0, 0, 0, time.Local))
func TestLocalDate_Add(t *testing.T) {
expected := date.NewLocalDate(time.Date(1971, time.February, 2, 0, 0, 0, 0, time.Local))
actual := date.
From(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)).
NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)).
Add(1, 1, 1)

assert.Equal(t, expected, actual)
}

func TestLocalDate_String(t *testing.T) {
expected := "1970-01-01"
actual := date.NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)).String()

assert.Equal(t, expected, actual)
}
2 changes: 1 addition & 1 deletion mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type timeEntryResponse struct {
}

func (r *timeEntryResponse) toTimeEntry() *TimeEntry {
d, err := date.Parse(r.TimeEntry.Date)
d, err := date.ParseLocalDate(r.TimeEntry.Date)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit f0a96d0

Please sign in to comment.