Skip to content

Commit 785fdcd

Browse files
committed
time: fix JSON-related methods
1 parent 0efefb7 commit 785fdcd

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

time.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,27 @@ func NumericDate(tt time.Time) *Time {
1717
if tt.Before(internal.Epoch) {
1818
tt = internal.Epoch
1919
}
20-
return &Time{time.Unix(tt.Unix(), 0)}
21-
}
22-
23-
// IsZero checks whether no seconds have elapsed since epoch.
24-
func (t *Time) IsZero() bool {
25-
return t == nil || t.Before(internal.Epoch) || t.Equal(internal.Epoch)
20+
return &Time{time.Unix(tt.Unix(), 0)} // set time using Unix time
2621
}
2722

2823
// MarshalJSON implements a marshaling function for time-related claims.
29-
func (t *Time) MarshalJSON() ([]byte, error) {
30-
if t.IsZero() {
24+
func (t Time) MarshalJSON() ([]byte, error) {
25+
if t.Before(internal.Epoch) {
3126
return json.Marshal(0)
3227
}
3328
return json.Marshal(t.Unix())
3429
}
3530

3631
// UnmarshalJSON implements an unmarshaling function for time-related claims.
3732
func (t *Time) UnmarshalJSON(b []byte) error {
38-
var unix int64
33+
var unix *int64
3934
if err := json.Unmarshal(b, &unix); err != nil {
4035
return err
4136
}
42-
if unix <= 0 {
37+
if unix == nil {
4338
return nil
4439
}
45-
tt := time.Unix(unix, 0)
40+
tt := time.Unix(*unix, 0)
4641
if tt.Before(internal.Epoch) {
4742
tt = internal.Epoch
4843
}

time_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestTimeMarshalJSON(t *testing.T) {
1818
{jwt.Time{}, 0},
1919
{jwt.Time{now}, now.Unix()},
2020
{jwt.Time{now.Add(24 * time.Hour)}, now.Add(24 * time.Hour).Unix()},
21+
{jwt.Time{now.Add(24 * 30 * 12 * time.Hour)}, now.Add(24 * 30 * 12 * time.Hour).Unix()},
2122
}
2223
for _, tc := range testCases {
2324
t.Run("", func(t *testing.T) {
@@ -39,17 +40,23 @@ func TestTimeMarshalJSON(t *testing.T) {
3940
func TestTimeUnmarshalJSON(t *testing.T) {
4041
now := time.Now()
4142
testCases := []struct {
42-
n int64
43-
want jwt.Time
43+
n int64
44+
want jwt.Time
45+
isNil bool
4446
}{
45-
{now.Unix(), jwt.Time{now}},
46-
{internal.Epoch.Unix() - 1337, jwt.Time{}},
47-
{internal.Epoch.Unix(), jwt.Time{}},
48-
{internal.Epoch.Unix() + 1337, jwt.Time{internal.Epoch.Add(1337 * time.Second)}},
47+
{now.Unix(), jwt.Time{now}, false},
48+
{internal.Epoch.Unix() - 1337, jwt.Time{internal.Epoch}, false},
49+
{internal.Epoch.Unix(), jwt.Time{internal.Epoch}, false},
50+
{internal.Epoch.Unix() + 1337, jwt.Time{internal.Epoch.Add(1337 * time.Second)}, false},
51+
{0, jwt.Time{}, true},
4952
}
5053
for _, tc := range testCases {
5154
t.Run("", func(t *testing.T) {
52-
b, err := json.Marshal(tc.n)
55+
var n *int64
56+
if !tc.isNil {
57+
n = &tc.n
58+
}
59+
b, err := json.Marshal(n)
5360
if err != nil {
5461
t.Fatal(err)
5562
}

0 commit comments

Comments
 (0)