-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
43 lines (38 loc) · 896 Bytes
/
json.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
package date
import (
"errors"
"time"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// UnmarshalJSON to parse the JSON
func (d *Date) UnmarshalJSON(bs []byte) error {
var s string
err := json.Unmarshal(bs, &s)
if err != nil {
return err
}
if s == "" {
*d = NewZero()
return nil
}
t, err := time.ParseInLocation(RFC3339Date, s, time.UTC)
if err != nil {
return err
}
*d = New(t)
return nil
}
// MarshalJSON marshal to the JSON
func (d Date) MarshalJSON() ([]byte, error) {
if d.IsZero() {
return json.Marshal(nil)
}
var t = d.ToTime()
if y := t.Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("date.MarshalJSON: year outside of range [0,9999]")
}
return json.Marshal(t.Format(RFC3339Date))
}