From 6d92ba75b968e99a2cfeaea49d8bfd1acd5ecdc2 Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Mon, 25 Mar 2019 12:34:01 -0700 Subject: [PATCH] Don't use pointer in TimeDuration.MarshalJSON --- authority/provisioner/timeduration.go | 11 ++++--- authority/provisioner/timeduration_test.go | 34 ++++++++++++++++++---- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/authority/provisioner/timeduration.go b/authority/provisioner/timeduration.go index c0f29ac2..8c27d463 100644 --- a/authority/provisioner/timeduration.go +++ b/authority/provisioner/timeduration.go @@ -19,6 +19,11 @@ type TimeDuration struct { d time.Duration } +// NewTimeDuration returns a TimeDuration with the defined time. +func NewTimeDuration(t time.Time) TimeDuration { + return TimeDuration{t: t} +} + // ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or // time.Duration string. func ParseTimeDuration(s string) (TimeDuration, error) { @@ -55,10 +60,8 @@ func (t *TimeDuration) SetTime(tt time.Time) { // MarshalJSON implements the json.Marshaler interface. If the time is set it // will return the time in RFC 3339 format if not it will return the duration // string. -func (t *TimeDuration) MarshalJSON() ([]byte, error) { +func (t TimeDuration) MarshalJSON() ([]byte, error) { switch { - case t == nil: - return []byte("null"), nil case t.t.IsZero(): if t.d == 0 { return []byte("null"), nil @@ -111,7 +114,7 @@ func (t *TimeDuration) Time() time.Time { t.t = now().Add(t.d) return t.t default: - return t.t + return t.t.UTC() } } diff --git a/authority/provisioner/timeduration_test.go b/authority/provisioner/timeduration_test.go index 22cfa678..97dd4ce5 100644 --- a/authority/provisioner/timeduration_test.go +++ b/authority/provisioner/timeduration_test.go @@ -6,6 +6,28 @@ import ( "time" ) +func TestNewTimeDuration(t *testing.T) { + tm := time.Unix(1584198566, 535897000).UTC() + type args struct { + t time.Time + } + tests := []struct { + name string + args args + want TimeDuration + }{ + {"ok", args{tm}, TimeDuration{t: tm}}, + {"zero", args{time.Time{}}, TimeDuration{}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewTimeDuration(tt.args.t); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewTimeDuration() = %v, want %v", got, tt.want) + } + }) + } +} + func TestParseTimeDuration(t *testing.T) { type args struct { s string @@ -111,15 +133,14 @@ func TestTimeDuration_MarshalJSON(t *testing.T) { tm := time.Unix(1584198566, 535897000).UTC() tests := []struct { name string - timeDuration *TimeDuration + timeDuration TimeDuration want []byte wantErr bool }{ - {"null", nil, []byte("null"), false}, - {"null", &TimeDuration{}, []byte("null"), false}, - {"timestamp", &TimeDuration{t: tm}, []byte(`"2020-03-14T15:09:26.535897Z"`), false}, - {"duration", &TimeDuration{d: 1 * time.Hour}, []byte(`"1h0m0s"`), false}, - {"fail", &TimeDuration{t: time.Date(-1, 0, 0, 0, 0, 0, 0, time.UTC)}, nil, true}, + {"null", TimeDuration{}, []byte("null"), false}, + {"timestamp", TimeDuration{t: tm}, []byte(`"2020-03-14T15:09:26.535897Z"`), false}, + {"duration", TimeDuration{d: 1 * time.Hour}, []byte(`"1h0m0s"`), false}, + {"fail", TimeDuration{t: time.Date(-1, 0, 0, 0, 0, 0, 0, time.UTC)}, nil, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -182,6 +203,7 @@ func TestTimeDuration_Time(t *testing.T) { {"zero", nil, time.Time{}}, {"zero", &TimeDuration{}, time.Time{}}, {"timestamp", &TimeDuration{t: tm}, tm}, + {"local", &TimeDuration{t: tm.Local()}, tm}, {"duration", &TimeDuration{d: 1 * time.Hour}, tm.Add(1 * time.Hour)}, } for _, tt := range tests {