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

Add support for local timezone on ISO8601 output #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions constants.go
Original file line number Diff line number Diff line change
@@ -11,8 +11,6 @@ const (
annotationISO8601 = "iso8601"
annotationSeperator = ","

iso8601TimeFormat = "2006-01-02T15:04:05Z"

// MediaType is the identifier for the JSON API media type
//
// see http://jsonapi.org/format/#document-structure
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
@@ -278,7 +278,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
break
}

t, err := time.Parse(iso8601TimeFormat, tm)
t, err := time.Parse(time.RFC3339, tm)
if err != nil {
er = ErrInvalidISO8601
break
@@ -327,7 +327,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
break
}

v, err := time.Parse(iso8601TimeFormat, tm)
v, err := time.Parse(time.RFC3339, tm)
if err != nil {
er = ErrInvalidISO8601
break
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
@@ -311,7 +311,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
}

if iso8601 {
node.Attributes[args[1]] = t.UTC().Format(iso8601TimeFormat)
node.Attributes[args[1]] = t.Format(time.RFC3339)
} else {
node.Attributes[args[1]] = t.Unix()
}
@@ -331,7 +331,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
}

if iso8601 {
node.Attributes[args[1]] = tm.UTC().Format(iso8601TimeFormat)
node.Attributes[args[1]] = tm.Format(time.RFC3339)
} else {
node.Attributes[args[1]] = tm.Unix()
}
59 changes: 59 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -468,6 +468,35 @@ func TestMarshalISO8601Time(t *testing.T) {
}
}

func TestMarshalISO8601TimeWithLocalTimezone(t *testing.T) {
loc, _ := time.LoadLocation("Europe/Vienna")

testModel := &Timestamp{
ID: 5,
Time: time.Date(2016, 8, 17, 8, 27, 12, 23849, loc),
}

out := bytes.NewBuffer(nil)
if err := MarshalPayload(out, testModel); err != nil {
t.Fatal(err)
}

resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}

data := resp.Data

if data.Attributes == nil {
t.Fatalf("Expected attributes")
}

if data.Attributes["timestamp"] != "2016-08-17T08:27:12+02:00" {
t.Fatal("Timestamp was not serialised into ISO8601 correctly")
}
}

func TestMarshalISO8601TimePointer(t *testing.T) {
tm := time.Date(2016, 8, 17, 8, 27, 12, 23849, time.UTC)
testModel := &Timestamp{
@@ -496,6 +525,36 @@ func TestMarshalISO8601TimePointer(t *testing.T) {
}
}

func TestMarshalISO8601TimePointerWithLocalTimezone(t *testing.T) {
loc, _ := time.LoadLocation("Europe/Vienna")

tm := time.Date(2016, 8, 17, 8, 27, 12, 23849, loc)
testModel := &Timestamp{
ID: 5,
Next: &tm,
}

out := bytes.NewBuffer(nil)
if err := MarshalPayload(out, testModel); err != nil {
t.Fatal(err)
}

resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}

data := resp.Data

if data.Attributes == nil {
t.Fatalf("Expected attributes")
}

if data.Attributes["next"] != "2016-08-17T08:27:12+02:00" {
t.Fatal("Next was not serialised into ISO8601 correctly")
}
}

func TestSupportsLinkable(t *testing.T) {
testModel := &Blog{
ID: 5,