-
Notifications
You must be signed in to change notification settings - Fork 14
/
jsontime.go
63 lines (55 loc) · 1.34 KB
/
jsontime.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package jsontime
import (
"encoding/json"
"errors"
"time"
)
// Time is an alias of time.Time.
//
// Can marshal to and from json and yaml.
type Time time.Time
// Time returns the equivalent time.Time struct.
func (t Time) Time() time.Time {
return time.Time(t)
}
// Time returns the equivalent time.Time struct.
func (t Time) String() string {
return time.Time(t).String()
}
// MarshalJSON implements json.Marshaller.
func (t Time) MarshalJSON() ([]byte, error) {
return time.Time(t).MarshalJSON()
}
// UnmarshalJSON implements json.Unmarshaller.
func (t *Time) UnmarshalJSON(data []byte) error {
var tt time.Time
if err := json.Unmarshal(data, &tt); err != nil {
return err
}
*t = Time(tt)
return nil
}
// MarshalYAML implements yaml.Marshaller.
func (t Time) MarshalYAML() (interface{}, error) {
if time.Time(t).Year() < 0 || time.Time(t).Year() > 10000 {
// As in Time.MarshalJSON
return nil, errors.New("Time.MarshalYAML: year outside range [0,9999]")
}
return time.Time(t).Format(time.RFC3339Nano), nil
}
// UnmarshalYAML implements yaml.Unmarshaller.
func (t *Time) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
if s == "null" {
return nil
}
tim, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
return err
}
*t = Time(tim)
return nil
}