Skip to content

Commit

Permalink
fix(JsonTimeISO8601): use UTC TZ for String()
Browse files Browse the repository at this point in the history
+ Amazon seems to only accept UTC timezone in some cases
https://github.com/amzn/selling-partner-api-docs/issues/3324
#48
  • Loading branch information
Coffeeri committed Dec 22, 2023
1 parent 580ac2d commit 80d631e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
7 changes: 5 additions & 2 deletions apis/json_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ type JsonTimeISO8601 struct {
}

func (t JsonTimeISO8601) MarshalJSON() ([]byte, error) {
value := "\"" + t.Format(time.RFC3339) + "\""
value := "\"" + t.UTC().Format(time.RFC3339) + "\""
return []byte(value), nil
}

func (t JsonTimeISO8601) String() string {
return t.Format(time.RFC3339)
if t.IsZero() {
return ""
}
return t.UTC().Format(time.RFC3339)
}
26 changes: 25 additions & 1 deletion apis/json_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@ func TestJsonTimeISO8601_MarshalJSON(t1 *testing.T) {
wantErr bool
}{
{
name: "Seconds",
name: "Seconds in UTC",
fields: fields{
Time: time.Date(2022, 02, 26, 9, 12, 11, 0, time.UTC),
},
want: []byte("\"2022-02-26T09:12:11Z\""),
wantErr: false,
},
{
name: "Zero Time",
fields: fields{
Time: time.Time{},
},
want: []byte("\"0001-01-01T00:00:00Z\""),
wantErr: false,
},
{
name: "Time in PST",
fields: fields{
Time: time.Date(2022, 02, 26, 1, 12, 11, 0, time.FixedZone("PST", -8*3600)),
},
want: []byte("\"2022-02-26T09:12:11Z\""), // Converted to UTC
wantErr: false,
},
{
name: "Time in IST",
fields: fields{
Time: time.Date(2022, 02, 26, 14, 42, 11, 0, time.FixedZone("IST", 5*3600+1800)),
},
want: []byte("\"2022-02-26T09:12:11Z\""), // Converted to UTCc
wantErr: false,
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
Expand Down

0 comments on commit 80d631e

Please sign in to comment.