Skip to content

Commit

Permalink
Merge pull request kubernetes#5416 from smarterclayton/use_utc_serial…
Browse files Browse the repository at this point in the history
…ized_and_local_otherwise

Use UTC when marshalling times and Local when unmarshalling
  • Loading branch information
satnam6502 committed Mar 20, 2015
2 parents 8fe47aa + d516a86 commit ec77230
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (t *Time) UnmarshalJSON(b []byte) error {
return err
}

t.Time = pt
t.Time = pt.Local()
return nil
}

Expand All @@ -97,7 +97,7 @@ func (t Time) MarshalJSON() ([]byte, error) {
return []byte("null"), nil
}

return json.Marshal(t.Format(time.RFC3339))
return json.Marshal(t.UTC().Format(time.RFC3339))
}

// Fuzz satisfies fuzz.Interface.
Expand Down
27 changes: 16 additions & 11 deletions pkg/util/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package util

import (
"encoding/json"
"reflect"
"testing"
"time"

Expand All @@ -35,7 +34,7 @@ func TestTimeMarshalYAML(t *testing.T) {
result string
}{
{Time{}, "t: null\n"},
{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "t: 1998-05-05T05:05:05Z\n"},
{Date(1998, time.May, 5, 1, 5, 5, 50, time.FixedZone("test", -4*60*60)), "t: 1998-05-05T05:05:05Z\n"},
{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: 1998-05-05T05:05:05Z\n"},
}

Expand All @@ -57,7 +56,7 @@ func TestTimeUnmarshalYAML(t *testing.T) {
result Time
}{
{"t: null\n", Time{}},
{"t: 1998-05-05T05:05:05Z\n", Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)},
{"t: 1998-05-05T05:05:05Z\n", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
}

for _, c := range cases {
Expand Down Expand Up @@ -99,7 +98,7 @@ func TestTimeUnmarshalJSON(t *testing.T) {
result Time
}{
{"{\"t\":null}", Time{}},
{"{\"t\":\"1998-05-05T05:05:05Z\"}", Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)},
{"{\"t\":\"1998-05-05T05:05:05Z\"}", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
}

for _, c := range cases {
Expand All @@ -118,25 +117,31 @@ func TestTimeMarshalJSONUnmarshalYAML(t *testing.T) {
input Time
}{
{Time{}},
{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC).Rfc3339Copy()},
{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Rfc3339Copy()},
{Date(1998, time.May, 5, 5, 5, 5, 50, time.Local).Rfc3339Copy()},
{Date(1998, time.May, 5, 5, 5, 5, 0, time.Local).Rfc3339Copy()},
}

for _, c := range cases {
for i, c := range cases {
input := TimeHolder{c.input}
jsonMarshalled, err := json.Marshal(&input)
if err != nil {
t.Errorf("1: Failed to marshal input: '%v': %v", input, err)
t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err)
}

var result TimeHolder
err = yaml.Unmarshal(jsonMarshalled, &result)
if err != nil {
t.Errorf("2: Failed to unmarshal '%+v': %v", string(jsonMarshalled), err)
t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err)
}

iN, iO := input.T.Zone()
oN, oO := result.T.Zone()
if iN != oN || iO != oO {
t.Errorf("%d-3: Time zones differ before and after serialization %s:%d %s:%d", i, iN, iO, oN, oO)
}

if !reflect.DeepEqual(input, result) {
t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result)
if input.T.UnixNano() != result.T.UnixNano() {
t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result)
}
}
}

0 comments on commit ec77230

Please sign in to comment.