Skip to content

Commit 6d92ba7

Browse files
committed
Don't use pointer in TimeDuration.MarshalJSON
1 parent 698058b commit 6d92ba7

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

authority/provisioner/timeduration.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ type TimeDuration struct {
1919
d time.Duration
2020
}
2121

22+
// NewTimeDuration returns a TimeDuration with the defined time.
23+
func NewTimeDuration(t time.Time) TimeDuration {
24+
return TimeDuration{t: t}
25+
}
26+
2227
// ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or
2328
// time.Duration string.
2429
func ParseTimeDuration(s string) (TimeDuration, error) {
@@ -55,10 +60,8 @@ func (t *TimeDuration) SetTime(tt time.Time) {
5560
// MarshalJSON implements the json.Marshaler interface. If the time is set it
5661
// will return the time in RFC 3339 format if not it will return the duration
5762
// string.
58-
func (t *TimeDuration) MarshalJSON() ([]byte, error) {
63+
func (t TimeDuration) MarshalJSON() ([]byte, error) {
5964
switch {
60-
case t == nil:
61-
return []byte("null"), nil
6265
case t.t.IsZero():
6366
if t.d == 0 {
6467
return []byte("null"), nil
@@ -111,7 +114,7 @@ func (t *TimeDuration) Time() time.Time {
111114
t.t = now().Add(t.d)
112115
return t.t
113116
default:
114-
return t.t
117+
return t.t.UTC()
115118
}
116119
}
117120

authority/provisioner/timeduration_test.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ import (
66
"time"
77
)
88

9+
func TestNewTimeDuration(t *testing.T) {
10+
tm := time.Unix(1584198566, 535897000).UTC()
11+
type args struct {
12+
t time.Time
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want TimeDuration
18+
}{
19+
{"ok", args{tm}, TimeDuration{t: tm}},
20+
{"zero", args{time.Time{}}, TimeDuration{}},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := NewTimeDuration(tt.args.t); !reflect.DeepEqual(got, tt.want) {
25+
t.Errorf("NewTimeDuration() = %v, want %v", got, tt.want)
26+
}
27+
})
28+
}
29+
}
30+
931
func TestParseTimeDuration(t *testing.T) {
1032
type args struct {
1133
s string
@@ -111,15 +133,14 @@ func TestTimeDuration_MarshalJSON(t *testing.T) {
111133
tm := time.Unix(1584198566, 535897000).UTC()
112134
tests := []struct {
113135
name string
114-
timeDuration *TimeDuration
136+
timeDuration TimeDuration
115137
want []byte
116138
wantErr bool
117139
}{
118-
{"null", nil, []byte("null"), false},
119-
{"null", &TimeDuration{}, []byte("null"), false},
120-
{"timestamp", &TimeDuration{t: tm}, []byte(`"2020-03-14T15:09:26.535897Z"`), false},
121-
{"duration", &TimeDuration{d: 1 * time.Hour}, []byte(`"1h0m0s"`), false},
122-
{"fail", &TimeDuration{t: time.Date(-1, 0, 0, 0, 0, 0, 0, time.UTC)}, nil, true},
140+
{"null", TimeDuration{}, []byte("null"), false},
141+
{"timestamp", TimeDuration{t: tm}, []byte(`"2020-03-14T15:09:26.535897Z"`), false},
142+
{"duration", TimeDuration{d: 1 * time.Hour}, []byte(`"1h0m0s"`), false},
143+
{"fail", TimeDuration{t: time.Date(-1, 0, 0, 0, 0, 0, 0, time.UTC)}, nil, true},
123144
}
124145
for _, tt := range tests {
125146
t.Run(tt.name, func(t *testing.T) {
@@ -182,6 +203,7 @@ func TestTimeDuration_Time(t *testing.T) {
182203
{"zero", nil, time.Time{}},
183204
{"zero", &TimeDuration{}, time.Time{}},
184205
{"timestamp", &TimeDuration{t: tm}, tm},
206+
{"local", &TimeDuration{t: tm.Local()}, tm},
185207
{"duration", &TimeDuration{d: 1 * time.Hour}, tm.Add(1 * time.Hour)},
186208
}
187209
for _, tt := range tests {

0 commit comments

Comments
 (0)