From 51ee0f9717fff7fff9fb2d7a4b573d9ab2680889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Thu, 4 Apr 2019 12:18:58 +0200 Subject: [PATCH] Fix and assert date locality --- date/iso_date.go | 10 +++++----- date/iso_date_test.go | 36 ++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 4 ++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 date/iso_date_test.go diff --git a/date/iso_date.go b/date/iso_date.go index ac478a7..930c083 100644 --- a/date/iso_date.go +++ b/date/iso_date.go @@ -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 } diff --git a/date/iso_date_test.go b/date/iso_date_test.go new file mode 100644 index 0000000..6e94330 --- /dev/null +++ b/date/iso_date_test.go @@ -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) +} diff --git a/go.mod b/go.mod index ec1560a..2267d6d 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 00f95f8..789ea1a 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=