-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdate.go
54 lines (42 loc) · 1.1 KB
/
date.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
package jsonschema
import (
"encoding"
"encoding/json"
"time"
)
// DateLayout describes date format.
const DateLayout = "2006-01-02"
// Date is a date represented in YYYY-MM-DD format.
type Date time.Time
var (
_ encoding.TextMarshaler = new(Date)
_ encoding.TextUnmarshaler = new(Date)
_ json.Marshaler = new(Date)
_ json.Unmarshaler = new(Date)
)
// UnmarshalText loads date from a standard format value.
func (d *Date) UnmarshalText(data []byte) error {
t, err := time.Parse(DateLayout, string(data))
if err != nil {
return err
}
*d = Date(t)
return nil
}
// MarshalText marshals date in standard format.
func (d Date) MarshalText() ([]byte, error) {
return []byte(time.Time(d).Format(DateLayout)), nil
}
// UnmarshalJSON unmarshals date in standard format.
func (d *Date) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
return d.UnmarshalText([]byte(s))
}
// MarshalJSON marshals date in standard format.
func (d Date) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(d).Format(DateLayout))
}