Skip to content

Commit 81825d3

Browse files
committed
Add support for local timezone on ISO8601 output
to be able to provide a appropriate time representation the fix conversion to UTC is removed. Also the time.RFC3339 format is used instead of a custom format constant
1 parent 2dcc18f commit 81825d3

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

constants.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const (
1111
annotationISO8601 = "iso8601"
1212
annotationSeperator = ","
1313

14-
iso8601TimeFormat = "2006-01-02T15:04:05Z"
15-
1614
// MediaType is the identifier for the JSON API media type
1715
//
1816
// see http://jsonapi.org/format/#document-structure

request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
278278
break
279279
}
280280

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

330-
v, err := time.Parse(iso8601TimeFormat, tm)
330+
v, err := time.Parse(time.RFC3339, tm)
331331
if err != nil {
332332
er = ErrInvalidISO8601
333333
break

response.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
311311
}
312312

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

333333
if iso8601 {
334-
node.Attributes[args[1]] = tm.UTC().Format(iso8601TimeFormat)
334+
node.Attributes[args[1]] = tm.Format(time.RFC3339)
335335
} else {
336336
node.Attributes[args[1]] = tm.Unix()
337337
}

response_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,35 @@ func TestMarshalISO8601Time(t *testing.T) {
468468
}
469469
}
470470

471+
func TestMarshalISO8601TimeWithLocalTimezone(t *testing.T) {
472+
loc, _ := time.LoadLocation("Europe/Vienna")
473+
474+
testModel := &Timestamp{
475+
ID: 5,
476+
Time: time.Date(2016, 8, 17, 8, 27, 12, 23849, loc),
477+
}
478+
479+
out := bytes.NewBuffer(nil)
480+
if err := MarshalPayload(out, testModel); err != nil {
481+
t.Fatal(err)
482+
}
483+
484+
resp := new(OnePayload)
485+
if err := json.NewDecoder(out).Decode(resp); err != nil {
486+
t.Fatal(err)
487+
}
488+
489+
data := resp.Data
490+
491+
if data.Attributes == nil {
492+
t.Fatalf("Expected attributes")
493+
}
494+
495+
if data.Attributes["timestamp"] != "2016-08-17T08:27:12+02:00" {
496+
t.Fatal("Timestamp was not serialised into ISO8601 correctly")
497+
}
498+
}
499+
471500
func TestMarshalISO8601TimePointer(t *testing.T) {
472501
tm := time.Date(2016, 8, 17, 8, 27, 12, 23849, time.UTC)
473502
testModel := &Timestamp{
@@ -496,6 +525,36 @@ func TestMarshalISO8601TimePointer(t *testing.T) {
496525
}
497526
}
498527

528+
func TestMarshalISO8601TimePointerWithLocalTimezone(t *testing.T) {
529+
loc, _ := time.LoadLocation("Europe/Vienna")
530+
531+
tm := time.Date(2016, 8, 17, 8, 27, 12, 23849, loc)
532+
testModel := &Timestamp{
533+
ID: 5,
534+
Next: &tm,
535+
}
536+
537+
out := bytes.NewBuffer(nil)
538+
if err := MarshalPayload(out, testModel); err != nil {
539+
t.Fatal(err)
540+
}
541+
542+
resp := new(OnePayload)
543+
if err := json.NewDecoder(out).Decode(resp); err != nil {
544+
t.Fatal(err)
545+
}
546+
547+
data := resp.Data
548+
549+
if data.Attributes == nil {
550+
t.Fatalf("Expected attributes")
551+
}
552+
553+
if data.Attributes["next"] != "2016-08-17T08:27:12+02:00" {
554+
t.Fatal("Next was not serialised into ISO8601 correctly")
555+
}
556+
}
557+
499558
func TestSupportsLinkable(t *testing.T) {
500559
testModel := &Blog{
501560
ID: 5,

0 commit comments

Comments
 (0)