From 4f537be5c121129961ddaf74af004a9c2c00d973 Mon Sep 17 00:00:00 2001 From: Alex Guerrieri Date: Mon, 23 Nov 2015 18:04:12 +0100 Subject: Timezone fixes --- objects.go | 10 +++++++++- objects_test.go | 22 +++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/objects.go b/objects.go index 351a022..9397bc8 100644 --- a/objects.go +++ b/objects.go @@ -76,7 +76,15 @@ func (s *Signature) Decode(b []byte) { if c == ' ' || end { t, err := strconv.ParseInt(string(b[from:i]), 10, 64) if err == nil { - s.When = time.Unix(t, 0) + loc := time.UTC + ts := time.Unix(t, 0) + if len(b[i:]) >= 6 { + tl, err := time.Parse(" -0700", string(b[i:i+6])) + if err == nil { + loc = tl.Location() + } + } + s.When = ts.In(loc) } end = true } diff --git a/objects_test.go b/objects_test.go index 9bebe26..a51f372 100644 --- a/objects_test.go +++ b/objects_test.go @@ -1,6 +1,7 @@ package git import ( + "fmt" "io/ioutil" "time" @@ -42,7 +43,7 @@ func (s *ObjectsSuite) TestNewCommit(c *C) { c.Assert(commit.Author.Email, Equals, "mcuadros@gmail.com") c.Assert(commit.Author.Name, Equals, "Máximo Cuadros") - c.Assert(commit.Author.When.Unix(), Equals, int64(1427802434)) + c.Assert(commit.Author.When.Format(time.RFC3339), Equals, "2015-03-31T13:47:14+02:00") c.Assert(commit.Committer.Email, Equals, "mcuadros@gmail.com") c.Assert(commit.Message, Equals, "Merge pull request #1 from dripolles/feature\n\nCreating changelog\n") } @@ -92,17 +93,22 @@ func (s *ObjectsSuite) TestParseSignature(c *C) { `Foo Bar 1257894000 +0100`: { Name: "Foo Bar", Email: "foo@bar.com", - When: time.Unix(1257894000, 0), + When: MustParseTime("2009-11-11 00:00:00 +0100"), + }, + `Foo Bar 1257894000 -0700`: { + Name: "Foo Bar", + Email: "foo@bar.com", + When: MustParseTime("2009-11-10 16:00:00 -0700"), }, `Foo Bar <> 1257894000 +0100`: { Name: "Foo Bar", Email: "", - When: time.Unix(1257894000, 0), + When: MustParseTime("2009-11-11 00:00:00 +0100"), }, ` <> 1257894000`: { Name: "", Email: "", - When: time.Unix(1257894000, 0), + When: MustParseTime("2009-11-10 23:00:00 +0000"), }, `Foo Bar `: { Name: "Foo Bar", @@ -122,11 +128,17 @@ func (s *ObjectsSuite) TestParseSignature(c *C) { } for raw, exp := range cases { + fmt.Println("> testing", raw) got := &Signature{} got.Decode([]byte(raw)) c.Assert(got.Name, Equals, exp.Name) c.Assert(got.Email, Equals, exp.Email) - c.Assert(got.When.Unix(), Equals, exp.When.Unix()) + c.Assert(got.When.Format(time.RFC3339), Equals, exp.When.Format(time.RFC3339)) } } + +func MustParseTime(value string) time.Time { + t, _ := time.Parse("2006-01-02 15:04:05 -0700", value) + return t +} -- cgit