Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 5c8fff7

Browse files
committed
Merge pull request #6 from klaidliadon/v2.0.0
Timezone fixes
2 parents caab43e + 4f537be commit 5c8fff7

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

objects.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ func (s *Signature) Decode(b []byte) {
7676
if c == ' ' || end {
7777
t, err := strconv.ParseInt(string(b[from:i]), 10, 64)
7878
if err == nil {
79-
s.When = time.Unix(t, 0)
79+
loc := time.UTC
80+
ts := time.Unix(t, 0)
81+
if len(b[i:]) >= 6 {
82+
tl, err := time.Parse(" -0700", string(b[i:i+6]))
83+
if err == nil {
84+
loc = tl.Location()
85+
}
86+
}
87+
s.When = ts.In(loc)
8088
}
8189
end = true
8290
}

objects_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"time"
67

@@ -42,7 +43,7 @@ func (s *ObjectsSuite) TestNewCommit(c *C) {
4243

4344
c.Assert(commit.Author.Email, Equals, "[email protected]")
4445
c.Assert(commit.Author.Name, Equals, "Máximo Cuadros")
45-
c.Assert(commit.Author.When.Unix(), Equals, int64(1427802434))
46+
c.Assert(commit.Author.When.Format(time.RFC3339), Equals, "2015-03-31T13:47:14+02:00")
4647
c.Assert(commit.Committer.Email, Equals, "[email protected]")
4748
c.Assert(commit.Message, Equals, "Merge pull request #1 from dripolles/feature\n\nCreating changelog\n")
4849
}
@@ -92,17 +93,22 @@ func (s *ObjectsSuite) TestParseSignature(c *C) {
9293
`Foo Bar <[email protected]> 1257894000 +0100`: {
9394
Name: "Foo Bar",
9495
95-
When: time.Unix(1257894000, 0),
96+
When: MustParseTime("2009-11-11 00:00:00 +0100"),
97+
},
98+
`Foo Bar <[email protected]> 1257894000 -0700`: {
99+
Name: "Foo Bar",
100+
101+
When: MustParseTime("2009-11-10 16:00:00 -0700"),
96102
},
97103
`Foo Bar <> 1257894000 +0100`: {
98104
Name: "Foo Bar",
99105
Email: "",
100-
When: time.Unix(1257894000, 0),
106+
When: MustParseTime("2009-11-11 00:00:00 +0100"),
101107
},
102108
` <> 1257894000`: {
103109
Name: "",
104110
Email: "",
105-
When: time.Unix(1257894000, 0),
111+
When: MustParseTime("2009-11-10 23:00:00 +0000"),
106112
},
107113
`Foo Bar <[email protected]>`: {
108114
Name: "Foo Bar",
@@ -122,11 +128,17 @@ func (s *ObjectsSuite) TestParseSignature(c *C) {
122128
}
123129

124130
for raw, exp := range cases {
131+
fmt.Println("> testing", raw)
125132
got := &Signature{}
126133
got.Decode([]byte(raw))
127134

128135
c.Assert(got.Name, Equals, exp.Name)
129136
c.Assert(got.Email, Equals, exp.Email)
130-
c.Assert(got.When.Unix(), Equals, exp.When.Unix())
137+
c.Assert(got.When.Format(time.RFC3339), Equals, exp.When.Format(time.RFC3339))
131138
}
132139
}
140+
141+
func MustParseTime(value string) time.Time {
142+
t, _ := time.Parse("2006-01-02 15:04:05 -0700", value)
143+
return t
144+
}

0 commit comments

Comments
 (0)