-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
218 lines (187 loc) · 5.13 KB
/
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package schema
import (
"bytes"
"database/sql/driver"
"errors"
"fmt"
"math"
"time"
"github.com/volatiletech/null"
)
var (
// DateTimeFormat represents the default format of the time
DateTimeFormat = "20060102"
// TimeNegativeInfinity represents the negative infinity time
TimeNegativeInfinity = time.Date(math.MinInt32, time.January, 1, 0, 0, 0, 0, time.UTC)
// TimePositiveInfinity represents the positive infinity time
TimePositiveInfinity = time.Date(math.MaxInt32, time.December, 31, 23, 59, 59, 1e9-1, time.UTC)
)
// A Date represents an instant in time with nanosecond precision.
type Date struct {
time.Time
}
// Scan reads the value from db
func (t *Date) Scan(value interface{}) error {
switch x := value.(type) {
case time.Time:
*t = Date{Time: x}
case nil:
return nil
default:
return fmt.Errorf("schema: cannot scan type %T into schema.NullDate: %v", value, value)
}
return nil
}
// Value returns the DB value
func (t Date) Value() (driver.Value, error) {
null := Date{}
if t == null {
return nil, nil
}
return t.Time, nil
}
// MarshalText implements the encoding.TextMarshaler interface.
// The time is formatted in RFC 3339 format, with sub-second precision added if present.
func (t Date) MarshalText() ([]byte, error) {
if y := t.Year(); y < 0 || y >= 10000 {
return nil, errors.New("Time.MarshalText: year outside of range [0,9999]")
}
b := make([]byte, 0, len(DateTimeFormat))
return t.AppendFormat(b, DateTimeFormat), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The time is expected to be in RFC 3339 format.
func (t *Date) UnmarshalText(data []byte) error {
// Fractional seconds are handled implicitly by Parse.
parsed, err := time.Parse(DateTimeFormat, string(data))
if err != nil {
return err
}
*t = Date{parsed}
return nil
}
// MarshalJSON implements the json.Marshaler interface.
// The time is a quoted string in RFC 3339 format, with sub-second precision added if present.
func (t Date) MarshalJSON() ([]byte, error) {
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("Time.MarshalJSON: year outside of range [0,9999]")
}
b := make([]byte, 0, len(DateTimeFormat)+2)
b = append(b, '"')
b = t.AppendFormat(b, DateTimeFormat)
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format.
func (t *Date) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == string(null.NullBytes) {
return nil
}
// Fractional seconds are handled implicitly by Parse.
parsed, err := time.Parse(`"`+DateTimeFormat+`"`, string(data))
if err != nil {
return err
}
*t = Date{parsed}
return nil
}
// A NullDate represents an instant in time with nanosecond precision.
type NullDate struct {
Date Date
Valid bool
}
// NewNullDate creates a new Time.
func NewNullDate(t time.Time, valid bool) NullDate {
return NullDate{
Date: Date{t},
Valid: valid,
}
}
// NullDateFrom creates a new Time that will always be valid.
func NullDateFrom(t time.Time) NullDate {
return NewNullDate(t, true)
}
// NullDateFromPtr creates a new Time that will be null if t is nil.
func NullDateFromPtr(t *time.Time) NullDate {
if t == nil {
return NewNullDate(time.Time{}, false)
}
return NewNullDate(*t, true)
}
// MarshalJSON implements json.Marshaler.
func (t NullDate) MarshalJSON() ([]byte, error) {
if !t.Valid {
return null.NullBytes, nil
}
return t.Date.MarshalJSON()
}
// UnmarshalJSON implements json.Unmarshaler.
func (t *NullDate) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, null.NullBytes) {
t.Valid = false
t.Date = Date{}
return nil
}
if err := t.Date.UnmarshalJSON(data); err != nil {
return err
}
t.Valid = true
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (t NullDate) MarshalText() ([]byte, error) {
if !t.Valid {
return null.NullBytes, nil
}
return t.Date.MarshalText()
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (t *NullDate) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
t.Valid = false
return nil
}
if err := t.Date.UnmarshalText(text); err != nil {
return err
}
t.Valid = true
return nil
}
// SetValid changes this Time's value and sets it to be non-null.
func (t *NullDate) SetValid(v time.Time) {
t.Date = Date{v}
t.Valid = true
}
// Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.
func (t NullDate) Ptr() *time.Time {
if !t.Valid {
return nil
}
return &t.Date.Time
}
// Scan implements the Scanner interface.
func (t *NullDate) Scan(value interface{}) error {
var err error
switch x := value.(type) {
case time.Time:
t.Date = Date{x}
case nil:
t.Valid = false
return nil
default:
err = fmt.Errorf("schema: cannot scan type %T into schema.NullDate: %v", value, value)
}
t.Valid = err == nil
return err
}
// Value implements the driver Valuer interface.
func (t NullDate) Value() (driver.Value, error) {
if !t.Valid {
return nil, nil
}
return t.Date, nil
}