-
Notifications
You must be signed in to change notification settings - Fork 13
/
time.go
42 lines (35 loc) · 908 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
package v1
import (
"time"
)
const (
// APIServerTimeFormat - api-server time lacks the colon in timezone
APIServerTimeFormat = "2006-01-02T15:04:05.000-0700"
// APIServerTimeFormatAlt - api-server time with colon in timezone
APIServerTimeFormatAlt = "2006-01-02T15:04:05.000-07:00"
)
// Time - time
type Time time.Time
// UnmarshalJSON - unmarshal json for time
func (t *Time) UnmarshalJSON(bytes []byte) error {
tt, err := time.Parse(`"`+APIServerTimeFormat+`"`, string(bytes))
if err == nil {
*t = Time(tt)
return nil
}
tt, err = time.Parse(`"`+APIServerTimeFormatAlt+`"`, string(bytes))
if err != nil {
return err
}
*t = Time(tt)
return nil
}
// MarshalJSON -
func (t Time) MarshalJSON() ([]byte, error) {
tt := time.Time(t)
b := make([]byte, 0, len(APIServerTimeFormat)+2)
b = append(b, '"')
b = tt.AppendFormat(b, APIServerTimeFormat)
b = append(b, '"')
return b, nil
}