Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RFC3339 to format dates, fixes #16 #32

Merged
merged 1 commit into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 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,14 @@ type PersonInfo struct {
}

func (pi *PersonInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
date, err := pi.date()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{
"name": pi.Name,
"email": pi.Email,
"date": pi.Date + " " + pi.Timezone,
"date": *date,
})
}

Expand Down Expand Up @@ -77,12 +82,31 @@ func (pi *PersonInfo) resolve(p []string) (interface{}, []string, error) {
case "email":
return pi.Email, p[1:], nil
case "date":
return pi.Date + " " + pi.Timezone, p[1:], nil
date, err := pi.date()
if err != nil {
return nil, nil, err
}
return date.Format(time.RFC3339), p[1:], nil
default:
return nil, nil, errors.New("no such link")
}
}

func (pi *PersonInfo) date() (*time.Time, error) {
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
sameer marked this conversation as resolved.
Show resolved Hide resolved
location := time.FixedZone("UTC", hr*60*60+mm*60)
date := time.Unix(sec, 0).In(location)
return &date, nil
}

type MergeTag struct {
Object cid.Cid `json:"object"`
Type string `json:"type"`
Expand Down
20 changes: 19 additions & 1 deletion git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,30 @@ func TestParsePersonInfo(t *testing.T) {
}

assert(t, pi.String() == "Someone <[email protected]>")

pi, err = parsePersonInfo([]byte("prefix Łukasz Magiera <[email protected]> 1546187652 +0100"))
piJSON, err := pi.MarshalJSON()
date, _, err := pi.resolve([]string{"date"})
assert(t, string(piJSON) == `{"date":"2018-12-30T17:34:12+01:00","email":"[email protected]","name":"Łukasz Magiera"}`)
assert(t, date == "2018-12-30T17:34:12+01:00")

pi, err = parsePersonInfo([]byte("prefix Sameer <[email protected]> 1545162499 -0500"))
piJSON, err = pi.MarshalJSON()
assert(t, string(piJSON) == `{"date":"2018-12-18T14:48:19-05:00","email":"[email protected]","name":"Sameer"}`)

pi, err = parsePersonInfo([]byte("prefix Łukasz Magiera <[email protected]> 1546187652 +0122"))
piJSON, err = pi.MarshalJSON()
assert(t, string(piJSON) == `{"date":"2018-12-30T17:56:12+01:22","email":"[email protected]","name":"Łukasz Magiera"}`)

pi, err = parsePersonInfo([]byte("prefix Sameer <[email protected]> 1545162499 -0545"))
piJSON, err = pi.MarshalJSON()
assert(t, string(piJSON) == `{"date":"2018-12-18T14:03:19-05:45","email":"[email protected]","name":"Sameer"}`)
}

func assert(t *testing.T, ok bool) {
if !ok {
fmt.Printf("\n")
t.Fatal("Assertion failed")
panic("Assertion failed")
}
}

Expand Down