-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
time.go
46 lines (40 loc) · 956 Bytes
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package jwt
import (
"encoding/json"
"time"
"github.com/gbrlsnchs/jwt/v3/internal"
)
// Time is the allowed format for time, as per the RFC 7519.
type Time struct {
time.Time
}
// NumericDate is a resolved Unix time.
func NumericDate(tt time.Time) *Time {
if tt.Before(internal.Epoch) {
tt = internal.Epoch
}
return &Time{time.Unix(tt.Unix(), 0)} // set time using Unix time
}
// MarshalJSON implements a marshaling function for time-related claims.
func (t Time) MarshalJSON() ([]byte, error) {
if t.Before(internal.Epoch) {
return json.Marshal(0)
}
return json.Marshal(t.Unix())
}
// UnmarshalJSON implements an unmarshaling function for time-related claims.
func (t *Time) UnmarshalJSON(b []byte) error {
var unix *int64
if err := json.Unmarshal(b, &unix); err != nil {
return err
}
if unix == nil {
return nil
}
tt := time.Unix(*unix, 0)
if tt.Before(internal.Epoch) {
tt = internal.Epoch
}
t.Time = tt
return nil
}