forked from rickb777/date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal.go
120 lines (106 loc) · 4.23 KB
/
marshal.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package date
import (
"bytes"
"errors"
)
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (d Date) MarshalBinary() ([]byte, error) {
enc := []byte{
byte(d.day >> 24),
byte(d.day >> 16),
byte(d.day >> 8),
byte(d.day),
}
return enc, nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (d *Date) UnmarshalBinary(data []byte) error {
if len(data) == 0 {
return errors.New("Date.UnmarshalBinary: no data")
}
if len(data) != 4 {
return errors.New("Date.UnmarshalBinary: invalid length")
}
d.day = PeriodOfDays(data[3]) | PeriodOfDays(data[2])<<8 | PeriodOfDays(data[1])<<16 | PeriodOfDays(data[0])<<24
return nil
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (ds DateString) MarshalBinary() ([]byte, error) {
return Date(ds).MarshalBinary()
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (ds *DateString) UnmarshalBinary(data []byte) error {
return (*Date)(ds).UnmarshalBinary(data)
}
// MarshalJSON implements the json.Marshaler interface.
// The date is given in ISO 8601 extended format (e.g. "2006-01-02").
// If the year of the date falls outside the [0,9999] range, this format
// produces an expanded year representation with possibly extra year digits
// beyond the prescribed four-digit minimum and with a + or - sign prefix
// (e.g. , "+12345-06-07", "-0987-06-05").
// Note that the zero value is marshalled as a blank string, which allows
// "omitempty" to work.
func (d Date) MarshalJSON() ([]byte, error) {
buf := &bytes.Buffer{}
buf.Grow(14)
buf.WriteByte('"')
d.WriteTo(buf)
buf.WriteByte('"')
return buf.Bytes(), nil
}
// MarshalText implements the encoding.TextMarshaler interface.
// The date is given in ISO 8601 extended format (e.g. "2006-01-02").
// If the year of the date falls outside the [0,9999] range, this format
// produces an expanded year representation with possibly extra year digits
// beyond the prescribed four-digit minimum and with a + or - sign prefix
// (e.g. , "+12345-06-07", "-0987-06-05").
func (d Date) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The date is expected to be in ISO 8601 extended format
// (e.g. "2006-01-02", "+12345-06-07", "-0987-06-05");
// the year must use at least 4 digits and if outside the [0,9999] range
// must be prefixed with a + or - sign.
// Note that the a blank string is unmarshalled as the zero value.
func (d *Date) UnmarshalText(data []byte) (err error) {
if len(data) == 0 {
return nil
}
u, err := ParseISO(string(data))
if err == nil {
d.day = u.day
}
return err
}
// MarshalJSON implements the json.Marshaler interface.
// The date is given in ISO 8601 extended format (e.g. "2006-01-02").
// If the year of the date falls outside the [0,9999] range, this format
// produces an expanded year representation with possibly extra year digits
// beyond the prescribed four-digit minimum and with a + or - sign prefix
// (e.g. , "+12345-06-07", "-0987-06-05").
// Note that the zero value is marshalled as a blank string, which allows
// "omitempty" to work.
func (ds DateString) MarshalJSON() ([]byte, error) {
return Date(ds).MarshalJSON()
}
// MarshalText implements the encoding.TextMarshaler interface.
// The date is given in ISO 8601 extended format (e.g. "2006-01-02").
// If the year of the date falls outside the [0,9999] range, this format
// produces an expanded year representation with possibly extra year digits
// beyond the prescribed four-digit minimum and with a + or - sign prefix
// (e.g. , "+12345-06-07", "-0987-06-05").
func (ds DateString) MarshalText() ([]byte, error) {
return Date(ds).MarshalText()
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The date is expected to be in ISO 8601 extended format
// (e.g. "2006-01-02", "+12345-06-07", "-0987-06-05");
// the year must use at least 4 digits and if outside the [0,9999] range
// must be prefixed with a + or - sign.
func (ds *DateString) UnmarshalText(data []byte) (err error) {
return (*Date)(ds).UnmarshalText(data)
}