diff --git a/commit.go b/commit.go index ebe2a2f..a9f4e1f 100644 --- a/commit.go +++ b/commit.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" "sync" + "time" cid "github.com/ipfs/go-cid" node "github.com/ipfs/go-ipld-format" @@ -41,10 +42,21 @@ type PersonInfo struct { } func (pi *PersonInfo) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]string{ + sec, err := strconv.ParseInt(pi.Date, 10, 64) + if err != nil { + return nil, err + } + zoneOffset, err := strconv.Atoi(pi.Timezone) + if err != nil { + return nil, err + } + hr, mm := zoneOffset/100, zoneOffset%100 + location := time.FixedZone("UTC", hr*60*60+mm*60) + date := time.Unix(sec, 0).In(location) + return json.Marshal(map[string]interface{}{ "name": pi.Name, "email": pi.Email, - "date": pi.Date + " " + pi.Timezone, + "date": date, }) } diff --git a/git_test.go b/git_test.go index 5ede191..de836c6 100644 --- a/git_test.go +++ b/git_test.go @@ -341,6 +341,14 @@ func TestParsePersonInfo(t *testing.T) { } assert(t, pi.String() == "Someone ") + + pi, err = parsePersonInfo([]byte("prefix Łukasz Magiera 1546187652 +0100")) + piJSON, err := pi.MarshalJSON() + assert(t, string(piJSON) == `{"date":"2018-12-30T17:34:12+01:00","email":"magik6k@users.noreply.github.com","name":"Łukasz Magiera"}`) + + pi, err = parsePersonInfo([]byte("prefix Sameer <11097096+sameer@users.noreply.github.com> 1545162499 -0500")) + piJSON, err = pi.MarshalJSON() + assert(t, string(piJSON) == `{"date":"2018-12-18T14:48:19-05:00","email":"11097096+sameer@users.noreply.github.com","name":"Sameer"}`) } func assert(t *testing.T, ok bool) {