Skip to content

Commit

Permalink
Use RFC3339 to format date in PersonInfo JSON, fixes ipfs#16
Browse files Browse the repository at this point in the history
  • Loading branch information
sameer committed Dec 30, 2018
1 parent 705872d commit 1c3720b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strconv"
"sync"
"time"

cid "github.com/ipfs/go-cid"
node "github.com/ipfs/go-ipld-format"
Expand Down Expand Up @@ -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.ParseInt(pi.Timezone, 10, 64)
if err != nil {
return nil, err
}
hr, mm := zoneOffset/100, zoneOffset%100
zoneOffsetDuration := time.Duration(mm)*time.Minute + time.Duration(hr)*time.Hour
date := time.Unix(sec, 0).Add(zoneOffsetDuration)
return json.Marshal(map[string]interface{}{
"name": pi.Name,
"email": pi.Email,
"date": pi.Date + " " + pi.Timezone,
"date": date,
})
}

Expand Down
4 changes: 4 additions & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ func TestParsePersonInfo(t *testing.T) {
}

assert(t, pi.String() == "Someone <some.one@some.where>")

pi, err = parsePersonInfo([]byte("prefix Łukasz Magiera <magik6k@users.noreply.github.com> 1546187652 +0100"))
piJSON, err := pi.MarshalJSON()
assert(t, string(piJSON) == `{"date":"2018-12-30T12:34:12-05:00","email":"magik6k@users.noreply.github.com","name":"Łukasz Magiera"}`)
}

func assert(t *testing.T, ok bool) {
Expand Down

0 comments on commit 1c3720b

Please sign in to comment.