Skip to content

Commit

Permalink
Fix and assert date locality
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 4, 2019
1 parent a3aab89 commit 51ee0f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
10 changes: 5 additions & 5 deletions date/iso_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ type Date struct {
}

func Today() Date {
return From(time.Now())
return From(time.Now().Local())
}

func From(time time.Time) Date {
return Date{time: time}
func From(t time.Time) Date {
return Date{time: t}
}

func Parse(date string) (Date, error) {
t, err := time.Parse(ISO8601, date)
func Parse(s string) (Date, error) {
t, err := time.ParseInLocation(ISO8601, s, time.Local)
if err != nil {
return Date{}, err
}
Expand Down
36 changes: 36 additions & 0 deletions date/iso_date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package date_test

import (
"github.com/leanovate/mite-go/date"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestDate_Today(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")

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

_, err = date.Parse("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))
actual := date.
From(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)).
Add(1, 1, 1)

assert.Equal(t, expected, actual)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
github.com/stretchr/testify v1.3.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/cheynewallace/tabby v1.1.0/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyr
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand Down Expand Up @@ -36,8 +37,11 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=
github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down

0 comments on commit 51ee0f9

Please sign in to comment.